: This article mainly introduces PHP image operations. if you are interested in the PHP Tutorial, you can refer to it. GD Database Overview
GD refers to Graphic Device. the GD library of PHP is an extension library used to process graphics. through a series of APIs provided by the GD Library, images can be processed or new images can be generated directly.
In addition to text processing, PHP can process JPG, PNG, GIF, SWF, and other images through the GD Library. The GD Library is commonly used in image watermarking and verification code generation.
By default, PHP has integrated the GD Library. you only need to enable it during installation.
Header ("content-type: image/png"); $ img = imagecreatetruecolor (100,100); $ red = imagecolorallocate ($ img, 0xFF, 0x00, 0x00 ); imagefill ($ img, 0, 0, $ red); imagepng ($ img); imagedestroy ($ img );
Draw lines
To operate the image, you must first create a canvas. you can use the imagecreatetruecolor function to create a real-color blank image:
$ Img = imagecreatetruecolor (0, 100,100 );
In the GD Library, the color of the paint brush needs to be allocated through the imagecolorallocate function, and the color of the paint brush is determined by setting the RGB color value through parameters:
$ Red = imagecolorallocate ($ img, 0xFF, 0x00, 0x00 );
Then, we call the line segment function imageline to draw the line, and specify the start and end points to finally obtain the line.
Imageline ($ img, 0, 0,100,100, $ red );
After the line is drawn, the image is output through the header and imagepng.
Header ("content-type: image/png"); imagepng ($ img );
You can call imagedestroy to release the memory occupied by the image.
Imagedestroy ($ img );
Through the above steps, we can find that PHP is very simple to draw images, but many times we do not only need to output images, we may also need to get an image file, you can use the imagepng function to specify the file name to save the drawn image to the file.
Imagepng ($ img, 'img.png ');
Draw text in an image
The GD library can be used to perform basic operations on a variety of graphics, such as drawing lines, background filling, drawing rectangles, and drawing texts.
Similar to creating a line, you must first create an image and initialize the color.
$ Img = imagecreatetruecolor (100,100); $ red = imagecolorallocate ($ img, 0xFF, 0x00, 0x00 );
The imagestring function is used to draw text. many parameters of this function are: imagestring (resource $ image, int $ font, int $ x, int $ y, string $ s, int $ col), you can use $ font to set the font size, x, y to set the position where the text is displayed, $ s is the text to be drawn, and $ 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 before, imagepng can directly output images to the browser, but many times we want to save the processed images to files so that they can be used multiple times. Save the image to a file by specifying the path parameters.
$ Filename = 'img.png '; imagepng ($ img, $ filename );
You can use imagepng to save an image as png. if you want to save the image as another format, you need to use different functions to save the image as jpeg, and imagegif to save the image as gif, it must be noted that imagejpeg compresses the image, so you can set a quality parameter.
$ Filename = 'img.jpg ';? Imagejpeg ($ img, $ filename, 80 );
Generate image verification code
The simple verification code is actually to output several characters in the image, which can be achieved through the imagestring function described in the previous chapter.
However, in terms of processing, in order to make the verification code more secure and prevent automatic identification by other programs, it is often necessary to interfere with the verification code. generally, noise and line segments are drawn, skew and distortion of output characters.
You can use imagesetpixel to draw a point to implement noise interference, but it is not helpful to draw only one point. Therefore, here we often use loops to draw randomly.
For ($ I = 0; $ I <50; $ I ++) {imagesetpixel ($ im, rand (0,100), rand (0,100), $ black ); imagesetpixel ($ im, rand (0,100), rand (0,100), $ green );}
Add watermarks to images
You can add a watermark to an image by adding a string to the image, or adding a logo or another image to the image.
Because existing images are processed here, you can create a canvas directly from an existing image and create an image directly from the image file through imagecreatefromjpeg.
$ Im = imagecreatefromjpeg ($ filename );
After creating an image object, we can use the previous GD function to draw a string to the image. If the watermark to be added is a logo image, you need to create another image object, and then copy the logo image to the source image through the GD function imagecopy.
$ Logo = imagecreatefrompng ($ filename); imagecopy ($ im, $ logo, 15, 15, 0, 0, $ width, $ height );
After you copy the logo image to the original image, you can save the output of the watermark image.
Imagejpeg ($ im, $ filename );
Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.
The above introduces the PHP image operations, including the content, hope to be helpful to friends who are interested in PHP tutorials.