PHP image Processing
Drawing
Verification Code, statistical chart
Installing the GD library-----LAMP
There are php_gd2.dll files in the D:\APMServ5.2.6\PHP\ext file after installation
Extension=php_gd2.dll in the php.ini
(1) Create a canvas---Create a resource type---height width
Resource imagecreate (int $x _size, int $y _size) Create a new palette-based image
Resource Imagecreatetruecolor (int $width, int $height) Create a new true color image
(2) Drawing images
Develop a variety of colors
Imagecolorallocate (); Assigning colors to an image
(Use Imagecolorallocate () to create an image using Imagecreate ())
Imagefill () area fill
Rectangle, Circle, point, segment, fan, stooped (character, string, FreeType)
Each image corresponds to a function
Imagefilledrectangle () draw a rectangle and fill
Imagerectangle () Draw a rectangle
Imageline () draw a line segment
Imagesetpixel () draw a single pixel
Imageellipse () Draw an ellipse
Imagefilledellipse () Draw an ellipse and fill
<?PHP//1. Create a picture resource $img=imagecreatetruecolor (200,200);//$img =imagecreate (200,200); $red=imagecolorallocate ($img, 255, 0, 0); $yellow=imagecolorallocate ($img, 255,255,0); $green=imagecolorallocate ($img, 0,255,0); $blue=imagecolorallocate ($img, 0,0,255); $white=imagecolorallocate ($img, 255,255,255); //Area FillImagefill ($img, 0, 0,$white); //2. Draw a variety of images//Draw a rectangle and fillImagefilledrectangle ($img, 10, 10, 50, 30,$blue); //Draw a rectangleImagerectangle ($img, 100,100,190, 80,$green); //Draw a line segmentImageline ($img, 0, 0,200,200,$red); //Draw pointsImagesetpixel ($img, 200, 90,$yellow); //Draw an ellipseImageellipse ($img, 100,100,100,100,$green); //Draw an ellipse and fill itImagefilledellipse ($img, 100,100,10,10,$red); //3. Output or save images Header("Content-type:image/gif"); Imagegif ($img); //4. Releasing ResourcesImagedestroy ($img);?>
Imagefilledarc () draws an elliptical arc and fills
<?PHP//1. Create a picture resource $img=imagecreatetruecolor (200,200); $white=imagecolorallocate ($img, 255,255,255);//White $gray=imagecolorallocate ($img, 0xc0,0xc0,0xc0);//Grey $darkgray=imagecolorallocate ($img, 0x90,0x90,0x90);//Light Gray $navy=imagecolorallocate ($img, 0,0,0x80);// $darknavy=imagecolorallocate ($img, 0,0,0x50); $red=imagecolorallocate ($img, 255,0,0);//Red $darkred=imagecolorallocate ($img, 0x90,0,0);//Pink//Background set to whiteImagefill ($img, 0, 0,$white); //2. The effect of making 3D for($i= 60;$i>50;$i--){ //Imagefilledarc () draws an elliptical arc and fillsImagefilledarc ($img, 50,$i, 100,50,-160,40,$darknavy,Ing_arc_pie); Imagefilledarc ($img, 50,$i, 100,50,40,75,$darkgray,Ing_arc_pie); Imagefilledarc ($img, 50,$i, 100,50,75,200,$darkred,Ing_arc_pie); } Imagefilledarc ($img, 50,$i, 100,50,-160,40,$navy,Ing_arc_pie); Imagefilledarc ($img, 50,$i, 100,50,40,75,$gray,Ing_arc_pie); Imagefilledarc ($img, 50,$i, 100,50,75,200,$red,Ing_arc_pie); //3. Output or save images Header("Content-type:image/gif"); Imagegif ($img); //4. Releasing ResourcesImagedestroy ($img);?>
Imagechar () to draw a character horizontally
Imagefttext () writes text to an image using the FreeType 2 font
<?PHP//Create a picture resource $img=imagecreatetruecolor (200,200); $white=imagecolorallocate ($img, 255,255,255); $gray=imagecolorallocate ($img, 0xc0,0xc0,0xc0); $darkgray=imagecolorallocate ($img, 0x90,0x90,0x90); $navy=imagecolorallocate ($img, 0,0,0x80); $darknavy=imagecolorallocate ($img, 0,0,0x50); $red=imagecolorallocate ($img, 255,0,0); $darkred=imagecolorallocate ($img, 0x90,0,0); //background set to whiteImagefill ($img, 0, 0,$gray); //Horizontal stooped characterImagechar ($img, 5,100,100, "A",$red); Imagechar ($img, 5,120,120, "B",$red); //Vertical stooped characterImagecharup ($img, 5,60,60, "C",$red); Imagecharup ($img, 5,80,80, "D",$red); //Draw a StringImagestring ($img, 3, ten, ten, "Hello",$navy); Imagestringup ($img, 3,150,150, "Hello",$navy); //Imagefttext () writes text to an image using the FreeType 2 fontImagettftext ($img, 25, 60, 160, 160,$red, "Simkai.ttf", "Hello"); //output or save an image Header("Content-type:image/gif"); Imagegif ($img); //Freeing ResourcesImagedestroy ($img);?>
(3) Output image/save processed image.
Output various types (GIF,PNG,JPEG)
Imagegif ();
Imagejpeg ();
Imagepng ();
(4) Releasing resources
Imagedestroy ();
Drawing of PHP image processing