This article mainly and everyone to share the PHP image graphics operation of the basic use of GD Library tutorial, I hope to help everyone.
1>GD Library Introduction
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.
General process for creating images
Set the header to tell the browser which MIME type you want to generate
Create an image area that will be based on this image area in future operations
Draw a fill background in a blank image area
Draw a graphic outline on the background enter text
Output Final graphic
Clear All Resources
Other page calls
Header ("Content-type:image/png"); $img =imagecreatetruecolor (+), $red =imagecolorallocate ($img, 0xFF, 0x00, 0x00); Imagefill ($img, 0, 0, $red); Imagepng ($img); Imagedestroy ($img);
Draw lines
Imageline ()
Syntax: Imageline (
Sx
EX,
COL);
Draw a Circle
Imagearc ()
Syntax: Imagearc (
CX,
W
StartAngle,
color)
$img = Imagecreatetruecolor (200, 200);//Assign Color $red = imagecolorallocate ($img, 255, 0, 0); $white = Imagecolorallocate ($img, 255, 255, 255);//background filled with white Imagefill ($img, 0,0, $white);//Draw a red round imagearc ($img, N, N,,, 0, $red); Imagepng ($i MG);//Release Memory Imagedestroy ($IMG);
Draw a rectangle
Imagerectangle ()
Syntax: Imagerectangle (
X1,
X2,
Col
$img = Imagecreatetruecolor (200, 200);//Assign Color $red = imagecolorallocate ($img, 255, 0, 0); $white = Imagecolorallocate ($img, 255, 255, 255); Imagefill ($img, 0,0, $white);//Draw a red rectangle Imagerectangle ($img, 50,50,100, $red); Imagepng ($img);// Free memory Imagedestroy ($IMG);
Draw text
Grammar 1:imagestring (
Font,
Y
COL)
Grammar 2:imagettftext (
Size
X
Color
Text
Header ("Content-type:image/png");//imagestring font size cannot be set $img = Imagecreatetruecolor (+); $red = Imagecolorallocate ($img, 0xFF, 0x00, 0x00); Imagestring ($img, 5, ten, "Hello World", $red); Imagepng ($img); Imagedestroy ($img), $img 1=imagecreatetruecolor (200,200), $red =imagecolorallocate ($img 1,255,0,0); $white Imagecolorallocate ($img 1,255,255,255); Imagefill ($img 1,0,0, $red); $font = "C:\Windows\Fonts\simhei.ttf"; Imagettftext ($img 1,23,0,100,100, $white, $font, "How are You"); Imagepng ($img 1); Imagedestroy ($img 1);
Draw a noise point
Syntax: Imagesetpixel (
X
Col
Draw 10 noise for ($i =0; $i <10; $i + +) { imagesetpixel ($img, rand (0), rand (0,), $black); Imagesetpixel ($img, rand (0, +), rand (0, +), $green);}
Output image file
through Imagepng you can directly output an image to a browser, save the image to a file by specifying a path parameter
1. Imagepng ()
Meaning: Save picture in PNG format
Syntax: imag Epng (
filename)
2. imagejpeg ()
Meaning: Save picture to JPEG format
Syntax: imagepng (filename, $quality)
3. Imagegif ()
Meaning: Save a picture in GIF format
Syntax: imagegif (filename)
Case:
1. Randomly generated verification code (PHP)
2. Add a watermark to a picture