Explore PHP dynamic image creation techniques. PHP dynamic image creation is quite easy. Next, I will introduce how to implement it in detail. Install the GD library file before using basic image creation functions. If you want
PHP dynamic image creation is quite easy. Next, I will introduce how to implement it in detail.
Install the GD library file before using basic image creation functions. If you want to create a function using JPEG-related images, you also need to install the jpeg-6b, and if you want to use the Type 1 font in the image, you must install t1lib.
Before creating an image creation environment, make some preparations. First install t1lib, then install the jpeg-6b, and then install the GD library file. Be sure to install in the order given here, because the jpeg-6b will be used when compiling the GD library, if the jpeg-6b is not installed, errors will occur during compilation.
After installing these three components, you need to reconfigure PHP again. this is also one of the places you are glad to install PHP using DSO. Run make clean and add the following content to the configuration in the current PHP dynamic image creation:
-- With-gd = [/path/to/gd]
-- With-jpeg-dir = [/path/to/jpeg-6b]
-- With-t1lib = [/path/to/t1lib]
After adding the file, run the make command, run the make install command, restart Apache, and run phpinfo () to check whether the new settings have taken effect. Now we can start image creation.
Based on the installed GD library file version, you can determine whether you can create a GIF or PNG image file. If you are installing a gd-1.6 or a previous version, you can use a file in GIF format but cannot create a PNG format if you are installing a version later than the gd-1.6, you can create PNG files but not GIF files.
Many functions are required to create a simple image.
In the following PHP dynamic image creation example, we will create a PNG image file. the following code is a header containing the MIME type of the created image:
Use ImageCreate () to create a variable that represents a blank image. this function requires a parameter of the image size in pixels. the format is ImageCreate (x_size, y_size ). To create an image of 250x250 in size, use the following statement:
$ NewImg = ImageCreate (250,250 );
Because the image is still blank, you may want to fill it with some color. You must 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]). To define the sky blue, 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. the ImageFill () function has several versions, such as ImageFillRectangle () and ImageFillPolygon. For simplicity, we use the ImageFill () function in 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 occupied memory are released:
- ImagePNG($newImg);
- ImageDestroy($newImg); ?>
In this way, all the code for creating PHP dynamic images is as follows:
- $newImg = ImageCreate(250,250);
- $skyblue = ImageColorAllocate
($newImg,136,193,255);
- ImageFill($newImg,0,0,$skyblue);
- ImagePNG($newImg);
- ImageDestroy($newImg);
- ?>
If you save the script file as skyblue. php and use a browser to access it, we will see a PNG image of the sky blue 250x250 format.
We can also use the image creation function to process the image. for example, we can make a large image into a small image:
Suppose you have an image and want to crop a 35x35 image. All you need to do is create a 35 × 35 blank image and create an image stream containing the original image, then, place an adjusted original image to the new blank image.
The key function to complete this task is ImageCopyResized (), which requires the following format:
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]).
- header('Content-type: image/png');
- $newWidth = 35;
- $newHeight = 35;
- $newImg = ImageCreate($newWidth,$newHeight);
- $origImg = ImageCreateFromPNG('test.png');
- ImageCopyResized($newImg,$origImg,0,
0,0,0,$newWidth,$newHeight,ImageSX
($origImg),ImageSY($origImg));
- ImagePNG($newImg);
- ImageDestroy($newImg); ?>
If you save this short script as resized. php, and then access it with a browser, you will see a 35 × 35 size PNG format image, so far PHP dynamic image is created.
Bytes. Next, I will introduce how to implement it in detail. Install the GD library file before using basic image creation functions. If you want to make...