16/7/11_PHP-GD Library Introduction

Source: Internet
Author: User
Tags imagejpeg

About GD Library

GD refers to the graphic device,php of the GD library is used to process graphics extension library, through the GD Library provides a series of APIs, you can process the image or directly generate new images.

In addition to the text processing, PHP, through the GD library, JPG, PNG, GIF, SWF and other images can be processed. GD library is commonly used in image watermarking, verification code generation and so on.

PHP is already integrated with the GD library by default and only needs to be opened when it is installed.

Header ("Content-type:image/png"); $img =imagecreatetruecolor (+), $red =imagecolorallocate ($img, 0xFF, 0x00, 0x00); Imagefill ($img, 0, 0, $red); Imagepng ($img); Imagedestroy ($img);
Draw lines

To manipulate a graphic, start by creating a new canvas, and you can create a blank picture of a true color with the Imagecreatetruecolor function:

$img = Imagecreatetruecolor (100, 100);

The color of the brush used in the GD library needs to be assigned through the Imagecolorallocate function, and the color value of the RGB is determined by the parameter setting:

$red = Imagecolorallocate ($img, 0xFF, 0x00, 0x00);

We then draw the line by calling the draw segment function Imageline, and finally get the line by specifying a starting point and ending point.

Imageline ($img, 0, 0, +, +, $red);

After the line is drawn, the output of the image is done through the header and Imagepng.

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

Finally, you can call Imagedestroy to release the memory that the picture occupies.

Imagedestroy ($IMG);

Through the above steps, you can find PHP drawing is very simple, but many times we do not just need to output images, perhaps we also need to get a picture file, can be specified by the Imagepng function file name after the image is saved to the file.

Imagepng ($img, ' img.png ');
Draw text in an image

GD library can be used for the basic operation of a variety of graphics, often have drawn lines, background fill, draw rectangles, draw text and so on.

Like drawing a line, you first need to create a new picture and initialize the color.

$img = Imagecreatetruecolor (+), $red = Imagecolorallocate ($img, 0xFF, 0x00, 0x00);

The imagestring function is then used to draw the text, which has many parameters: imagestring (resource $image, int $font, int $x, int $y, string $s, int $col), which can be To set the font size by $font, X, Y sets the position of the text display, $s the text to be drawn, $col is the color of the text.

Imagestring ($img, 5, 0, 0, "Hello World", $red), header ("Content-type:image/png"), Imagepng ($img); Imagedestroy ($img);
Output image File

As we have learned earlier, the image can be output directly to the browser via Imagepng, but many times we want to save the processed image to a file so that it can be used multiple times. Saves the image to a file by specifying a path parameter.

$filename = ' img.png '; Imagepng ($img, $filename);

Use Imagepng to save images in PNG format, if you want to save to other formats using different functions, using imagejpeg to save the picture in JPEG format, imagegif Save the picture to GIF format, you need to note that Imagejpeg compresses the picture, so you can also set a quality parameter.

$filename = ' img.jpg ';? Imagejpeg ($img, $filename, 80);

16/7/11_PHP-GD Library Introduction

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.