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