Creation of PHP dynamic images

Source: Internet
Author: User
As long as you install some third-party library files and have a certain degree of 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 first. In the basic application, you only need to install some third-party library files and have certain 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 first.

Before using the basic image creation function, install the GD library file. If you want to apply JPEG-related image creation function, you also need to install jpeg-6b, if you want to apply Type 1 fonts in the image, you must install t1lib.

Before creating an image creation environment, some preparation work is required. First install t1lib, then install the jpeg-6b, and then install the GD library file. The installation must be in the order given here, because the jpeg-6b will be used when the GD is compiled into the 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 the DSO method. 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, execute the make command, execute 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 will decide whether you can create GIF or PNG format graphics files. If the installation is a gd-1.6 or a previous version, you can apply the GIF pattern file but can not create a PNG pattern, if the installation is a version of the gd-1.6, you can create PNG files but not GIF files.

Creating a simple image also requires a lot of functions. we will clarify them step by step.

In the following example, we will create an image file with a PNG pattern. 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 the vacant Image. this function requests the parameter of the image size in pixels. the pattern is ImageCreate (x_size, y_size ). If you want to create an image of 250x250, you can apply the following statement:

$ NewImg = ImageCreate (250,250 );

Because the image is still blank, you may expect to fill it with some color. You need to first apply the ImageColorAllocate () function to specify a name for this color with its RGB value. The pattern of this function is ImageColorAllocate ([image], [red], [green], [blue]). If you want to define the sky blue, you can apply the following statement:

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

Next, you need to apply the ImageFill () function to fill the image with this color. the ImageFill () function has several versions, such as ImageFillRectangle () and ImageFillPolygon. For the sake of simplicity, we use the following pattern to apply the ImageFill () function:

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 displayed:

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

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.