note 60691 added to ref.image

From: Date: Sat, 14 Jan 2006 00:02:07 +0000
Subject: note 60691 added to ref.image
Groups: php.notes 
Request: Send a blank email to php-notes+get-101734@lists.php.net to get a copy of this message
I've take the example of Peter Hulstaert and modified so: 1. grid width is scaled basing on the number of data points to print; 2. images are scaled by min and max values: even if min is 1000, that point will be very close to the image bottom Hope this will helpfull to someone. <? //Original from peter dot hulstaert at gmail dot com //Available at http://it.php.net/manual/en/ref.image.php#60410 header("Content-type: image/png"); // Define variables $Values=array(120,190,130,155,150,140,320,150,140,186,240,128,650); $imgWidth=500; $imgHeight=200; $grid=25; $graphspacing=0.07; //Scales number of vertical grid lines for the number of data to print $gridW=$imgWidth/(count($Values)-1); //Get min and max values to scale image for ($i=0; $i<count($Values); $i++){ if ($Values[$i]>$max){$max=$Values[$i];} } //Min values are less the maximum... $min = $max; for ($i=0; $i<count($Values); $i++){ if ($Values[$i]<$min){$min=$Values[$i];} } for ($i=0; $i<count($Values); $i++){ $graphValues[$i] = ($Values[$i] - $min*(1-2*$graphspacing)) * (($imgHeight*(1-$graphspacing))/($max-$min)); } // Create image and define colors $image=imagecreate($imgWidth, $imgHeight); $colorWhite=imagecolorallocate($image, 255, 255, 255); $colorGrey=imagecolorallocate($image, 192, 192, 192); $colorBlue=imagecolorallocate($image, 0, 0, 255); // Create border around image imageline($image, 0, 0, 0, $imgHeight, $colorGrey); imageline($image, 0, 0, $imgWidth, 0, $colorGrey); imageline($image, $imgWidth-1, 0, $imgWidth-1, $imgHeight-1, $colorGrey); imageline($image, 0, $imgHeight-1, $imgWidth-1, $imgHeight-1, $colorGrey); // Create grid for ($i=1; $i<($imgWidth/$gridW); $i++) {imageline($image, $i*$gridW, 0, $i*$gridW, $imgHeight, $colorGrey);} for ($i=1; $i<($imgHeight/$grid); $i++) {imageline($image, 0, $i*$grid, $imgWidth, $i*$grid, $colorGrey);} //Create the lines connecting points for ($i=0; $i<count($graphValues)-1; $i++) {imageline($image, $i*$gridW, ($imgHeight-$graphValues[$i]), ($i+1)*$gridW, ($imgHeight-$graphValues[$i+1]), $colorBlue);} // Output graph and clear image from memory imagepng($image); imagedestroy($image); ?> ---- Manual Page -- http://www.php.net/manual/en/ref.image.php Edit -- http://master.php.net/manage/user-notes.php?action=edit+60691 Delete: added to the manual -- http://master.php.net/manage/user-notes.php?action=delete+60691&report=yes&reason=added+to+the+manual Delete: bad code -- http://master.php.net/manage/user-notes.php?action=delete+60691&report=yes&reason=bad+code Delete: spam -- http://master.php.net/manage/user-notes.php?action=delete+60691&report=yes&reason=spam Delete: useless -- http://master.php.net/manage/user-notes.php?action=delete+60691&report=yes&reason=useless Delete: non-english -- http://master.php.net/manage/user-notes.php?action=delete+60691&report=yes&reason=non-english Delete: already in docs -- http://master.php.net/manage/user-notes.php?action=delete+60691&report=yes&reason=already+in+docs Delete: other reasons -- http://master.php.net/manage/user-notes.php?action=delete+60691&report=yes Reject -- http://master.php.net/manage/user-notes.php?action=reject+60691&report=yes Search -- http://master.php.net/manage/user-notes.php

« previous php.notes (#101734) next »