8 Tips for PHP scripts (4) Create images dynamically

Source: Internet
Author: User
Tags format final functions header php and requires versions
Create | dynamic | tips | Script After installing some third-party libraries, with your graphics processing skills, you can create and process images in PHP. In fact, you don't need too much geometrical knowledge. When I was in high school, this course always failed, and now I will use PHP to create images! You need to install the GD library before using the basic image creation function. If you want to use a JPEG-related image creation function You will also need to install jpeg-6b. You must also install T1lib when using Type 1 fonts in the image.





here, you also need to make further adjustments to your system. First, you must install T1lib to provide image processing support, and then install JPEG-6B. The third step is to install the GD function library. You have to finish the three work in order, because you need to compile the GD library to use the JPEG-6B library, if the jpeg-6b step first installation, the compiler will be wrong, then you are busy around the round and there is no way.





after installing the above three function libraries, you will have to reconfigure PHP. This is what you do when you install the DSO version of PHP. Then execute make clean, command, and then add the following code to the current configuration Directive character:





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


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


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





execute the make, make install command to complete the compounding task in the final order. Restart Apache and run the Phpinfo () function to check whether the new feature is working properly.





is related to your installed GD library, you might or may not have the ability to create GIF or PNG images. The point is: if you have a gd-1.6 or an earlier version installed, you can handle GIF but not PNG. If gd-1.6 or later versions are installed, you can handle PNG but not GIF.





creating a simple image requires several functions. I will follow the steps to take you to learn this process:





output A file header that contains the MIME type of the image you created, in our case PNG.





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





uses Imagecreate () to create a variable that holds a blank image. The function requires an image size in pixels. The format is imagecreate (X_size, Y_size), and for 250-x-250 pixel images, the usage is as follows:





$newImg = imagecreate (250,250);





because your image is still blank, so you have to try to fill it with some color, but first you need to follow the color of the RGB value for each color to assign a name, this use of the imagecolorallocate () function. The format of the function is imagecolorallocate ([image], [red], [green], [blue]). If it is sky-blue, the specific code is as follows:





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





then you need to call the Imagefill () function to populate the image with the color above. Imagefill (), there are several versions of the function, such as Imagefillrectangle (), Imagefillpolygon (), and so on. For simplicity, we use the Imagefill () function for color filling, in the following format:





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


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





Finally, you create the image and destroy the image stream to free up memory:





imagepng ($NEWIMG);


Imagedestroy ($NEWIMG);?>





specific code looks like the following:





;? 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 call this script skyblue.php and use your own browser to access it, you'll see a 250-x-250 pixel-large blue PNG image.





You can also use image creation functions to process images, such as creating thumbnails of large images, and so on.





Suppose you plan to make a thumbnail of 35-x-35 pixel size for a picture. You do this by creating a new X 35 pixel size image, creating an image stream containing its original image content, and then changing the size of the original image and putting it in a new, blank image.




The key function that
uses to achieve this is imagecopyresized (), which has 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], [Origin Al image Y]);





The following are code comments.





? * Send a header so that the browser knows the Content-type of the file * * * *


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





/* Set up variables to hold the height and width of your new image */


$newWidth = 35;


$newHeight = 35;





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


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





/* Get the data from the original, large image */


$origImg = imagecreatefrompng ("Test.png");





/* Copy the resized image. Use the Imagesx () and Imagesy functions to get the X and y sizes of the orginal image. */


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





/* Create final image and free up the memory * *


imagepng ($NEWIMG);


Imagedestroy ($NEWIMG);?>





If you call the above script resized.php and use your own browser to access it, you should be able to see a 35-x-35 pixel size thumbnail png.











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.