Use PHP to create dynamic images _ PHP Tutorial

Source: Internet
Author: User
Use PHP to create dynamic images. As long as you install some third-party library files and have some geometric knowledge, you can use PHP to create and process images. Using PHP to create dynamic images is quite easy. As long as you install some third-party library files and have some geometric knowledge, you can use PHP to create and process images. Using PHP to create dynamic images 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 current configuration:

-- 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 example, we will create an image file in PNG format. the code below is a header containing the MIME type of the created image:

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

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);?>

The code for creating an image is 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 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]).
<? /* Send a header to let the browser know the content type contained in the file */
Header ("Content-type: image/png ");
/* Create a variable to save the height and width of the new image */
$ NewWidth = 35;
$ NewHeight = 35;
/* Create a new blank image with a given height and width */
$ NewImg = ImageCreate ($ newWidth, $ newHeight );
/* Obtain data from the original large image */
$ OrigImg = ImageCreateFromPNG ("test.png ");
/* Copy the adjusted image and use ImageSX () and ImageSY () to obtain the size of the original image in terms of X and Y */
ImageCopyResized ($ newImg, $ origImg, 0, 0, 0, $ newWidth, $ newHeight, ImageSX ($ origImg), ImageSY ($ origImg ));
/* Create the desired image and release the memory */
ImagePNG ($ newImg );
ImageDestroy ($ newImg);?>

If you save this short script as resized. php and access it using a browser, you will see a 35 × 35 PNG image.

Bytes. Using PHP to create dynamic images is quite easy. ...

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.