PHP image processing function Instance Daquan and PHP verification code

Source: Internet
Author: User
Tags imagecopy imagejpeg mathematical functions shuffle
    1. Prepare the canvas

    2. $im = Imagecreatetruecolor (500, 300);

    3. Preparing Paint

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

    5. $white = Imagecolorallocate ($im, 255, 255, 255);

    6. Background filled with black

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

    8. Draw a rectangle, fill it with white

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

    14. Close Canvas

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

Copy Code

PHP image processing function 1, math function 2, image processing function

Mathematical functions: 1,max (); 2,min (); 3,mt_rand (); randomly taking a number

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

Mt_rand randomly take a value

    1. Randomly take a 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. ?>

Copy Code

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. ?>

Copy Code

6.pi (); Pi take pi

    1. Echo (pi ());
    2. Echo M_pi;
    3. ?>
Copy Code

Picture processing functions use scenario 1. Verification Code 2. Scaling 3. Cropping 4. Watermark

Five steps to wear an image in PHP 1. Prepare the Canvas 2. Prepare Paint 3. Draw an image or text on the canvas 4. Output final image or Cao Cun final image 5. Release Canvas Resources

Example:

    1. 1. Preparing the canvas

    2. $im = Imagecreatetruecolor (500,300);
    3. 2. Preparing 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 populated, the default is black
    8. Imageellipse ($im, 258,151,200,200, $white);

    9. 4. Output the final image or save the final image

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

Copy Code

Draw Image: Imagefill (); Imagesetpixel ();//Portrait Point Imageline ();//Draw line Imagerectangle ();//Draw a rectangle Imagepolygon ();// Draw a polygon imageellipse ();//Draw an ellipse imageare (); Draw an arc Imagechar ();//horizontal Draw a character imagestring ();//horizontal draw a line of string

Example:

    1. Draw Line

    2. 1. Preparing the canvas
    3. $im = Imagecreatetruecolor (500,300);
    4. 2. Preparing 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 populated, the default 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 the final image or save the final image

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

Copy Code

Example:

    1. Add interferon

    2. 1. Preparing the canvas
    3. $im = Imagecreatetruecolor (500,300);
    4. 2. Preparing 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 a random point
    9. for ($i =0; $i < $i + +) {

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

    11. }

    12. Generate a random line

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

    14. Imageline ($im, Mt_rand (0,500), Mt_rand (0,300), Mt_rand (0,500), Mt_rand (0,300), $white);
    15. }//4. outputting the final image or saving the final image
    16. Header ("Content-type:image/png");
    17. Imagepng ($im);
    18. 5. Releasing Canvas resources
    19. Imagedestroy ($im);
    20. ?>

Copy Code

Example:

    1. Draw a rectangle:

    2. 1. Preparing the canvas
    3. $im = Imagecreatetruecolor (500,300);
    4. 2. Preparing 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, 480, 280, $white);//
    9. Imagefilledrectangle ($im, 480, 280, $white);//Fill

    10. 4. Output the final image or save the final image

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

Copy Code

Example:

    1. Imagepolygon Draw Polygon _ Draw Triangle

    2. 1. Preparing the canvas
    3. $im = Imagecreatetruecolor (500,300);
    4. 2. Preparing 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 the final image or save the final image

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

Copy Code

example, draw a 3D pie chart

  1. 1. Preparing the canvas

  2. $im = Imagecreatetruecolor (500,300);
  3. 2. Preparing 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 < $i + +) {
  13. Imagefilledarc ($im, $i, 150+, 0, $gray, Img_arc_pie);
  14. Imagefilledarc ($im, $i, 150+, $grayred, Img_arc_pie);
  15. Imagefilledarc ($im, $i, 150+, $green, Img_arc_pie);
  16. Imagefilledarc ($im, $i, 150+, $blue, Img_arc_pie);

  17. }

  18. Imagefilledarc ($im, $, 0, $white, Img_arc_pie);
  19. Imagefilledarc ($im,,,,,,,,, $red, Img_arc_pie);
  20. Imagefilledarc ($im,,,,,,,, $green, Img_arc_pie);
  21. Imagefilledarc ($im,,,,,,,, $blue, Img_arc_pie);

  22. 4. Output the final image or save the final image

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

Copy Code

Example:

    1. Write text:

    2. 1. Preparing the canvas
    3. $im = Imagecreatetruecolor (500,300);
    4. 2. Preparing 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, $STR, $green);

    15. 4. Output the final image or save the final image
    16. Header ("Content-type:image/png");
    17. Imagepng ($im);
    18. 5. Releasing Canvas resources
    19. Imagedestroy ($im);
    20. ?>

