Installation see http://www.php.net/manual/en/ref.image.php. Astahost hosting service has enable gd library, so won’t need any extra works except in coding.
Lets get started, creating images from php
set the canvas:
CODE
$img = imagecreate(250, 80)
imagecreatethis function create and set a blank canvas with the wide of 250 pixels and height of 80 pixels.
setting the basic colors:
CODE
$black = imagecolorallocate($img, 0, 0, 0)
$white = imagecolorallocate($img, 255, 255, 255)
$red = imagecolorallocate($img, 255, 0, 0)
$green = imagecolorallocate($img, 0, 0, 255)
$white = imagecolorallocate($img, 255, 255, 255)
$red = imagecolorallocate($img, 255, 0, 0)
$green = imagecolorallocate($img, 0, 0, 255)
imagecolorallocate $img represent the canvas. the next three value is color value in rgb(0-255), you also set the color in hex (0x00 – 0xff).
drawing a something:
CODE
imagerectangle($img, 10, 10, 240, 70, $white)
imagerectangle this function create a rectangle a width of 230[240(x2) – 10(x1)] and a height of 60[70(y2) – 10(y1)]. $img the canvas, the first two value sets the starting point(x1, y1) . and last two number is the ending(x2, y2). And the last value is the color.
CODE
imagefilledrectangle($img, 20, 20, 60, 60, $red)
imagefilledrectangle this function create a filled rectangle with a color red. All values is the same as the imagerectangle function.
CODE
imagefilledellipse($img, 90, 40, 40, 40, $blue)
imagefilledellipse this one creates an ellipse(circular objects). $img is the canvas, the first two value(cx, cy) set the origin(center) of the ellipse. The third value set the width(horizontal width) and fifth one is the height(vertical width). Last but not the least the color value.
CODE
$corners = array(
0 => 190,
1 => 60,
2 => 210,
3 => 20,
4 => 230,
5 => 60,
);
imagefilledpolygon($img, $corners, 3, $white);
0 => 190,
1 => 60,
2 => 210,
3 => 20,
4 => 230,
5 => 60,
);
imagefilledpolygon($img, $corners, 3, $white);
imagefilledellipse and the last drawing function I discuss is a polygon function, in the example there’s only 3 corners. The first value is the canvas, the second one is the corners in array, the value 3 is the number of vertices, and the last one is color.
Showing the graphics:
CODE
header("Content-type: image/jpeg")
imagejpeg($img)
imagejpeg($img)
header imagejpeg To output the image you must first send the appropriate header, in this example I use jpg extension and the content type is set to “image/jpeg”. And call the imagejpeg function to create the images and shown up to the browser. For other file type such as png(image/png), gif(image/gif), windows bitmap(image/vnd.wap.wbmp), check php manual for details.
And finally, use the imagedestroy function just to clear up the memory used by the imagecreate functions.
Final code should look like this.
CODE
<?php
$img = imagecreate(250,80);
$black = imagecolorallocate($img, 0, 0, 0);
$white = imagecolorallocate($img, 255, 255, 255);
$red = imagecolorallocate($img, 255, 0, 0);
$green = imagecolorallocate($img, 0, 255, 0);
$blue = imagecolorallocate($img, 0, 0, 255);
$corners = array(
0 => 190,
1 => 60,
2 => 210,
3 => 20,
4 => 230,
5 => 60,
);
imagerectangle($img, 10, 10, 240, 70, $white);
imagefilledrectangle($img, 20, 20, 60, 60, $red);
imagefilledellipse($img, 90, 40, 40, 40, $blue);
imagefilledellipse($img, 150, 40, 70, 40, $green);
imagefilledpolygon($img, $corners, 3, $white);
header ("Content-type: image/jpeg");
imagejpeg($img);
imagedestroy($img);
?>
$img = imagecreate(250,80);
$black = imagecolorallocate($img, 0, 0, 0);
$white = imagecolorallocate($img, 255, 255, 255);
$red = imagecolorallocate($img, 255, 0, 0);
$green = imagecolorallocate($img, 0, 255, 0);
$blue = imagecolorallocate($img, 0, 0, 255);
$corners = array(
0 => 190,
1 => 60,
2 => 210,
3 => 20,
4 => 230,
5 => 60,
);
imagerectangle($img, 10, 10, 240, 70, $white);
imagefilledrectangle($img, 20, 20, 60, 60, $red);
imagefilledellipse($img, 90, 40, 40, 40, $blue);
imagefilledellipse($img, 150, 40, 70, 40, $green);
imagefilledpolygon($img, $corners, 3, $white);
header ("Content-type: image/jpeg");
imagejpeg($img);
imagedestroy($img);
?>
enjoy


