Drawing with GD Library

After 'Introduction to GD Library', We can start to know functions to draw line, rectangles, circles, arcs and strings.
To draw rectangle there are 2 functions:
- imagerectangle() creates a rectangle starting at the specified coordinates.
- imagefilledrectangle() creates a rectangle filled with color in the given image.

First example:

Code:

<?php
$img = imagecreatetruecolor(200, 200);
$color = imagecolorallocate($img, 21,154,224);
imagerectangle($img, 10, 30, 190, 190, $color);
header("Content-type: image/png");
imagepng($img);
imagedestroy($img);
?>

Result:


Second example (filled):

Code:

<?php
$img = imagecreatetruecolor(200, 200);
$color = imagecolorallocate($img, 21,154,224);
imagefilledrectangle($img, 10, 30, 190, 190, $color);
header("Content-type: image/png");
imagepng($img);
imagedestroy($img);
?>

Result:


Draw a line:
Example:

Code:

<?php
$img = imagecreatetruecolor(200, 200);
$color = imagecolorallocate($img, 255,255,255);
imageline($img, 10, 30, 190, 190, $color);
header("Content-type: image/png");
imagepng($img);
imagedestroy($img);
?>

Result:

imageline() draws a line between the two given points.

Draw a circle:
Example:

Code:

<?php
$img = imagecreatetruecolor(200, 200);
$color = imagecolorallocate($img, 255,0,0);
imagearc($img, 100, 100, 80, 80, 0, 360, $color);
header("Content-type: image/png");
imagepng($img);
imagedestroy($img);
?>

(Replace imagearc with imagefilledarc for filled circle)

Result:

imagearc() draws an arc of circle centered at the given coordinates.

Draw an arc:
Example:

Code:

<?php
$img = imagecreatetruecolor(200, 200);
$color = imagecolorallocate($img, 0,255,0);
imagefilledarc($img, 100, 100, 80, 80, 0, 60, $color, IMG_ARC_PIE);
header("Content-type: image/png");
imagepng($img);
imagedestroy($img);
?>

(Replace imagefilledarc with imagearc for unfilled arc)

Result:

Draws a partial filled ellipse centered at the specified coordinate in the given image.

Draw a string:
Example:

Code:

<?php
$img = imagecreatetruecolor(200, 200);
$color = imagecolorallocate($img, 0,0,255);
imagestring($img, 3, 10, 10, "Hello world!", $color);
header("Content-type: image/png");
imagepng($img);
imagedestroy($img);
?>

Result:

imagestring() draws a string at the given coordinates.

Fusion of all codes:
Example:

Code:

<?php
$img = imagecreatetruecolor(200, 200);
$color = imagecolorallocate($img, 21,154,224);
imagefilledrectangle($img, 10, 30, 190, 190, $color);
$color = imagecolorallocate($img, 255,255,255);
imageline($img, 10, 30, 190, 190, $color);
$color = imagecolorallocate($img, 255,0,0);
imagearc($img, 100, 100, 80, 80, 0, 360, $color);
$color = imagecolorallocate($img, 0, 255, 0);
imagefilledarc($img, 100, 100, 80, 80, 0, 60, $color, IMG_ARC_PIE);
$color = imagecolorallocate($img, 0,0,255);
imagestring($img, 5, 10, 10, "Hello world!", $color);
header("Content-type: image/png");
imagepng($img);
imagedestroy($img);
?>

Result:


For more information visit http://www.php.net/gd