Create and process images in PHP

Source: Internet
Author: User
Tags format end final functions header php and version variable
Create

After installing some third-party libraries, you can quickly use PHP to create and process images with graphics processing skills. In fact, you don't need a lot of geometry--because I flunked this course in high school and now I can use PHP to create images!

You need to install the GD library before you use the basic image creation function. To use JPEG-related image creation functions, you also need to install jpeg-6b. You must also install T1lib when using Type 1 fonts in the image. Asdf

Here you will also need to make further adjustments to your system settings. First install T1lib and end, then jpeg-6b. The third step is to install the GD function library. Ensure that the above three parts are installed sequentially because you need to compile the GD library to use the JPEG-6B library. If you install jpeg-6b first, there will be an error in compiling, which can overwhelm you for a while.

After the three function libraries, reconfigure PHP. This is a typical way to easily install the DSO version of PHP. Then execute make clean, command, and add the following code to the current configuration prompt:

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

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

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

Finally execute make, make install complete configuration. Restart Apache, and run the Phpinfo () function to check that the new functionality is working properly, and then you can start.

Depending on the version of the GD library installed, you may have the ability to create GIF or PNG images. The point is that if you have a gd-1.6 or an earlier version installed, you can process the GIF file, but not the PNG file, and if you install gd-1.6 or later, you can handle the PNG file but not the GIF file.

Creating a simple image requires several functions. I will follow the steps to demonstrate the following.

The output contains the file header for the MIME type of the image you created, in this case PNG.

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

Use Imagecreate () to create a variable that holds a blank image. The function requires a picture pixel size. The format is imagecreate (X_size, y_size), a picture of 250*250 pixels, as follows:

$NEWIMG = Imagecreate (250,250);

Because your image is still blank at this time, you need to fill it with some color. But first you need to use the imagecolorallocate () function to determine the name for each color according to the RGB value of the color. The format of the function is imagecolorallocate ([image], [red], [green], [blue]). In the case of Azure, use:

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

Next, use the Imagefill () function to fill the image with the above color. In fact, the Imagefill () function has multiple versions, such as Imagefillrectangle (), Imagefillpolygon (), and so on. For simplicity, use the Imagefill () function for color padding, in the following format:

Imagefill ([Image], [Start x Point], [Start y point], [color])

Imagefill ($NEWIMG, 0,0, $skyblue);

Finally, create the final image and destroy the image stream to free the memory and defragment the system at the end:

Imagepng ($NEWIMG);

Imagedestroy ($NEWIMG);

Your code should look 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 invoke the script skyblue.php and access it with your own browser, you will see a 250*250 pixel blue png image.

You can also use image creation functions to process images, such as thumbnails created for large images.

Suppose you're going to make a thumbnail of a 35*35 pixel size for a picture. What you want to do is create a new 35*35 pixel size image, produce an image stream containing the original image content, and then change the size of the original image and place it in a new, blank image.

The key function to achieve this is imagecopyresized (), and the format is as follows: 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]);

Note below the code:

? /* Send file header to make the browser aware of the file type * *

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

/* Set Variable save new picture long width * *

$newWidth = 35;

$newHeight = 35;

/* Create a blank new picture with a given length and width.

$NEWIMG = Imagecreate ($newWidth, $newHeight);

/* Obtain data from the original large image/*

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

/* Copy the picture after changing the size. Use function Imagesx () and Imagesy to get the x and x dimensions of the original picture.

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

/* Create the final picture and empty the memory * *

Imagepng ($NEWIMG);

Imagedestroy ($NEWIMG);?>

If you invoke the script resized.php and use the browser to access the file, you can see a thumbnail png picture of 35*35 pixels.



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.