PHP image processing

Source: Internet
Author: User
PHP image processing PHP provides some functions for image editing. The most typical applications are random image verification codes, image watermarks, and the generation of pie charts and column charts in data statistics.

Some graphical functions in PHP can be used directly, but most of them need to be used after the GD2 function library is installed. Installing the GD2 Library on Windows is very easy, that is, the php_gd2_dll file in the ext directory that comes with PHP5.

If no, the GD2 library is not installed during PHP installation. open php. ini file, view php. ini file (c: \ winsows), check whether there is a sentence in the file "; extension = php_gd2.dll" (in Paths and Directories). If yes, remove, set the extended directory extension_dir value in the file to the complete path of the PHP ext Directory, for example, "extension_dir = E: \ php5 \ ext". Save the file and restart Apache.

Creating a chart generally involves four steps: 1) creating a background, 2) drawing a graph or input text in the background, 3) outputting a graph, 4) releasing all resources

Before that, change the default off of output_buffering under Language Options in the php. ini file to on, and then Restart Apache (Apache Restart). Otherwise, the following Warning occurs:

Cannot modify header information-headers already sent .....

For example, to draw a PNG image, such as a straight line

$ Image = imagecreate (400,400); // create an image. Two parameters indicate the width and height of the image, in pixels, and return the data stream of this image $ background_color = imagecolorallocate ($ image, 255,255,255); // sets the white background stream for the image, $ black = imagecolorallocate ($ image, 0); // sets the black background stream imageline ($ image, 100,100, $ black) for the image; // draws a straight line, and set the color to black ob_clean (); // clear the output; otherwise, the image cannot display the header ("Content-type: image/png"); // send the header information to the browser, output png image imagepng ($ image); // output image imagedestroy ($ image); // clear resources

The image processing function can directly create an image stream to draw the image. It can also read the existing image as an image stream and then modify the image, such as adding the watermark effect. This function is often used to prevent image theft.

For example, add a watermark URL to the original image (in jpeg format ).

    ob_clean();    header("Content-type:image/jpeg");    $im = imagecreatefromjpeg("dog.jpg");    $red = imagecolorallocate($im,255,0,0);    imagestring($im,5,6,10,"http://www.cnblogs.com/520xiuge/",$red);    imagejpeg($im);    imagedestroy($im);

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.