Php image processing function example and php verification code

Source: Internet
Author: User
Tags imagecopy imagejpeg mathematical functions
Php image processing function example and php verification code

  1. // Prepare the canvas

  2. $ Im = imagecreatetruecolor (500,300 );

  3. // Prepare paint

  4. $ Black = imagecolorallocate ($ im, 0, 0, 0 );

  5. $ White = imagecolorallocate ($ im, 255,255,255 );

  6. // Fill the background in black

  7. Imagefill ($ im, 0, 0, $ black );

  8. // Draw a rectangle and fill it in white

  9. Imagefilledellipse ($ im, 258,151,200,200, $ white );
  10. // Output to the browser or save it
  11. Header ("content-type: image/png ");
  12. // Output image
  13. Imagepng ($ im );

  14. // Close the canvas

  15. Imagedestroy ($ im );
  16. ?>

Php image processing function 1, math function 2, image processing function

Mathematical functions: 1, max (); 2, min (); 3, mt_rand (); random number

  1. Echomt_rand (1, 5 );
  2. ?>

Mt_rand random value

  1. // Obtain a random value from an array

  2. $ Arr = array ("a", "B", "c", "d", "e ");

  3. $ Rkey = mt_rand (0, count ($ arr)-1 );

  4. Echo $ arr [$ rkey];

  5. ?>

4. ceil (); ceiling 5. floor (); 6. round (); rounding

  1. Echo ceil (2.4); // 3

  2. Echo floor (2.4); // 2
  3. Echo round (2.4); // 2
  4. Echo round (2.6); // 3

  5. ?>

6. pi (); // π obtain the circumference rate

  1. Echo (pi ());
  2. Echo M_PI;
  3. ?>

Image processing function scenario 1. verification code 2. scaling 3. cropping 4. watermark

Five steps for wearing an image in php 1. prepare the Canvas 2. prepare the paint 3. draw an image or text on the canvas 4. output the final image or caocun final image 5. release the canvas resources

Example:

  1. // 1. prepare the canvas

  2. $ Im = imagecreatetruecolor (500,300 );
  3. // 2. prepare the paint
  4. $ Black = imagecolorallocate ($ im, 0, 0, 0 );
  5. $ White = imagecolorallocate ($ im, 255,255,255 );

  6. // 3. draw an image or text on the canvas

  7. // If the background is not filled, the default value is black.
  8. Imageellipse ($ im, 258,151,200,200, $ white );

  9. // 4. output or save the final image

  10. Header ("content-type: image/png ");
  11. Imagepng ($ im );
  12. // 5. release canvas resources
  13. Imagedestroy ($ im );
  14. ?>

Draw an image: imagefill (); imagesetpixel (); // draw the pixel imageline (); // draw the line imagerectangle (); // draw a rectangle imagepolygon (); // draw a polygon imageellipse (); // draw an elliptical imageare (); draw an arc imagechar (); // horizontally draw a character imagestring (); // draw a line of string horizontally

Example:

  1. // Draw a line

  2. // 1. prepare the canvas
  3. $ Im = imagecreatetruecolor (500,300 );
  4. // 2. prepare the paint
  5. $ Black = imagecolorallocate ($ im, 0, 0, 0 );
  6. $ White = imagecolorallocate ($ im, 255,255,255 );

  7. // 3. draw an image or text on the canvas

  8. // If the background is not filled, the default value is black.
  9. Imageline ($ im, 0, 0, 500,300, $ white );
  10. Imageline ($ im, 0,300,500, 0, $ white );
  11. Imageline ($ im, 0,150,500,150, $ white );
  12. Imageline ($ im, 250,0, 250,300, $ white );

  13. // 4. output or save the final image

  14. Header ("content-type: image/png ");
  15. Imagepng ($ im );
  16. // 5. release canvas resources
  17. Imagedestroy ($ im );
  18. ?>

