As long as you install some third-party library files and have some geometric knowledge, you can use PHP to create and process images. Creating dynamic images with PHP is a pretty easy thing to do. Below, the author will introduce in detail how to implement it.
Before using basic image creation functions, you need to install the GD library file. If you want to use JPEG-related image creation functions, you also need to install jpeg-6b. If you want to use Type 1 fonts in your image, you must install t1lib.
Before creating an image creation environment, some preparations need to be done. First, install t1lib and then install jpeg-6b, and then install the GD library file. During installation, you must install it in the order given here, because jpeg-6b will be used when compiling GD into the library. If jpeg-6b is not installed, an error will occur during compilation.
After installing these three components, you need to reconfigure PHP, which is one of the things you are grateful for using DSO to install PHP. Run make clean, and then add the following content to the current configuration:
--with-gd=[/path/to/gd]
--with-jpeg-dir=[/path/to/jpeg-6b]
--with-t1lib=[/path/to/t1lib]
After adding, execute the make command, then execute the make install command, restart Apache and run phpinfo() to check whether the new settings are effective. Now we can start the image creation work.
Depending on the version of the installed GD library file, it will determine whether you can create a GIF or PNG format graphic file. If you are installing gd-1.6 or previous versions, you can use GIF format files but cannot create PNG format. If you are installing gd-1.6 or later versions, you can create PNG files but cannot create GIF format files.
Creating a simple image also requires many functions, which we will explain step by step.
In the following example, we will create a PNG image file, and the following code is a header of the MIME type of the created image:
<? header ("Content-type: image/png");
Use ImageCreate() to create a variable representing a blank image. This function requires parameters of the image size in pixels, and its format is ImageCreate(x_size, y_size). If you want to create an image of size 250×250, you can use the following statement:
$newImg = ImageCreate(250,250);
Since the image is still blank, you may want to fill it with some color. You need to first use the ImageColorAllocate() function to specify a name for this color with its RGB value. The format of this function is ImageColorAllocate([image], [red], [green], [blue]). If you want to define sky blue, you can use the following statement:
$skyblue = ImageColorAllocate($newImg,136,193,255);
Next, you need to use the ImageFill() function to fill the image with this color. There are several versions of the ImageFill() function, such as ImageFillRectangle(), ImageFillPolygon(), etc. For simplicity, we use the ImageFill() function through the following format:
ImageFill([image], [start x point], [start y point], [color])
ImageFill($newImg,0,0,$skyblue);
Finally, after the image is created, the image handle and the memory occupied are released:
ImagePNG($newImg);
ImageDestroy($newImg); ?>
In this way, all the codes for creating the image are as follows:
<? header ("Content-type: image/png");
$newImg = ImageCreate(250,250);
$skyblue = ImageColorAllocate($newImg,136,193,255);
ImageFill($newImg,0,0,$skyblue);
ImagePNG($newImg);
ImageDestroy($newImg);
?>
If you save this script file as and access it with a browser, we will see an azure 250×250 PNG format image.
We can also use image creation functions to process images, such as making a larger image into a small image:
Suppose you have an image that you want to crop out of it with a size of 35×35. All you need to do is create a 35×35-size blank image, create an image stream containing the original image, and then place the resized original image into the new blank image.
The key function to accomplish this task is ImageCopyResized(), which requires the format as follows:
ImageCopyResized([new image handle],[original image handle],[new image X], [new Image Y], [original image X], [original image Y], [new image X], [new image Y], [original image X], [original image Y])。
<?/* Send a header so that the browser knows the type of content contained in the file*/
header("Content-type: image/png");
/* Create variables that save the height and width of the new image*/
$newWidth = 35;
$newHeight = 35;
/* Create a new blank image of a given height and width*/
$newImg = ImageCreate($newWidth,$newHeight);
/* Get data from the original larger image*/
$origImg = ImageCreateFromPNG("");
/*Copy the resized image, use ImageSX() and ImageSY() to get the original image in X and Y size */
ImageCopyResized($newImg,$origImg,0,0,0,0,$newWidth,$newHeight,ImageSX($origImg),ImageSY($origImg));
/*Create the image you want to get and free up memory */
ImagePNG($newImg);
ImageDestroy($newImg); ?>
If you save this small script as and access it with a browser, you will see an image in PNG format of 35×35 size.