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);
Generate an Image verification code
The simple verification code is actually the output of a few characters in the image, through the imagestring function described in the previous section can be achieved.
But in the processing, in order to make the verification code more secure, prevent other programs to automatically recognize, so often need to do some interference processing of verification code, usually by drawing some noise, interfering with line segments, the output of the characters to tilt, distort and other operations.
You can use Imagesetpixel to draw points to achieve noise disturbances, but drawing only one point is not very useful, so it is often used for random plotting using loops.
for ($i =0; $i <50; $i + +) { imagesetpixel ($im, rand (0), rand (0, +), $black);
<?php$img = Imagecreatetruecolor (+), $black = Imagecolorallocate ($img, 0x00, 0x00, 0x00); $green = Imagecolorallocate ($img, 0x00, 0xFF, 0x00); $white = Imagecolorallocate ($img, 0xFF, 0xFF, 0xFF); Imagefill ($img, 0,0,$ white);//Generate random verification code $code = "; for ($i = 0; $i < 4; $i + +) { $code. = rand (0, 9);} Imagestring ($img, 5, ten, $code, $black);//Add noise interference for ($i =0; $i <50; $i + +) { imagesetpixel ($img, rand (0, 100), Rand (0), $black); Imagesetpixel ($img, rand (0, +), rand (0, +), $green);} Output Verification Code header ("Content-type:image/png"); Imagepng ($img); Imagedestroy ($img);
Add a watermark to a picture
There are two ways to add a watermark to a picture, one is to put a string on top of the picture, and the other is to include a logo or other image on the image.
Because this is a picture that already exists, you can create a canvas directly from an existing image, creating an image directly from the image file via Imagecreatefromjpeg.
$im = Imagecreatefromjpeg ($filename);
After creating the image object, we can draw the string to the image through the previous GD function. If the watermark to be added is a logo image, then you need to create an image object, and then through the GD function imagecopy to copy the image of the logo to the source image.
$logo = Imagecreatefrompng ($filename); Imagecopy ($im, $logo, 0, 0, $width, $height);
When the logo image is copied to the original image, the watermark will be added after the output of the image to complete the watermark processing.
imagejpeg ($im, $filename);
<?php//here is just for the case need to prepare some footage of the image of the "http://www.iyi8.com/uploadfile/2014/0521/20140521105216901.jpg"; $content = File_get_contents ($url); $filename = ' tmp.jpg '; file_put_contents ($filename, $content); $url = ' http:// Wiki.ubuntu.org.cn/images/3/3b/qref_edubuntu_logo.png '; file_put_contents (' Logo.png ', file_get_contents ($url)); /Start Add watermark Operation $im = Imagecreatefromjpeg ($filename); $logo = Imagecreatefrompng (' logo.png '); $size = getimagesize (' logo.png ') ); Imagecopy ($im, $logo, 0, 0, $size [0], $size [1]); Header ("Content-type:image/jpeg"); imagejpeg ($im);
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
PHP Graphics Image Manipulation