Create and process images in PHP

Source: Internet
Author: User


After installing some third-party function libraries, you can quickly use PHP to create and process images with graphic processing skills. In fact, you don't need a lot of geometric knowledge-because I used to fail this course when I was in middle school, but now I can use PHP to create images!
Install the GD library before using basic image creation functions. To create a function using JPEG-related images, you also need to install the jpeg-6b. T1lib must also be installed when type 1 is used in the image. ASDF
Here, you also need to make further adjustments to your system settings. First install t1lib and end, then the jpeg-6b. Step 3 install the GD function library. Make sure that the above three parts are installed in order because you need to compile the GD library before using the jpeg-6b library. If you install the jpeg-6b first, compilation will go wrong, which will make you overwhelmed for a while.
After the three function libraries, reconfigure PHP. This is a typical method for easily installing the DSO version of PHP. Run the make clean command and add the following to the current configuration prompt: Code :

-- With-Gd = [/path/to/gd]
-- With-JPEG-Dir = [/path/to/jpeg-6b]
-- with-t1lib = [/path/to/t1lib]
Finally, execute make and make install to complete the configuration. Restart Apache and run the phpinfo () function to check whether the new function runs properly. Then you can start.
depending on the installed GD library version, you may be able to create GIF or PNG images. The key is that if you have installed a gd-1.6 or an earlier version, you can process GIF files, but not PNG files; if you have installed a gd-1.6 or a later version, you can process PNG files but not GIF files.
several functions are required to create a simple image. I will demonstrate it as follows.
output the file header containing the MIME type of the image you created. In this example, It is PNG.
header ("Content-Type: image/PNG");
Use imagecreate () to create a variable to store blank images. This function requires an image pixel size. The format is imagecreate (x_size, y_size). For 250*250 pixels of images, see the following:
$ newimg = imagecreate (250,250 );
because your image is still blank, you need to fill it with some colors. However, you must first use the imagecolorallocate () function to determine the name of each color based on the RGB value of the color. The function format is imagecolorallocate ([Image], [Red], [Green], [Blue]). If the sky is blue, use:
$ skyblue = imagecolorallocate ($ newimg, 136,193,255);

Then, fill the above color with the imagefill () function. In fact, the imagefill () function has multiple versions, such as imagefillrectangle () and imagefillpolygon. For simplicity, use the imagefill () function for color filling. The format is as follows:
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 release the memory and organize the system after the end:

Imagepng ($ newimg );
Imagedestroy ($ newimg );
Your code should look like this:

$ newimg = imagecreate (250,250);
$ skyblue = imagecolorallocate ($ newimg, 136,193,255 );
imagefill ($ newimg, 0, 0, $ skyblue);
imagepng ($ newimg);
imagedestroy ($ newimg);
?>
if you call the script skyblue. php and use your browser to access it, you will see a 250*250 pixel blue PNG image.
You can also use the image creation function to process images, such as thumbnails for large images.
assume that you want to create a 35*35 pixel thumbnail for an image. All you need to do is create a new 35*35 pixel size image, and generate an image stream containing the original image content. Then, change the size of the original image, and place it in the new blank image.
the key function to achieve this goal is imagecopyresized (), in 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]);
note the following code:

header ("Content-Type: image/PNG ");
/* set the variable to save the length and width of the new image */
$ newwidth = 35;
$ newheight = 35;
/* Create a blank image with the given length and width */
$ newimg = imagecreate ($ newwidth, $ newheight );
/* obtain data from the original big image */
$ origimg = imagecreatefrompng ("test.png");
/* copy the picture after the size change. Use the imagesx () and imagesy functions to obtain the X and X dimensions of the original image */
imagecopyresized ($ newimg, $ origimg, $ newwidth, $ newheight, imagesx ($ origimg), imagesy ($ origimg);
/* Create the final image and clear the memory */
imagepng ($ newimg );
imagedestroy ($ newimg);?>
if you call the script resized. php and use a browser to access this file, you can see a thumbnail PNG Image 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.