The GD library is one of the most commonly used image processing libraries in PHP (the other is ImageMagick), which can generate images, verification codes, watermarks, thumbnails, and so on. To use the GD library, you first need to open the GD library extension,
You need to remove the semicolon from the front of the Extension=php_gd2.dll and restart the Web server in php.ini under Windows System.
Linux system generally in the compilation of PHP has opened the GD Library extension, if you do not open the GD library extension will need to compile the installation of FreeType, Jpegsrc,libpng and phpize install the extension library for compilation and installation.
Image generation:
<?PHP/*draw with a Windows drawing board 1. Create a new blank canvas (Specify wide height) 2. Creating pigments. (Red, R green G Blue B, composed of three primary colors.) Three primary colors from weak to strong each can choose between 0-255) 3. Draw lines, write, draw graphics, fill, etc. 4. Save/Output Picture 5. Destroying the canvas*/ //Use the GD library to paint, still the above 5 steps. //1: Make the canvas, return the Imagecreatetruecolor (wide, high) in the form of resources;$im = Imagecreatetruecolor ( -, $); //2: Create pigment imagecolorallocate (canvas, red, green, blue)$gray = Imagecolorallocate ($im, -, -, -); //3: Fill the canvasImagefill ($im,0,0, $gray); //4: Save as Picture imagepng (canvas [, save location]), imagejpeg (), Imagegif ()Header'Content-type:image/jpeg'); Imagepng ($im); //5: Destroy Canvas destroy screen Imagedestroy (canvas)Imagedestroy ($im);?>
Contraction Rate graph
<?PHP/*step: 1. Open the picture source file resource 2. Get the width of the source file 3. Use a fixed formula to calculate the new width height 4. Generate Target Image resource 5. Zoom 6. Save the Image 7. Release Resources*/ //1. Open the image source file resource$im = Imagecreatefromjpeg ('./bg.jpg'); //2. Get the width and height of the source file$FX = Imagesx ($im);//Get width$fy = Imagesy ($im);//Get Height//3. Calculate the new width and height using a fixed formula$SX = $FX/2; $sy= $fy/2; //4. Generating the target image resource$small =Imagecreatetruecolor ($SX, $sy); //5. Zoom inImagecopyresampled ($small, $im,0,0,0,0, $SX, $sy, $fx, $fy); //6. Save the image if(Imagejpeg ($small,'./sbg.jpg') {echo'saved successfully'; } Else{echo'Save failed'; } //7. Releasing ResourcesImagedestroy ($im); Imagedestroy ($small); ?>
Watermark Generation
<?PHP/*steps: 1. Create a size drawing cloth and get their width height 2. Add a text watermark 3. Perform image watermark processing 4. Output 5. Destroying the canvas*/ //1. Create the size drawing cloth separately and get their width and height$big = Imagecreatefromjpeg ('./bg.jpg'); $BX=Imagesx ($BIG); $by=Imagesy ($BIG); $small= Imagecreatefrompng ('./tu.png'); $SX=Imagesx ($small); $sy=Imagesy ($small); //2. Add watermark Text$blue = Imagecolorallocate ($big,0,0,255); Imagettftext ($big, -,0, -, -, $blue,'./msyh.ttf','proud teenager.'); //3. Perform image watermark processingImagecopymerge ($big, $small, $BX-$SX,0,0,0, $SX, $sy,Panax Notoginseng); //4. Output to browserHeader'Content-type:image/jpeg'); Imagejpeg ($BIG); //5. Destroying the canvasImagedestroy ($BIG); Imagedestroy ($small); ?>
Verification Code
<?PHP/*steps: 1. Create a canvas 2. Paint 3. Fill the background color 4. Draw Interference points 5. Draw Noise 6. Write the string 7. Output Picture 8. Destroy the painting Cloth*/ //1. Create a canvas$im =imagecreatetruecolor ( -, -); //2. Pigment making$gray = Imagecolorallocate ($im, -, -, -); $red= Imagecolorallocate ($im,255,0,0); $blue= Imagecolorallocate ($im, -,255,255); //3. Fill the background colorImagefill ($im,0,0, $blue); //4. Draw Interference points for($i =0; $i <4; $i + +) {imageline ($im, rand (0, -),0, -, Rand (0, -), $red); } //5. Draw the noise point for($i =0; $i < -; $i + +) {Imagesetpixel ($im, rand (0, -), Rand (0, -), $gray); } //6. Write a string$str =substr (Str_shuffle ('ABCDEFGHIJKMNPQRSTUVWXYZabcdefghijkmnpqrstuvwxyz23456789'),0,4); Imagestring ($im,5,5,5, $str, $red); //7. Output ImageHeader'Content-type:image/png'); Imagepng ($im); //8. Destroying the canvasImagedestroy ($im);?>
In the process of use. If you just need to export the picture. You can use the second parameter of Imagepng (). Like what
Imagepng ($im,' picture storage path '
"PHP" using GD library for image generation, zooming, logo watermark and simple verification code