Tuesday, March 31, 2009

Latest updates 2

Dear readers,

actually im working on improve this args:


It should be nice to turn the schema into EBNF as another final example!

have a nice day!

Friday, March 13, 2009

Xiaolin wu circle php implementation

In this post i will finally close this works: Xiaolin Wu look like circle.

Here the function wu_cirlce (helped by distance() and new_color()) that plot an antialiased circle. You need GD supports, the params are commented. Hope this could help someone:


<?php
/*
@author mauro p
*/
function distance($r, $y) {
$real_point = sqrt(pow($r, 2) - pow($y, 2));
return ceil($real_point) - $real_point;
}

function new_color($i) {
return round(($i * 127));
}

/**
* $image a gd image resource
* $r circle's radius
* $color rgb array like array('red' => int(0 : 255), 'green' => int(0 : 255), 'blue' => int(0 : 255))
* $offset_x x axis circle's center
* $offset_y y axis circle's center
*/
function wu_circle($image, $r, $color, $offset_x = null, $offset_y = null) {
$red = $color["red"];
$green = $color["green"];
$blue = $color["blue"];
$offset_x = (is_null($offset_x)) ? 0 : $offset_x;
$offset_y = (is_null($offset_y)) ? 0 : $offset_y;
$x = $xx = $r;
$y = $yy = -1;
$t = 0;
$color = imagecolorallocate($image, $red, $green, $blue);
while($x > $y) {
$y++;
$current_distance = distance($r, $y);
if($current_distance < $t) {
$x--;
}

$trasparency = new_color($current_distance);
$alpha = imagecolorallocatealpha($image, $red, $green, $blue, $trasparency );
$alpha2 = imagecolorallocatealpha($image, $red, $green, $blue, 127 - $trasparency);
imagesetpixel($image, $x + $offset_x, $y + $offset_y, $color);
imagesetpixel($image, $x + $offset_x - 1, $y + $offset_y, $alpha2 );
imagesetpixel($image, $x + $offset_x + 1, $y + $offset_y, $alpha );

imagesetpixel($image, $y + $offset_x, $x + $offset_y, $color);
imagesetpixel($image, $y + $offset_x, $x + $offset_y - 1, $alpha2);
imagesetpixel($image, $y + $offset_x, $x + $offset_y + 1, $alpha);

imagesetpixel($image, $offset_x - $x , $y + $offset_y, $color);
imagesetpixel($image, $offset_x - $x + 1, $y + $offset_y, $alpha2);
imagesetpixel($image, $offset_x - $x - 1, $y + $offset_y, $alpha);

imagesetpixel($image, $offset_x - $y, $x + $offset_y, $color);
imagesetpixel($image, $offset_x - $y, $x + $offset_y - 1, $alpha2);
imagesetpixel($image, $offset_x - $y, $x + $offset_y + 1, $alpha);

imagesetpixel($image, $x + $offset_x, $offset_y - $y, $color);
imagesetpixel($image, $x + $offset_x - 1, $offset_y - $y, $alpha2);
imagesetpixel($image, $x + $offset_x + 1, $offset_y - $y, $alpha);

imagesetpixel($image, $y + $offset_x, $offset_y - $x, $color);
imagesetpixel($image, $y + $offset_x, $offset_y - $x - 1, $alpha);
imagesetpixel($image, $y + $offset_x, $offset_y - $x + 1, $alpha2);

imagesetpixel($image, $offset_x - $y, $offset_y - $x, $color);
imagesetpixel($image, $offset_x - $y, $offset_y - $x - 1, $alpha);
imagesetpixel($image, $offset_x - $y, $offset_y - $x + 1, $alpha2);

imagesetpixel($image, $offset_x - $x, $offset_y - $y, $color);
imagesetpixel($image, $offset_x - $x - 1, $offset_y - $y, $alpha);
imagesetpixel($image, $offset_x - $x + 1, $offset_y - $y, $alpha2);

$t = $current_distance;
}

return $image;
}

