The example in this paper describes the PHP image processing. Share to everyone for your reference, as follows:
demo1.php
<?php //General generated image can be png,jpg,gif,bmp //jpeg,wbmp //First step, set file MIME type, output type text/html type is page type, default can not write Change the output type to the image stream header (' content-type:image/png; '); The second step, create a graphics area, image background //There are two ways to create, resource type, usually with the @ symbol, to prevent errors //imagecreatetruecolor returns a resource handle //This function creates an area of an image , the background is black $im = Imagecreatetruecolor (200,200) when no padding is made; The third step, there is a blank image area, drawing color, text called, line ah ... ///Fill color Replace, the first to have a color filler //imagecolorallocate--To assign a color to an image $blue = Imagecolorallocate ($im, 0,102,255); Fill the blue color to the background //imagefill-area filled imagefill ($im, 0,0, $blue); Fourth, enter some lines on the blue background, text, etc. $white = imagecolorallocate ($im, 255,255,255); Imageline--Draw a line segment imageline ($im, 0,0,200,200, $white); Imageline ($im, 200,0,0,200, $white); Imagestring--Draw a line of strings horizontally imagestring ($im, 5,80,20, ' Mr.one ', $white); Fifth step, output final graphics //output images in PNG format to browser or file imagepng ($im); Sixth step, I want to empty all the resources Imagedestroy ($IM); >
demo2.php
<?php //SRC can insert all kinds of pictures //demo1.php is actually a PNG image header (' content-type:text/html; Charset=gbk '); Echo '
demo3.php
<?php //Simple Verification Code //random number// Why do you want to loop the number between 0-15? //Because to achieve the simplest mix of letters and numbers //16 binary 0-9 a-f //dechex--decimal conversion to hexadecimal //create a four-bit verification code for ($i =0; $i <4; $i + + { $nmsg. = Dechex (Mt_rand (0,15)); } echo $nmsg; Header (' content-type:image/png; '); $im = Imagecreatetruecolor (75,25); $blue = Imagecolorallocate ($im, 0,102,255); $white = Imagecolorallocate ($im, 255,255,255); Imagefill ($im, 0,0, $blue); Imagestring ($im, 5,20,5, $nmsg, $white); Imagepng ($im); Imagedestroy ($im);? >
demo4.php
<?php define (' __dir__ ', DirName (__file__). ' \\'); Load the existing image header (' content-type:image/png; '); Header (' content-type:image/jpeg; '); Imagecreatefrompng--Create a new image from a PNG file or URL //load an image with image, you can edit the image //In the loaded image, add a small watermark $im = Imagecreatefrompng (__dir__. ' Ss.png '); $im = Imagecreatefromjpeg (' xx.jpg '); $white = Imagecolorallocate ($im, 255,255,255); Imagestring ($im, 5,10,10, ' http://www.oneStopWeb.cn ', $white); Imagepng ($im); Imagejpeg ($im); Imagedestroy ($im);? >
demo5.php
<?php define (' __dir__ ', DirName (__file__). ' \\'); Load the existing image header (' content-type:image/png; '); Header (' content-type:image/jpeg; '); Imagecreatefrompng--Create a new image from a PNG file or URL //load an image with image, you can edit the image //In the loaded image, add a small watermark $im = Imagecreatefrompng (__dir__. ' Ss.png '); $im = Imagecreatefromjpeg (' xx.jpg '); $white = Imagecolorallocate ($im, 255,255,255); Imagestring ($im, 5,10,10, ' http://www.oneStopWeb.cn ', $white); Font fonts must also support Chinese $font = ' C:\WINDOWS\Fonts\SIMHEI. TTF '; Font file $text = iconv (' GBK ', ' utf-8 ', ' read who asks the chant '); Using the system-provided font //second parameter, is the size of the font, the third parameter is the rotation angle, the 4,5 parameter is the coordinate imagettftext ($im, 20,10,50,100, $white, $font, $text); Imagepng ($im); Imagejpeg ($im); Imagedestroy ($im);? >
demo6.php
<?php //thumbnail, not only the size of the surface has changed, the capacity has changed //is really changed, not the surface of the narrowing define (' __dir__ ', DirName (__file__). ' \\'); Header (' content-type:image/png; '); GetImageSize--Get the image size //Get to the original length and height list ($width, $height) = getimagesize (__dir__. ') Ss.png '); Scale the original image to 40% $_width = $width * 0.4; $_height = $height * 0.4; Create a new diagram $im = Imagecreatetruecolor ($_width,$_height); The following work is to load the original picture, copy the original image to the new diagram //Load the original $_im = Imagecreatefrompng (__dir__. ' Ss.png '); Resample the original image, copy it to the new diagram, and finally output the//imagecopyresampled at a scale of 0.4-resampling the copy portion of the images and resizing imagecopyresampled ($im, $_im, 0,0,0,0,$_width,$_height, $width, $height); Output the new diagram imagepng ($im); The second parameter does not need, direct null over //third parameter, is 0-100 to adjust the sharpness of JPG //If it is imagepng, then all are HD //imagejpeg ($im, null,50); Destruction of Imagedestroy ($im); Imagedestroy ($_im);? >
I hope this article is helpful to you in PHP programming.