PHP to create a dynamic image

Source: Internet
Author: User
Keywords Network programming PHP tutorial
Tags basic browser code content create creating creating a environment

Just install some third-party library files and have a certain degree of geometric knowledge, you can use PHP to create and process images. Creating a dynamic image with PHP is fairly easy. Below, I will detail how to achieve.

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

Before setting up the image creation environment, there is still some work to be done. First, install t1lib then install jpeg-6b, and then install the GD library file. Installation must be installed in accordance with the order given here, because the compiler GD storage will use jpeg-6b, if not installed jpeg-6b, at compile time will make an error.

After installing these three components, you also need to reconfigure PHP once, which is one of the places where you feel lucky about installing PHP with DSO. Run make clean, then add the following to the current configuration:

--with-gd = [/ path / to / gd]

--with-jpeg-dir = [/ path / to / jpeg-6b]

--with-t1lib = [/ path / to / t1lib]

After the completion of the implementation of the implementation of the make command, and then run the make install command, restart the Apache run phpinfo () to check whether the new settings take effect. Now we can start the image creation.


Depending on the version of the GD library installed, it will determine if you can create graphics files in GIF or PNG format. If you are installing gd-1.6 or earlier, you can use GIF format files but not PNG format. If you are installing gd-1.6 or later, you can create PNG files but can not create GIF format files.

Create a simple image also need to use many functions, we will explain step by step.

In the following example, we will create a PNG format image file, the following code is a header that contains the MIME type of the created image:

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

Use ImageCreate () to create a variable representing a blank image, which requires an image-size parameter in pixels of the form ImageCreate (x_size, y_size). If you want to create an image of size 250 × 250, you can use the following statement:

$ newImg = ImageCreate (250,250);

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

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

Next, you need to fill the image with this color using the ImageFill () function. There are several versions of the ImageFill () function, such as ImageFillRectangle (), ImageFillPolygon (), and so on. 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, release the image handle and memory after the image is created:

ImagePNG ($ newImg);

ImageDestroy ($ newImg);?>

So, the entire code to create the image looks like this:

php code:
<? 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 this script file as skyblue.php and access it with a browser, we will see a sky blue 250 × 250 PNG format image.

We can also use the image creation function to process the image, for example, make a large image into a small image:

Suppose you have an image from which you want to crop a 35 × 35-size image. All you need to do is create a 35 × 35-size blank image, create a stream of images that contains the original image, and place a resized original image in a new blank image.

The key function to accomplish 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]).

php code:
<? / * Send a header to let the browser know the type of content contained in the file * /

header ("Content-type: image / png");

/ * Create a new image to save the height and width of the variable * /

$ newWidth = 35;

$ newHeight = 35;

/ * Create a new blank image of a given height and width * /

$ newImg = ImageCreate ($ newWidth, $ newHeight);

/ * Get the data from the original larger image * /

$ origImg = ImageCreateFromPNG ("test.png");

/ * Copy the resized image using ImageSX (), ImageSY () to get the size of the original image in X, Y * /

ImageCopyResized ($ newImg, $ origImg, 0,0,0,0, $ newWidth, $ newHeight, ImageSX ($ origImg), ImageSY ($ origImg));

/ * Create the desired image, release the memory * /

ImagePNG ($ newImg);

ImageDestroy ($ newImg);?>


If you save this short script as resized.php and then access it with your browser, you will see a PNG 35x35 image

Related Article

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.