Example:

  1. // Add interferon

  2. // 1. prepare the canvas
  3. $ Im = imagecreatetruecolor (500,300 );
  4. // 2. prepare the paint
  5. $ Black = imagecolorallocate ($ im, 0, 0, 0 );
  6. $ White = imagecolorallocate ($ im, 255,255,255 );

  7. // 3. draw an image or text on the canvas

  8. // Generate random points
  9. For ($ I = 0; I I <1000; $ I ++ ){

  10. Imagesetpixel ($ im, mt_rand (0,500), mt_rand (0,300), $ white );

  11. }

  12. // Generate random lines

  13. For ($ j = 0; $ j <100; $ j ++ ){

  14. Imageline ($ im, mt_rand (0,500), mt_rand (0,300), mt_rand (0,500), mt_rand (0,300), $ white );
  15. } // 4. output or save the final image
  16. Header ("content-type: image/png ");
  17. Imagepng ($ im );
  18. // 5. release canvas resources
  19. Imagedestroy ($ im );
  20. ?>

Example:

  1. // Draw a rectangle:

  2. // 1. prepare the canvas
  3. $ Im = imagecreatetruecolor (500,300 );
  4. // 2. prepare the paint
  5. $ Black = imagecolorallocate ($ im, 0, 0, 0 );
  6. $ White = imagecolorallocate ($ im, 255,255,255 );

  7. // 3. draw an image or text on the canvas

  8. Imagerectangle ($ im, 20, 20,480,280, $ white );//
  9. Imagefilledrectangle ($ im, 20, 20,480,280, $ white); // fill

  10. // 4. output or save the final image

  11. Header ("content-type: image/png ");
  12. Imagepng ($ im );
  13. // 5. release canvas resources
  14. Imagedestroy ($ im );
  15. ?>

Example:

  1. // Imagepolygon: polyg_draw a triangle

  2. // 1. prepare the canvas
  3. $ Im = imagecreatetruecolor (500,300 );
  4. // 2. prepare the paint
  5. $ Black = imagecolorallocate ($ im, 0, 0, 0 );
  6. $ White = imagecolorallocate ($ im, 255,255,255 );

  7. // 3. draw an image or text on the canvas

  8. $ Arr = array (
  9. 250,20,
  10. 60,240,
  11. 440,240
  12. );
  13. Imagepolygon ($ im, $ arr, 3, $ white );

  14. // 4. output or save the final image

  15. Header ("content-type: image/png ");
  16. Imagepng ($ im );
  17. // 5. release canvas resources
  18. Imagedestroy ($ im );
  19. ?>

Example: draw a 3D pie chart

  1. // 1. prepare the canvas

  2. $ Im = imagecreatetruecolor (500,300 );
  3. // 2. prepare the paint
  4. $ Black = imagecolorallocate ($ im, 0, 0, 0 );
  5. $ Red = imagecolorallocate ($ im, 255, 0, 0 );
  6. $ Grayred = imagecolorallocate ($ im, 255,100,100 );
  7. $ Green = imagecolorallocate ($ im, 0,255, 0 );
  8. $ Blue = imagecolorallocate ($ im, 0, 0,255 );
  9. $ Gray = imagecolorallocate ($ im, 200,200,200 );
  10. $ White = imagecolorallocate ($ im, 255,255,255 );

  11. // 3. draw an image or text on the canvas

  12. For ($ I = 0; $ I <10; $ I ++ ){
  13. Imagefilledarc ($ im, 250,150 + $ I, 200,200, 0, 70, $ gray, IMG_ARC_PIE );
  14. Imagefilledarc ($ im, 250,150 + $ I, 200,200, 70,190, $ grayred, IMG_ARC_PIE );
  15. Imagefilledarc ($ im, 250,150 + $ I, 200,200,190,270, $ green, IMG_ARC_PIE );
  16. Imagefilledarc ($ im, 250,150 + $ I, 200,200,270,360, $ blue, IMG_ARC_PIE );

  17. }

  18. Imagefilledarc ($ im, 250,150,200,200, 0, 70, $ white, IMG_ARC_PIE );
  19. Imagefilledarc ($ im, 250,150,200,200, 70,190, $ red, IMG_ARC_PIE );
  20. Imagefilledarc ($ im, 250,150,200,200,190,270, $ green, IMG_ARC_PIE );
  21. Imagefilledarc ($ im, 250,150,200,200,270,360, $ blue, IMG_ARC_PIE );

  22. // 4. output or save the final image

  23. Header ("content-type: image/png ");
  24. Imagepng ($ im );
  25. // 5. release canvas resources
  26. Imagedestroy ($ im );
  27. ?>

