Analysis of image processing techniques in PHP getting started Tutorial: Image Processing
This example describes PHP image processing. We will share this with you for your reference. The details are as follows:
Demo1.php
<? Php // generally, the generated image can be png, jpg, gif, bmp // jpeg, and wbmp. The first step is to set the file MIME type, the text/html Type of the output Type is the webpage Type, which can be left empty by default // change the output Type to the image stream header ('content-Type: image/png; '); // step 2, create a graphic area, image background // There are two ways to create, resource type, usually add @ symbol, prevent errors // imagecreatetruecolor returns a resource handle // This function creates an image area. When it is not filled, the background is black by default $ im = imagecreatetruecolor (200,200 ); // step 3, there is a blank image area, draw color, text name, line... // Fill color change. First, you must have a color filler // imagecolorallocate -- assign color to an image $ blue = imagecolorallocate ($ im, 0,102,255 ); // fill the blue color in the background. // imagefill -- fill the imagefill area ($ im, $ blue); // enter some lines on the blue background, $ white = imagecolorallocate ($ im, 255,255,255); // imageline -- draw a line segment imageline ($ im, 200,200, $ white); imageline ($ im, 0, 0, 0,200, $ white); // imagestring -- horizontally draw a line of string imagestring ($ im, 20, 'Mr. one ', $ white); // Step 5: Enter Output final image // output the image to the browser or file imagepng ($ im) in PNG format; // step 6, I want to clear all the resources imagedestroy ($ im);?>
Demo2.php
<? Php // src can insert various types of images // 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 We Need To cycle the number between 0 and 15? // To achieve the simplest mix of letters and numbers // hexadecimal 0-9 a-f // dechex -- convert decimal to hexadecimal // create a four-digit verification code for ($ I = 0; $ I <4; $ I ++) {$ nmsg. = dechex (mt_rand ();} // echo $ nmsg; header ('content-Type: image/png; '); $ im = imagecreatetruecolor ); $ blue = imagecolorallocate ($ im, 0,102,255); $ white = imagecolorallocate ($ im, 255,255,255); imagefill ($ im, $ 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 -- creates an image from a PNG file or URL. // loads the image with an image. You can edit the image. // In the loaded image, add a small watermark $ im = imagecreatefrompng(__DIR__.'ss.png '); // $ im = imagecreatefrom(('xx.jpg'); $ white = imagecolorallocate ($ im, 255,255,255); imagestring ($ im, 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 -- creates an image from a PNG file or URL. // loads the image with an image. You can edit the image. // In the loaded image, add a small watermark $ im = imagecreatefrompng(__DIR__.'ss.png '); // $ im = imagecreatefrom(('xx.jpg'); $ white = imagecolorallocate ($ im, 255,255,255); imagestring ($ im, 10, 'http: // www.oneStopWeb.cn ', $ white) ; // The font must also support Chinese characters $ font = 'C: \ WINDOWS \ Fonts \ SIMHEI. ttf'; // font file $ text = iconv ('gbk', 'utf-8', 'Who can read this '); // use the font provided by the system. // The second parameter is the font size and the third parameter is the rotation angle. The 50,100 parameter is the coordinate imagettftext ($ im, 20, 10, $ white, $ font, $ text); imagepng ($ im); // imagejpeg ($ im); imagedestroy ($ im);?>
Demo6.php
<? Php // The thumbnail not only changes the surface size, but also the capacity. // It actually changes, not the reduction of the surface ('_ DIR __', dirname (_ FILE __). '\'); header ('content-Type: image/png ;'); // getimagesize -- Obtain the image size // obtain the source image length and height list ($ width, $ height) = getimagesize(%dir%.'ss.png '); // scale the source image to 40% $ _ width = $ width * 0.4; $ _ height = $ height * 0.4; // create a new image $ im = imagecreatetruecolor ($ _ width, $ _ height); // The following task is to load the source image and copy it to the new image. // load the source image $ _ im = imagecreatefrompng (_ DIR __. 'Ss.png '); // re-sample the source image, copy it to the new image, and then output it in a 0.4 ratio. // imagecopyresampled -- Re-sample and copy part of the image and adjust the size of imagecopyresampled ($ im, $ _ im, $ _ width, $ _ height, $ width, $ height); // output imagepng ($ im) to the new image ); // The second parameter is not required. It is directly null excessive // The third parameter is 0-100 to adjust the JPG definition. // if it is imagepng, then all are in high definition // imagejpeg ($ im, null, 50); // destroy imagedestroy ($ im); imagedestroy ($ _ im);?>