8 PHP script skills (4)

Source: Internet
Author: User

Dynamic image creation

After installing some third-party function libraries, you can use PHP to create and process images based on your graphics processing skills. In fact, you don't need too much geometric knowledge. I always fail this course when I was in middle school, and I still use PHP to create images if I am not! You need to install the GD library before using basic image creation functions. If you want to use JPEG-related image creation functions, you also need to install the jpeg-6b. T1lib must also be installed when Type 1 is used in the image.

Here, you also need to make further adjustments to your system. First, you must install t1lib to provide image processing support, and then install jpeg-6b. Step 3: Install the GD function library. You have to do these three jobs in order because you need to compile the GD library to use the jpeg-6b library, if the jpeg-6b step is first installed, the compilation will go wrong, at that time, you will be too busy to turn around.

After installing the above three function libraries, You need to reconfigure PHP. This is what you do when installing the DSO version of PHP! Run the make clean command and add the following code to the current configuration indicator:

-- With-gd = [/path/to/gd]
-- With-jpeg-dir = [/path/to/jpeg-6b]
-- With-t1lib = [/path/to/t1lib]

Finally, execute the make and make install commands in sequence to complete the preparation task. Restart Apache to run the new function of phpinfo () function check.

It is related to the GD library you installed. You may or may not be able to create GIF or PNG images. Key: If you have installed a gd-1.6 or an earlier version, you can handle GIF but not PNG. If you have installed a gd-1.6 or a later version, you can process PNG but not GIF.

Several functions are required to create a simple image. I will take you through the following steps:

Output a file header that contains the MIME type of the image you created. In our example, PNG is used.

<? Header ("Content-type: image/png ");

Use ImageCreate () to create a variable to store the blank image. This function requires an image size in pixels. The format is ImageCreate (x_size, y_size). The usage of 250-X-250 pixels is as follows:

$ NewImg = ImageCreate (250,250 );

Because your image is still blank, you still need to try to fill it with some colors, but first you need to assign names for each color based on the RGB value of the color, which requires ImageColorAllocate () function. The function format is ImageColorAllocate ([image], [red], [green], [blue]). The specific code is as follows:

$ Skyblue = ImageColorAllocate ($ newImg, 136,193,255 );

Then, you need to call the ImageFill () function to fill the image with the above colors. ImageFill (). The function has several versions, such as ImageFillRectangle () and ImageFillPolygon. For simplicity, we use the ImageFill () function for color filling. The format is as follows:

ImageFill ([image], [start x point], [start y point], [color])
ImageFill ($ newImg, 0, 0, $ skyblue );

Finally, you create an image and destroy the image stream to release the memory:

ImagePNG ($ newImg );
ImageDestroy ($ newImg);?>

The specific code looks like the following:

<? 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 call this script skyblue. php and access it with your browser, you will see a 250-X-250 pixel large blue PNG image.

You can also use the image creation function to process the image, such as creating a thumbnail for a large image.

Suppose you want to create a 35-x 35 pixel thumbnail for an image. You need to create a new 35X35 pixel image, create an image stream containing its original image content, and then change the size of the original image, and put it in the new blank image.

The key function used to achieve the preceding purpose is ImageCopyResized (). The function format is 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]);

The following is a code comment.

<? /* Send a header so that the browser knows the content-type of the file */
Header ("Content-type: image/png ");

/* Set up variables to hold the height and width of your new image */
$ NewWidth = 35;
$ NewHeight = 35;

/* Create a blank, new image of the given new height and width */
$ NewImg = ImageCreate ($ newWidth, $ newHeight );

/* Get the data from the original, large image */
$ OrigImg = ImageCreateFromPNG ("test.png ");

/* Copy the resized image. Use the ImageSX () and ImageSY functions to get the x and y sizes of the orginal image .*/
ImageCopyResized ($ newImg, $ origImg, 0, 0, 0, $ newWidth, $ newHeight, ImageSX ($ origImg), ImageSY ($ origImg ));

/* Create final image and free up the memory */
ImagePNG ($ newImg );
ImageDestroy ($ newImg);?>

If you call the above script resized. php and access it with your browser, you should be able to see a thumbnail PNG Image of 35-35 pixels.


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.