Example:

  1. // Write text:

  2. // 1. prepare the canvas
  3. $ Im = imagecreatetruecolor (500,300 );
  4. // 2. prepare the paint
  5. $ Black = imagecolorallocate ($ im, 0, 0, 0 );
  6. $ Red = imagecolorallocate ($ im, 255, 0, 0 );
  7. $ Grayred = imagecolorallocate ($ im, 255,100,100 );
  8. $ Green = imagecolorallocate ($ im, 0,255, 0 );
  9. $ Blue = imagecolorallocate ($ im, 0, 0,255 );
  10. $ Gray = imagecolorallocate ($ im, 200,200,200 );
  11. $ White = imagecolorallocate ($ im, 255,255,255 );

  12. // 3. draw an image or text on the canvas

  13. $ Str = "PHP is very much ";

  14. Imagestring ($ im, 5,260,160, $ str, $ green );

  15. // 4. output or save the final image
  16. Header ("content-type: image/png ");
  17. Imagepng ($ im );
  18. // 5. release canvas resources
  19. Imagedestroy ($ im );
  20. ?>

Example:

  1. // Write a single character:

  2. // 1. prepare the canvas
  3. $ Im = imagecreatetruecolor (500,300 );
  4. // 2. prepare the paint
  5. $ Black = imagecolorallocate ($ im, 0, 0, 0 );
  6. $ Red = imagecolorallocate ($ im, 255, 0, 0 );
  7. $ Grayred = imagecolorallocate ($ im, 255,100,100 );
  8. $ Green = imagecolorallocate ($ im, 0,255, 0 );
  9. $ Blue = imagecolorallocate ($ im, 0, 0,255 );
  10. $ Gray = imagecolorallocate ($ im, 200,200,200 );
  11. $ White = imagecolorallocate ($ im, 255,255,255 );

  12. // 3. draw an image or text on the canvas

  13. $ Str = "P ";

  14. Imagechar ($ im, 5,260,160, $ str, $ green );

  15. // 4. output or save the final image
  16. Header ("content-type: image/png ");
  17. Imagepng ($ im );
  18. // 5. release canvas resources
  19. Imagedestroy ($ im );
  20. ?>

Example,

  1. // Write on the image

  2. // 1. prepare the canvas
  3. $ Im = imagecreatetruecolor (500,300 );
  4. // 2. prepare the paint
  5. $ Black = imagecolorallocate ($ im, 0, 0, 0 );
  6. $ Red = imagecolorallocate ($ im, 255, 0, 0 );
  7. $ Grayred = imagecolorallocate ($ im, 255,100,100 );
  8. $ Green = imagecolorallocate ($ im, 0,255, 0 );
  9. $ Blue = imagecolorallocate ($ im, 0, 0,255 );
  10. $ Gray = imagecolorallocate ($ im, 200,200,200 );
  11. $ White = imagecolorallocate ($ im, 255,255,255 );

  12. // 3. draw an image or text on the canvas

  13. $ Str = "junzaivip ";

  14. $ File = "E:/PHP/fonts/SIMYOU. TTF ";
  15. // $ File = "fonts/SIMYOU. TTF ";

  16. Imagettftext ($ im, 50, 0,100,200, $ green, $ file, $ str );

  17. // 4. output or save the final image

  18. Header ("content-type: image/png ");
  19. Imagepng ($ im );
  20. // 5. release canvas resources
  21. Imagedestroy ($ im );
  22. ?>

