PHP coding implementation process dynamic image creation

Source: Internet
Author: User

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.CodeIs 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.

CCID-China Computer news

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.