PHP Drawing Example

The GD library is used to draw pictures in PHP. To enable GD, uncomment ;extension=php_gd2.dll in the configuration file "php.ini".

Let's have an example:

<?PHP
    $width=500;
	$height=300;
	$im = imagecreatetruecolor($width,$height);
	$gray = imagecolorallocate($im, 102, 102, 102);
	$white = imagecolorallocate($im, 255, 255, 255);
	$black = imagecolorallocate($im, 0, 0, 0);
	$blue   = imagecolorallocate($im, 0, 0, 255);
	$red    = imagecolorallocate($im, 255, 0, 0);
	$green  = imagecolorallocate($im, 0, 255, 0);
	imagefill($im,0,0,$white);

	$x_ori = 40;
	$y_ori = 60;
	$y_bottom = 60;
	$xlen = $width - $x_ori - $x_ori * 1.5;
	$ylen = $height - $y_ori - $y_bottom;

	imageline($im,$x_ori,$y_ori+$ylen,$x_ori+$xlen,$y_ori+$ylen,$black);  //x axis bottom
	imageline($im,$x_ori,$y_ori,$x_ori,$y_ori + $ylen,$black);  //y axis left
	imageline($im,$x_ori,$y_ori,$x_ori+$xlen,$y_ori,$black);  //x axis top
	imageline($im,$x_ori+$xlen,$y_ori,$x_ori+$xlen,$y_ori+$ylen,$black);  //y axis right

	imagestring($im,2,$x_ori + $xlen/2 -10,$y_ori + $ylen + 20,"width", $black);
	imagestringup($im,2,$x_ori-20,$y_ori + $ylen/2 + 20,"height", $black);			   
	
	$arry = array(1,50,100,150,200);
	
	for ($i=0;$i<5;$i++)
	{
	    $y = $y_ori + ($arry[$i] - $arry[0]) * $ylen/($arry[4]-$arry[0]);
		   
		$style=array($white,$white,$gray,$white,$white);
		imagesetstyle($im,$style);
		imageline($im,$x_ori+2,$y,$x_ori + $xlen -2,$y,IMG_COLOR_STYLED);
		imagestring($im,2,$x_ori + $xlen + 5,$y-7,number_format($arry[$i]), $black);			   
	}
	
	imagejpeg($im,"gd.jpg",65);
	imagedestroy($im);
?>

The picture looks like follows:


To print the picture on the screen instead of saving it as a file:

<?PHP
    header('Content-type: image/jpeg');
	imagejpeg($im);
?>

Draw line:

<?PHP
	imageline($im,x1,y1,x2,y2,color);
?>

Draw dashed line:

<?PHP
	$style=array($white,$white,$gray,$white,$white); //dashed style
	imagesetstyle($im,$style);
	imageline($im,$x_ori+2,$y,$x_ori + $xlen -2,$y,IMG_COLOR_STYLED);
?>

Draw rectangle:

<?PHP
	imagerectangle($im,x1,y1,x2,y2,color);
?>

Draw text:

<?PHP
	imagestring($im,font,x,y,$string,color);  //draw horizontal text
	imagestringup($im,font,x,y,$string,color); //draw vertical text
?>

Draw circle and ellipse:

<?PHP
	imageellipse($im,x,y,width,height,color);
	imagefilledellipse($im,x,y,width,height,color);
?>

Draw polygon:

<?PHP
	$arr=array(15,10,5,20,30,15);
	imagepolygon($im,$arr,points,color);	
?>


Click here for complete list of GD functions.


:: PHP Tutorials Home ::
PHP String Functions
 • concatenation • echo
 • ereg • ereg_replace
 • explode • htmlspecialchars
 • preg_match • preg_replace
 • preg_split • print,sprintf
 • str_replace • strcmp
 • strpos • strrev
 • strrpos • strtr
 • substr • substr_replace
PHP Array Functions
 • array_diff • array_flip
 • array_intersect • array_key_exists
 • array_keys • array_merge
 • array_pop • array_push
 • array_rand • array_search
 • array_splice • array_unshift
 • array_values • asort & arsort
 • count • in_array
 • ksort • shuffle
 • sort
PHP Data Types
 • array • associative array
 • date & time • number
 • class, object • regular expression
 • string • variables
PHP Loop & Conditions
 • continue & break • for loop
 • foreach • if else
 • not equal • while
PHP File System Functions
 • copy • delete, unlink
 • dirname • download url
 • file_exists • is_file
 • mkdir • read file
 • scandir • write file
PHP Popular Topics
 • ajax • clone
 • comments • constants
 • cookie • database
 • defined • die
 • form validation • gd, draw images
 • global variables • header url
 • heredoc • mail
 • pass by reference • print
 • regular expression • sessions
 • threads • xml parse
PHP Math Functions
 • abs • cos
 • exp • floor & ceil
 • fmod • log
 • max & min • pow
 • round • sin
 • sqrt • tan
convert.idontcarewhatyouthink.net © 2021  Terms of Use | Home