PHP verification code design

  1. // Prepare the canvas

  2. $ Im = imagecreatetruecolor (100,50 );
  3. // Prepare paint
  4. $ Black = imagecolorallocate ($ im, 0, 0, 0 );
  5. $ Gray = imagecolorallocate ($ im, 200,200,200 );

  6. // Fill in the background

  7. Imagefill ($ im, 0, 0, $ gray );

  8. // Text coordinate

  9. $ X = (100-4*20)/2 + 6;
  10. $ Y = (50-20)/2 + 20;

  11. // Draw an image or text on the canvas

  12. // Associate the three arrays

  13. $ Strarr = array_merge (range (1, 9), range (a, z), range (A, Z ));

  14. // Disrupt the array

  15. Shuffle ($ strarr );

  16. // Array_slice: obtains the first few digits of the array.

  17. // Join converts the array into a string and uses the first variable as the separator.
  18. $ Str = join ('', array_slice ($ strarr, 0, 4 ));

  19. $ File = "E:/PHP/fonts/msyh. ttf ";

  20. // $ File = "fonts/msyh. ttf ";

  21. Imagettftext ($ im, 20, 0, $ x, $ y, $ black, $ file, $ str );

  22. // Output or save the final image

  23. Header ("content-type: image/png ");
  24. Imagepng ($ im );
  25. // Release canvas resources
  26. Imagedestroy ($ im );
  27. ?>

Php verification code design: two pages are involved: index. php & reg. php description:

This verification code version only allows you to dynamically obtain the verification code of the verification image on the registration page at the front end and compare the verification code generated for the image. the verification code consists of numbers, lowercase letters, and uppercase letters.

Index. php // implement user registration

  1. Reg
  2. User registration page

Reg. php // used to verify whether the verification code is correct

  1. Session_start ();

  2. // Echo $ _ POST ['username'];
  3. // Echo $ _ POST ['password'];
  4. $ Code = strtolower ($ _ POST ['vcode']);

  5. // Echo $ code;

  6. // Echo"

    ";  
  7. // print_r($_SESSION);
  8. // echo "
  9. ";
  10. $ Vstr = strtolower ($ _ SESSION ['vstr']);

  11. If ($ code = $ vstr ){

  12. // Jump to the page
  13. Echo "script location = 'http: // www.xingzuo51.com 'script";
  14. } Else {
  15. Echo "script" alert ('verification code input error') script ";
  16. // Echo "return to registration page ";
  17. Echo "script location = 'index. php' script";

  18. }

  19. ?>

Auth. php is used to generate a verification code

  1. // Enable session. no output is allowed before the session is enabled.

  2. Session_start ();

  3. $ Width = 50; // verification code background width
  4. $ Height = 26; // verification code background high speed
  5. $ Fonttype = 10; // The size of the characters in the verification code
  6. // Prepare the canvas
  7. $ Im = imagecreatetruecolor ($ width, $ height );
  8. // Prepare paint
  9. $ Black = imagecolorallocate ($ im, 0, 0, 0 );
  10. $ Gray = imagecolorallocate ($ im, 200,200,200 );

  11. // Fill in the background

  12. Imagefill ($ im, 0, 0, $ gray );

  13. // Text coordinate

  14. $ X = ($ width-4 * $ fonttype)/2 + 2;
  15. $ Y = ($ height-$ fonttype)/2 + $ fonttype;

  16. // Draw an image or text on the canvas

  17. // Associate the three arrays

  18. $ Strarr = array_merge (range (1, 9), range (a, z), range (A, Z ));

  19. // Disrupt the array

  20. Shuffle ($ strarr );

  21. // Array_slice: obtains the first few digits of the array.

  22. // Join converts the array into a string and uses the first variable as the separator.
  23. $ Str = join ('', array_slice ($ strarr, 0, 4 ));

  24. // Put $ str into the session to facilitate use on all pages

  25. $ _ SESSION ['vstr'] = $ str;

  26. $ File = "E:/PHP/fonts/msyh. ttf ";

  27. // $ File = "fonts/msyh. ttf ";

  28. Imagettftext ($ im, $ fonttype, 0, $ x, $ y, $ black, $ file, $ str );

  29. // Output or save the final image

  30. Header ("content-type: image/png ");
  31. Imagepng ($ im );
  32. // Release canvas resources
  33. Imagedestroy ($ im );
  34. ?>

