PHP Image Verification Code function implementation code to implement distorted characters

Source: Internet
Author: User
  1. /**

  2. * Captchaimage ()-Create a captcha picture of distorted characters
  3. * $session the variable name of the session required to create the _name string verification code picture
  4. * $width int to verify the width of the picture, default 120, note: The picture height and width ratio is relatively fixed
  5. * $noise int interferon points, default 0
  6. * $disturb int number of interfering characters, default 0
  7. * $curve bool Increases interference curve, default ture
  8. * */
  9. function Captchaimage ($session _name = ", $width = +, $noise = 0, $disturb = 0, $curve = True) {
  10. $im _x = $width;
  11. $im _y = ceil (0.25 * $width);
  12. $im = Imagecreatetruecolor ($im _x, $im _y);
  13. $text _c = imagecolorallocate ($im, mt_rand (0, 0), Mt_rand (0, +), Mt_rand (+ 100));
  14. $gray _rand = Mt_rand (160, 220);
  15. $buttum _c = imagecolorallocate ($im, $gray _rand, $gray _rand, $gray _rand);
  16. Imagefill ($im, 0, 0, $buttum _c);

  17. Session_Start ();

  18. $text = ";
  19. $characters = ' abcefghjklmnpqrstuvwxyz ';
  20. for ($i = 0, $len = strlen ($characters), $i < 4; $i + +) {
  21. $text. = $characters {rand (0, $len-1)};
  22. if (Isset ($session _name{0}) && session_start ()) {
  23. $_session[$session _name] = $text;
  24. $_session[' captchasessiontime '] = time ();
  25. }
  26. }

  27. $font = ' Arial.ttf ';

  28. $size = Floor (0.2 * $width);

  29. $ttfbox = Imagettfbbox ($size, 0, $font, $text);

  30. $text _width = ABS ($ttfbox [2]-$ttfbox [0]);
  31. $side = Floor (($im _x-$text _width)/2);

  32. $array = Array (-1, 1);

  33. for ($i = 0; $i < strlen ($text); $i + +) {
  34. $p = Array_rand ($array);
  35. $an = $array [$p] * Mt_rand (5, 10); Character Tilt Angle
  36. Imagettftext ($im, $size, $an, $side + $i * ($size-1), $im _y-3, $text _c, $font, $text {$i});
  37. }

  38. $distortion _im = Imagecreatetruecolor ($im _x, $im _y);

  39. Imagefill ($distortion _im, 0, 0, $buttum _c);
  40. $distortion = Floor (0.04 * $width); Degree of distortion
  41. for ($i = 0; $i < $im _x; $i + +) {
  42. for ($j = 0; $j < $im _y; $j + +) {
  43. $rgb = Imagecolorat ($im, $i, $j);
  44. if ((int) ($i + sin ($j/$im _y * 2 * m_pi) * $distortion) <= $im _x && (int) ($i + sin ($j/$im _y * 2 * m_pi) * $d Istortion) >= 0) {
  45. Imagesetpixel ($distortion _im, (int) ($i + sin ($j/$im _y * 2 * M_PI-M_PI * 0.1) * $distortion), $j, $rgb);
  46. }
  47. }
  48. }

  49. if ($disturb > 0) {

  50. $chars = ' ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789 ';
  51. for ($i = 0; $i < $disturb; $i + +) {
  52. Imagestring ($distortion _im, 3, Mt_rand ( -10, $im _x), Mt_rand ( -10, $im _y), $chars [Mt_rand (0, a)], $text _c);
  53. }
  54. }

  55. adding interfering pixels;

  56. if ($noise > 0) {
  57. for ($i = 0; $i < $noise; $i + +) {
  58. $randcolor = Imagecolorallocate ($distortion _im, Mt_rand (0,255), Mt_rand (0,255), Mt_rand (0,255));
  59. Imagesetpixel ($distortion _im, Mt_rand ()% $im _x, Mt_rand ()% $im _y, $randcolor);
  60. }
  61. }

  62. Add Interference curve

  63. if ($curve) {
  64. $rand = Mt_rand (5, 30);
  65. $rand 1 = Mt_rand (15, 25);
  66. $rand 2 = Mt_rand (5, 10);
  67. for ($yy = $rand; $yy <= $rand + 2; $yy + +) {
  68. for ($px = -60; $px <=; $px + +) {
  69. $x = $px/$rand 1;
  70. $y = ($x! = 0)? Sin ($x): $y;
  71. $py = $y * $rand 2;
  72. Imagesetpixel ($distortion _im, $px +, $py + $yy, $text _c);
  73. }
  74. }
  75. }

  76. Set the file header;

  77. Header ("Content-type:image/jpeg");

  78. Export the image to a browser or file in PNG format;

  79. Imagepng ($distortion _im);

  80. Destroys an image, releasing the memory associated with image;

  81. Imagedestroy ($distortion _im);
  82. Imagedestroy ($im);
  83. }

  84. Captchaimage (' Code ', 100); Generate a picture and store random numbers in a session with key code

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.