Create Graphics
To create an image, the first thing we need to do is create a blank image
canvas. We use the ImageCreate() function to do this:
$image = ImageCreate(300,200);
In the above statement, 300 is width and 200 is height. The unit is pixel.
The next thing we need to know is what color we'll be drawing in. We do
this with the ImageColorAllocate() function::
$lightblue = ImageColorAllocate($image,
200,200,255);
$darkred = ImageColorAllocate($image,
200,0,0); The color system is RGB, ranging from 0 to 255. So "200,
0, 0" is dark red, and "200, 200, 255" is light blue. Now
we have three statements:
$image = ImageCreate(300,200);
$lightblue = ImageColorAllocate($image,200,200,255);
$darkred = ImageColorAllocate($image,200,0,0); By default, the first color will be the background color
of the canvas. So in the above sample, the background color of $image
will be light blue. We can use the nexr color $darkred to draw the shapes.
To draw a line, we use the ImageLine() function:
ImageLine($image, 10,10, 100,40, $darkred); The first two numbers "10, 10" are x and y coordinates
of the start of the line, and the next two numbers "100, 40"
defines the end point of the line.
To output this image to the browser, we use the following function:
ImageJPEG($image);
If we want to save the image to disk, we can specify a second argument,
giving a filename:
ImageJPEG($image, "image.jpg"); we can also specify a third argument, which must be an
integer value between –1 and 100, to specify the quality of the image.
ImageJPEG($image, "image.jpg", 50); After using the image, we should destroy the object and
release the memory:
ImageDestroy($image); To sending non-HTML data to the browser, we should let it
know what file type it is, We use the Header() function to handle this:
Header("Content-type: image/jpeg");
The following is the complete code block:
<?php
Header("Content-type: image/jpeg");
$image = ImageCreate(300,200);
$lightblue = ImageColorAllocate($image,200,200,255);
$darkred = ImageColorAllocate($image,200,0,0);
ImageLine($image,10,10,100,40,$darkred);
ImageJPEG($image);
ImageDestroy($image);
?> We can draw shaps on the image:
ImageArc($image,160,60,75,75,0,360,$darkred);
ImageRectangle($image,10,10,150,100,$darkred);
To use an image in our PHP code, we simply use:
$landscape = ImageCreateFromJPEG("landscape.jpg");
We can combine two images by using the ImageCopyResized() function:
<?php
Header("Content-type: image/jpeg");
$landscape = ImageCreateFromJPEG("landscape.jpg");
$girl = ImageCreateFromJPEG("girl.jpg");
$width = ImageSX($girl);
$height = ImageSY($girl);
ImageCopyResized($landscape,$girl,180,220,0,0,$width,$height,$width,$height);
ImageJPEG($landscape);
ImageDestroy($landscape);
?> There is function for transparent color -
ImageColorTransparent():
<?php
Header("Content-type: image/jpeg");
$landscape = ImageCreateFromJPEG("landscape.jpg");
$girl = ImageCreateFromJPEG("girl.jpg");
$trans = ImageColorAt($girl, 0, 0);
ImageColorTransparent($girl, $trans);
$width = ImageSX($girl);
$height = ImageSY($girl);
ImageCopyResized($landscape,$girl,180,220,0,0,$width,$height,$width,$height);
ImageJPEG($landscape);
ImageDestroy($landscape);
?> |