Php verification code design: page jump: 1, php jump

  1. $ Im = imagecreatefromjpeg ("lyf.jpg ");

  2. $ X = imagesx ($ im );

  3. $ Y = imagesy ($ im );

  4. Echo $ x. $ y;

  5. Exit;

  6. Header ("content-type: image/jpeg ");

  7. Imagejpeg ($ im );
  8. ?>

Method 2: obtain the image size:

  1. $ Imgfile = "lyf.jpg ";

  2. $ Imgarr = getimagesize ($ imgfile );

  3. Echo"

    ";  
  4. print_r($imgarr);
  5. echo "
  6. ";

  7. Exit;

  8. $ Im = imagecreatefromjpeg ("lyf.jpg ");

  9. Echo $ x. $ y;

  10. Header ("content-type: image/jpeg ");

  11. Imagejpeg ($ im );
  12. ?>

Image scaling function:

  1. $ Imgfile = "lyf.jpg ";

  2. // To obtain the width and height of a large image

  3. $ Imgarr = getimagesize ($ imgfile );

  4. $ Maxw = $ imgarr [0];

  5. $ Maxh = $ imgarr [1];
  6. $ Maxt = $ imgarr [2];
  7. $ Maxm = $ imgarr ['Mime '];

  8. // To change a large image to a resource

  9. $ Im = imagecreatefromjpeg ("lyf.jpg ");

  10. // Map resource

  11. $ Minw = 100;
  12. $ Minh = 400;

  13. // Proportional scaling
  14. If ($ minw/$ maxw)> ($ minh/$ maxh )){
  15. $ Rate = $ minh/$ maxh;
  16. } Else {
  17. $ Rate = $ minw/$ maxw;
  18. }

  19. $ Minw = floor ($ maxw * $ rate );

  20. $ Minh = floor ($ maxh * $ rate );
  21. $ Minim = imagecreatetruecolor ($ minw, $ minh );

  22. // Scale a large image to a small image

  23. Imagecopyresampled ($ minim, $ im, 0, 0, 0, $ minw, $ minh, $ maxw, $ maxh );

  24. // Small image output

  25. Header ("content-type: {$ maxm }");

  26. // Determine the type

  27. Switch ($ maxt ){
  28. Case 1:
  29. $ Imageout = "imagegif ";
  30. Break;
  31. Case 2:
  32. $ Imageout = "imagejpeg ";
  33. Break;
  34. Case 3:
  35. $ Imageout = "imagepng ";
  36. Break;

  37. }

  38. $ Imageout ($ minim );

  39. $ Minfilename = "s _". $ imgfile;
  40. $ Imageout ($ minim, $ minfilename );
  41. // Imagejpeg ($ im );

  42. // Release resources
  43. Imagedestroy ($ maxim );
  44. Imagedestroy ($ minim );
  45. ?>

Image cropping function: imagecopyresampled ();

Image watermark function: imagecopy ();

3. crop

4. watermark

  1. $ Maxim = imagecreatefromjpeg ("lyf.jpg ");

  2. $ Minim = imagecreatefromjpeg ("lyf.jpg ");

  3. $ Maxw = imagesx ($ maxim );

  4. $ Maxh = imagesy ($ maxim );

  5. $ Minw = imagesx ($ minim );

  6. $ Minh = imagesy ($ minim );

  7. Imagecopy ($ maxim, $ minim, $ maxw-$ minw, $ maxh-$ minh, 0, 0, $ minw, $ minh );

  8. Header ("content-type: image/jpeg ");

  9. Imagejpeg ($ mamim );

  10. ?>

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.