Scripts
Imagegraph
Deze functie gedraagt zich identiek aan alle andere GD functies en schrijft een grafiek in een image buffer. Gebruik: imagegraph(buffer &$img, string $title, int $mode, array $data); Waarbij geldt: $img een GD image buffer, vantevoren aangemaakt $title een string voor de titel boven aan de grafiek $mode staat voor 1:lijn 2:puntjes $data is een multi dimensional array met de data in de vorm van array(array(x=>y), array(x=>y), ..) Fix(26-04-06): Voor 'y(x) = c' functies een devide by zero gefixt. Ja, ook dit script is onder de GPL uit gegeven, houd daar alstublieft rekening mee.
imagegraph
// Functie
<?php
function imagegraph(&$img, $title, $mode, $data) {
if (!$img || ($mode != 1 && $mode != 2) || count($data) == 0) return false;
$x = imagesx($img);
$y = imagesy($img);
$white = imagecolorallocate($img, 255, 255, 255);
$black = imagecolorallocate($img, 0, 0, 0);
$grey = imagecolorallocate($img, 210, 210, 210);
$darkgrey = imagecolorallocate($img, 170, 170, 170);
$colors = array();
$colors[] = imagecolorallocate($img, 255, 0, 0);
$colors[] = imagecolorallocate($img, 0, 255, 0);
$colors[] = imagecolorallocate($img, 0, 0, 255);
$colors[] = imagecolorallocate($img, 255, 255, 0);
$colors[] = imagecolorallocate($img, 255, 0, 255);
$colors[] = imagecolorallocate($img, 0, 255, 255);
imagefill($img, 0, 0, $white);
imagerectangle($img, 0, 0, $x-1, $y-1, $black); //border
imagefilledrectangle($img, 30, 15, $x-15, $y-30, $grey); //achtergrond
imageline($img, 30, 15, 30, $y-30, $black); //y as
imageline($img, 30, $y-30, $x-15, $y-30, $black); //x as
$fy = imagefontheight(1);
$fx = imagefontwidth(1);
$rasterx = 45;
while ($rasterx <= $x-15) { //x raster
imageline($img, $rasterx, $y-31, $rasterx, 15, $darkgrey);
$rasterx += 15;
}
$rastery = $y-45;
while ($rastery >= 15) { //y raster
imageline($img, 31, $rastery, $x-15, $rastery, $darkgrey);
$rastery -= 15;
}
$graph = 0;
while($graph < count($data)) {
$datax = array_keys($data[$graph]);
$datay = array_values($data[$graph]);
if (!isset($largestx) || max($datax) > $largestx) $largestx = max($datax);
if (!isset($smallestx) || min($datax) < $smallestx) $smallestx = min($datax);
if (!isset($largesty) || max($datay) > $largesty) $largesty = max($datay);
if (!isset($smallesty) || min($datay) < $smallesty) $smallesty = min($datay);
$graph++;
}
$numrastx = ($x-45)/30;
$incx = ($largestx-$smallestx)/$numrastx;
$numrasty = ($y-45)/30;
$incy = ($largesty-$smallesty)/$numrasty;
$txtlocx = 30;
$i = 0;
while ($txtlocx <= $x-15) { //x as strings
imagestringup($img, 1, round($txtlocx-($fy/2)), $y-30+strlen(round($i*$incx+$smallestx, 0))*$fx, round($i*$incx+$smallestx, 0), $black);
$i++;
$txtlocx += 30;
}
$txtlocy = $y-30;
$i = 0;
while ($txtlocy >= 15) { //y as strings
imagestring($img, 1, 30-strlen(round($i*$incy+$smallesty, 0))*$fx, $txtlocy-round($fy/2), round($i*$incy+$smallesty, 0), $black);
$i++;
$txtlocy -= 30;
}
$graph = 0;
while ($graph < count($data)) {
if (!isset($colors[$graph])) break; //kleuren op :(
ksort($data[$graph]);
$datax = array_keys($data[$graph]);
$datay = array_values($data[$graph]);
$i = 1;
while ($i < count($datax)) {
if ($mode == 1) { //lijn voor graph
$dotx = ((($x-45)/($largestx-$smallestx))*($datax[$i-1]-$smallestx))+30;
$dotx2 = ((($x-45)/($largestx-$smallestx))*($datax[$i]-$smallestx))+30;
if ($largesty != $smallesty) {
$doty = $y-30-((($y-45)/($largesty-$smallesty))*($datay[$i-1]-$smallesty));
$doty2 = $y-30-((($y-45)/($largesty-$smallesty))*($datay[$i]-$smallesty));
} else {
$doty = $y-30-(($y-45)/2);
$doty2 = $y-30-(($y-45)/2);
}
imageline($img, $dotx, $doty, $dotx2, $doty2, $colors[$graph]);
} elseif ($mode == 2) { //dots voor graph
$dotx = ((($x-45)/($largestx-$smallestx))*($datax[$i]-$smallestx))+30;
if ($largesty != $smallesty) $doty = $y-30-((($y-45)/($largesty-$smallesty))*($datay[$i]-$smallesty));
else $doty = $y-30-(($y-45)/2);
imagefilledellipse($img, $dotx, $doty, 3, 3, $colors[$graph]);
}
$i++;
}
$graph++;
}
imagestring($img, 2, 30, 1, $title, $black);
return true;
}
?>
//Testcode voor voorbeeld
<?php
$data = array();
$i = -50;
while ($i <= 50) {
$data[0][$i] = sin(0.1*$i)*50;
$data[1][$i] = cos(0.1*$i)*50;
$data[2][$i] = sin(0.1*$i+pi())*50;
$data[3][$i] = cos(0.1*$i+pi())*50;
$data[4][$i] = $i;
$data[5][$i] = -$i;
$i++;
}
$img = imagecreatefromjpeg('/home/jorrizza/pics/boosmannetje.jpg');
$img2 = imagecreate(imagesx($img), imagesy($img));
imagegraph($img2, 'Testgrafiek', 1, $data);
imagecopymerge($img, $img2, 0, 0, 0, 0, imagesx($img2), imagesy($img2), 75);
header('Content-Type: image/png');
imagepng($img);
?>
Reacties
0