1. Verify that the GD Library in PHP is open
Find Extension=php_gd2.dll in php config file php.ini, remove the front (semicolon) '; ' , General PHP is enabled by default
2. Painting steps
- Create a canvas (artboard), brush, color.
- * Start painting (emphasis, difficulty)
- Output image (copy type)
- Destroying image resources (freeing memory)
3. Example
To facilitate the demonstration, first understand the next two steps
Output Image:
1 Header("Content-type:image/jpeg");//Set the response header information to a JPEG picture2Imagejpeg ($im);//output a JPEG image3 4 Header("Content-type:image/png");//Set the response header information to a PNG picture5Imagepng ($im);//output a PNG image6 7//output to the specified file (save As)8Imagepng ($im, "**.png");
Destroying images (decommissioning of resources):
1 bool Imagedestroy resource$image )//
Create a canvas:
1 // Create a 256-color canvas 2 $img=imagecreate (400,400); 3 Echo $img;
Display effect:
1 // Create a new true color image 2 $img=imagecreatetruecolor (400,400); 3 Echo $img;
Display effect:
The other way:
// and a canvas based on a picture. string stringstring filename
True Color and 256-color canvas display Difference-----True color will be filled with black canvas by default
<? php // Create a 256-color-based canvas $img 1 =imagecreate (400,400 $img 2 =imagecreatetruecolor (400,400 // output image header (' content-type:image/png ' ); // Imagepng ($img 1); Imagepng ( $img 2 // Destroy the image, remove the resource occupied//Imagedestroy ($img 1); Imagedestroy ( $img 2 ?
Display effect:
* Start painting
<?PHP//Assign definition Color $red= Imagecolorallocate ($im, 255,0,0);//assign a color//fill backgroundBOOL Imagefill (ResourceImage,int x,int y, int color);//Fill Background//Draw pointBOOL Imagesetpixel (ResourceImage,int x,int y,int color);//draw a pixel///function to draw a segmentBOOL Imageline (Resourceimage, int x1, int y1, int x2, int y2,int color)//Draw a rectangular box (not filled)BOOL Imagerectangle (Resourceimage, int x1, int y1, int x2, int y2,int color)//Draw a rectangular box (fill)BOOL Imagefilledrectangle (Resourceimage, int x1, int y1, int x2, int y2,int color)//Drawing PolygonsBOOL Imagepolygon (ResourceImageArraypoints, int num_points,int color) bool Imagefilledpolygon (ResourceImageArraypoints, int num_points,int color)//Draw ellipse (positive circle)Imageellipse (Resourceimage, int cx, int cy, int w, int h,int color) imagefilledellipse (Resourceimage, int cx, int cy, int w, int h,int color)//draw arcs (can be filled)Imagearc (Resourceimage, int cx, int cy, int w, int h, int s, int e, int color,int style) Imagefilledarc (Resourceimage, int cx, int cy, int w, int h, int s, int e, int color,int style)//Draw a StringBOOL Imagestring (Resourceimage, int font, int x, int y,stringSint col) bool Imagestringup (Resourceimage, int font, int x, int y,stringSint col)//Drawing charactersImagechar//Draw Text:*ArrayImagettftext (ResourceImagefloatSizefloatangle, int x, int y, int color,stringFontfile,stringtext)//When the above font is garbled, use the following function to convert the encodingstring Iconv(stringIn_charset,stringOut_charset,stringstr)$name= "Zhang San"; $str=Iconv("Iso8859-1", "UTF-8",$name); $str=Iconv("GBK", "UTF-8",$name); $str=Iconv("GB2312", "UTF-8",$name); //cropping, compositing, zooming of pictures**bool imagecopyresampled (ResourceDst_image,Resourcesrc_image, int dst_x, int dst_y, int src_x, int src_y, int dst_w, int dst_h, int src_w,int src_h)*imagesx-Get Image width*imagesy-Get Image Height*Array getimagesize(string $filename[,Array&$imageinfo]) to get the image size?>
Composite code Example:
1<?PHP2 //Create a canvas3 $img=imagecreatetruecolor (1000,1000);4 //Create color5 $red=imagecolorallocate ($img, 255,0,0);6 $blue=imagecolorallocate ($img, 0, 0, 255);7 $green=imagecolorallocate ($img, 0, 255, 0);8 $gray=imagecolorallocate ($img, 200, 200, 200);9 Ten //Fill Color OneImagefill ($img, 100, 0,$gray); A //Create a point -Imagesetpixel ($img, 20, 20,$red); - //randomly create 1000 points the for($i= 0;$i<1000;$i++){ -Imagesetpixel ($img,Rand(0,1000),Rand(0,1000),$red); - } - //Output Segment +Imageline ($img, 0, 0, 200, 200,$blue); - //Output Rectangle +Imagerectangle ($img, 200, 200, 400, 400,$blue);//not populated AImagefilledrectangle ($img, 200, 400, 400, 600,$blue);//Fill at //Draw polygon Imagefilledpolygon----Fill -Imagepolygon ($img,Array(0,0,100,200,300,200), 3,$blue); - //Draw ellipse (positive circle) -Imageellipse ($img, 600, 600, 200, 200,$blue); -Imagefilledellipse ($img, 800, 600, 200, 200,$blue); - //Draw a String inImagestring ($img, "string",$blue);//level -Imagestringup ($img, "string",$blue);//Vertical to //Draw characters +Imagechar ($img, 5, +, ' H ',$blue); -Imagecharup ($img, 5, +, ' H ',$blue); the //Draw Text *Imagettftext ($img, 20, 0, 500, 500,$blue, ' BB.TTC ', ' This is string! '); $ /*when the above font is garbled, use the following function to convert the encodingPanax Notoginseng string Iconv (String in_charset, String out_charset, String str)*/ - //imagecopyresampled ($img, "dd.jpg", "N", " a", "a", "a", "$", "$", "$"); the //Output image + Header(' Content-type:image/png '); AImagepng ($img); the //Destroying Images +Imagedestroy ($img); -?>
Difficult to focus on:
php-> new image using GD library