$image = imagecreatetruecolor(500, 500);
$c = array("red" => 255, "green" => 0, "blue" => 255);

$image = wu_circle($image, 50, $c, 150, 150);

header('Content-type: image/png');
imagepng($image);
imagedestroy($image);


the result:

Tuesday, March 10, 2009

MySql Database Inspector

Well, came back,
im very busy lately so my posts as you can see, decrease in number. Now i want to start in a new project (lol added to the lot i've already started). It's about mysql database, a simple web application that only show all databases structure in a web browser.

You need to download jquery to let it works.

The workflow is easy: it first put in a multidimensional php array all the information
array[dbs_name][tables_name][fields_name] = field_type.
then create div inside div to graphically render the structure, helped by jquery and putting information out with php.

In the future should be interesting improving it by adding a query box that helps the user in building the query by selecting the type of the query and filling it by clicking on the graphically structure, and obviously manually filling too.

Here the code:
"db_struct.php"
This file contains the function get_structure($mysql_connection_resource) which convert the databases into the array.

function get_structure($conn_res) {
$resdbs = null;
$restbls = null;
$resclmns = null;
$resdbs = mysql_list_dbs($conn_res);
$structure = array();

while($dbs = mysql_fetch_object($resdbs)) {
$dn = $dbs->Database;
$restbls = mysql_list_tables($dn, $conn_res);
while($tbls = mysql_fetch_array($restbls)) {
$tn = $tbls[0];
$resclmns = mysql_list_fields($dn, $tn, $conn_res);
$result = mysql_query("SHOW COLUMNS FROM $tn");
if (mysql_num_rows($result) > 0) {
while ($row = mysql_fetch_assoc($result)) {
$fn = $row['Field'];
$structure["$dn"]["$tn"]["$fn"] = $row["Type"];
}
}
}
}
return $structure;
}


"web gui"
This file is the web render. You need to put your connection data and a link to jquery (i used 2.6)
<?
require_once "db_struct.php";
$conn = mysql_connect("yourhost", "youruser", "yourpass");
$s = get_structure($conn);
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<script type="text/javascript" src="jquery_source"></script>
<style>
#main {
width: auto;
}
.database {
float: left;
border: 1px solid darkred;
background: #CCCCCC;
margin: 5px;
padding: 2px;
width: 300px;
overflow: auto;
}
.table {
border: 1px solid darkgreen;
background: #DDDDDD;
margin-bottom: 4px;
padding: 3px;
width: 280px;
overflow: auto;
}
.field {
border: 1px solid darkblue;
background: #EEEEEE;
margin-bottom: 2px;
padding: 2px;
width: 260px;
overflow: auto;
}
span {
font-weight: bold;
}
span.Db {
color: darkred;
font-size: 150%;
}
span.Tb {
color: darkgreen;
font-size: 130%;
}
</style>
</head>
<body>
<div class="closer">
<p>Collapse: </p>
<button id="cdb">All DBS</button>
<button id="ctb">All TBS</button>
<button id="cfd">All FDS</button>
</div>
<div id="main"></div>
<script type="text/javascript">
var main = $("#main");
<? foreach($s as $db => $tbls): ?>
var td = $("<div>");
td.html("<span class='Tit Db'><?= $db ?></span>");
td.addClass("database");
<? foreach($tbls as $tbl => $flds): ?>
var tt = $("<div>");
tt.html("<span class='Tit Tb'><?= $tbl ?></span>");
tt.addClass("table");
<? foreach($flds as $fld => $type): ?>
var tf = $("<div>");
tf.html("<i><?= $fld ?></i> : <?= $type ?>");
tf.addClass("field");
tt.append(tf);
<? endforeach; ?>
td.append(tt);
<? endforeach; ?>
main.append(td);
<? endforeach; ?>
$("#cdb").click(function() {
$(".database").toggle();
});
$("#ctb").click(function() {
$(".table").toggle();
});
$("#cfd").click(function() {
$(".field").toggle();
});
</script>
</body>
</html>
<? mysql_close($conn); ?>