Copy Code

Example:

    1. Write a single character:

    2. 1. Preparing the canvas
    3. $im = Imagecreatetruecolor (500,300);
    4. 2. Preparing 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, $STR, $green);

    15. 4. Output the final image or save the final image
    16. Header ("Content-type:image/png");
    17. Imagepng ($im);
    18. 5. Releasing Canvas resources
    19. Imagedestroy ($im);
    20. ?>

Copy Code

Example

  1. Write on a picture

  2. 1. Preparing the canvas
  3. $im = Imagecreatetruecolor (500,300);
  4. 2. Preparing 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, 0, $green, $file, $STR);

  17. 4. Output the final image or save the final image

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

Copy Code

Design of PHP Verification code

  1. Prepare the canvas

  2. $im = Imagecreatetruecolor (100,50);
  3. Preparing Paint
  4. $black = imagecolorallocate ($im, 0, 0, 0);
  5. $gray = Imagecolorallocate ($im, 200, 200, 200);

  6. Fill background

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

  8. Text coordinates

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

  11. Draw an image or text on the canvas

  12. Connect the three arrays.

  13. $strarr = Array_merge (range (1, 9), range (A, z), range (A, z));

  14. Scrambled Array

  15. Shuffle ($strarr);

  16. Array_slice: Take the first few of the array

  17. Join converts an array into a string and breaks it with the first variable
  18. $str = Join ("', Array_slice ($strarr, 0,4));

  19. $file = "E:/php/fonts/msyh.ttf";

  20. $file = "Fonts/msyh.ttf";

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

  22. Output final image or save final image

  23. Header ("Content-type:image/png");
  24. Imagepng ($im);
  25. Releasing Canvas resources
  26. Imagedestroy ($im);
  27. ?>
Copy Code

PHP captcha Design: There are two pages involved: index.php & reg.php Description:

This verification code version only implements the verification image of the dynamic access to the foreground registration page verification code and generate a picture of the verification code to compare the verification code is made up of digital lowercase capital letters randomly

index.php//to implement user registration

    1. Reg
    2. User Registration page

Copy Code

reg.php//to verify that 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. Implement a page jump
    13. echo "";
    14. }else{
    15. echo "";
    16. echo "Return to registration page";
    17. echo "";

    18. }

    19. ?>

Copy Code

Auth.php used to generate a verification code

  1. Open session, before opening session, no output

  2. Session_Start ();

  3. $width = 50;//Verification Code background width
  4. $height = 26;//Verification Code background High speed
  5. $fonttype = size of Word in 10;//verification code
  6. Prepare the canvas
  7. $im = Imagecreatetruecolor ($width, $height);
  8. Preparing Paint
  9. $black = imagecolorallocate ($im, 0, 0, 0);
  10. $gray = Imagecolorallocate ($im, 200, 200, 200);

  11. Fill background

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

  13. Text coordinates

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

  16. Draw an image or text on the canvas

  17. Connect the three arrays.

  18. $strarr = Array_merge (range (1, 9), range (A, z), range (A, z));

  19. Scrambled Array

  20. Shuffle ($strarr);

  21. Array_slice: Take the first few of the array

  22. Join converts an array into a string and breaks it with the first variable
  23. $str = Join ("', Array_slice ($strarr, 0,4));

  24. Put $str into session for easy access to 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 final image or save final image

  30. Header ("Content-type:image/png");
  31. Imagepng ($im);
  32. Releasing Canvas resources
  33. Imagedestroy ($im);
  34. ?>
Copy Code

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. ?>

Copy Code

Method two gets the size of the picture:

    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. ?>

Copy Code

Picture Zoom function:

  1. $imgfile = "Lyf.jpg";

  2. In order to get the wide height of the big picture

  3. $imgarr = getimagesize ($imgfile);

  4. $MAXW = $imgarr [0];

  5. $maxh = $imgarr [1];
  6. $maxt = $imgarr [2];
  7. $MAXM = $imgarr [' MIME '];

  8. In order to change the big picture into a resource

  9. $im = Imagecreatefromjpeg ("lyf.jpg");

  10. Small map Resources

  11. $MINW = 100;
  12. $minh = 400;

  13. Equal scale 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, 0, $MINW, $minh, $maxw, $maxh);

  24. Small graph output

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

  26. Judging 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. Freeing resources
  43. Imagedestroy ($maxim);
  44. Imagedestroy ($minim);
  45. ?>

Copy Code

Picture clipping 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. ?>

Copy Code
  • 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.