_php Foundation for creating dynamic image of PHP editing and implementation

Source: Internet
Author: User
Tags install php php and

As long as you install some Third-party library files and have some knowledge of geometry, you can use PHP to create and process images. Using PHP to create dynamic images is a fairly easy thing to do. Below, I will explain in detail how to implement.

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

Before you create an image creation environment, you need to do some preparation work. First, install t1lib then install jpeg-6b, then install the GD library file. Be sure to install it in the order given here, because the jpeg-6b is used when compiling the GD storage, and if the jpeg-6b is not installed, there will be an error at compile time.

After installing these three components, you will also need to reconfigure PHP once, which is one of the places where you are lucky to install PHP using the DSO method. Run make clean, and then add the following in the current configuration:

--WITH-GD=[/PATH/TO/GD]

--WITH-JPEG-DIR=[/PATH/TO/JPEG-6B]

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

Execute the Make command after the addition, and then execute the made install command, and then run Phpinfo () after restarting Apache to check to see if the new settings are in effect. Now we're ready to start the image creation process.

Depending on the version of the GD library file installed, you will be able to create graphics files in GIF or PNG format. If you are installing a gd-1.6 or a previous version, you can use a file in GIF format but you cannot create a PNG format, and if you are installing a later version of gd-1.6, you can create a PNG file but you cannot create a file in GIF format.

Creating a simple image also requires a number of functions, which we will explain step-by-step.

In the following example, we will create an image file in PNG format, which is a MIME type header containing the image created:

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

Use Imagecreate () to create a variable that represents a blank image, which requires an image size parameter in pixels, in the form of Imagecreate (X_size, y_size). If you want to create an image of size 250x250, you can use the following statement:

$NEWIMG = Imagecreate (250,250);

Because the image is still blank, you might want to fill it with some color. You need to first use the Imagecolorallocate () function to specify a name for this color with its RGB value, which is in the form of 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 use the Imagefill () function to populate the image with this color, and the Imagefill () function has several versions, such as Imagefillrectangle (), Imagefillpolygon (), and so on. For the sake of 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 the memory occupied after the image is established:

Imagepng ($NEWIMG);

Imagedestroy ($NEWIMG);

This way, all the code to create the image looks like this:

? 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 in a browser, we will see an image in the Azure 250x250 png format.

We can also use image creation functions to process images, such as making a larger image into a small image:

Suppose you have an image that you want to crop out a 35x35 size image from. All you need to do is create a blank image of the 35x35 size, create an image stream containing the original image, and then place a resized original image in a new, blank image.

The key function to complete this task is imagecopyresized (), which requires a format that looks like this: 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 so that the browser knows what type of content the file contains.

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

* * from the original larger image to get the data * *

$ORIGIMG = Imagecreatefrompng ("Test.png");

/* Copy resized image, use Imagesx (), Imagesy () to get the original image in the X, y size

Imagecopyresized ($NEWIMG, $ORIGIMG, 0,0,0,0, $newWidth, $newHeight, Imagesx ($ORIGIMG), Imagesy ($ORIGIMG));

/* Create desired image, free memory * *

Imagepng ($NEWIMG);

Imagedestroy ($NEWIMG);

If you save this little script as resized.php and then access it in a browser, you will see an image in the PNG format of 35x35 size.


Sadie Network-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.