Php implements verification code generation instance sharing, and php Verification Code instance sharing

Source: Internet
Author: User

Php implements verification code generation instance sharing, and php Verification Code instance sharing

Image. func. php

<? Php require_once ('string. func. php '); function verifyImage ($ type = 1, $ length = 4, $ pixel = 0, $ line = 0, $ sess_name = "verify") {session_start (); /* define the length and width */$ width = 80; $ height = 30;/* Create a canvas */$ image = imagecreatetruecolor ($ width, $ height ); /* this function is used to match the color of the image for other drawing functions. The image parameter indicates the image handle. The red, green, and blue parameters are the three primary colors of color. The value ranges from 0 to 255 .... here I define black and white */$ white = imagecolorallocate ($ image, 255,255,255); $ black = imagecolorallocate ($ image, 0 ); /* This function colors the closed rectangular area of the image. Parameters x1, y1, x2, and y2 are the coordinate of the diagonal line of the rectangle, respectively. The col parameter indicates the color to be coated */imagefilledrectangle ($ image, 1, 1, $ width-2, $ height-2, $ white ); /* The buildRandomString function is used to generate a Verification Code */$ chars = buildRandomString ($ type, $ length ); /* Send the verification code to the session to determine whether the user input is correct */$ _ SESSION [$ sess_name] = $ chars;/* define the library */$ fontfiles = array ('msyh. ttf', 'msyhbd. ttf', 'simsun. ttc ', 'simyou. ttf', 'sthupo. ttf', 'stkaiti. ttf', 'stliti. ttf');/* use a loop to write the verification code to the image one by one */for ($ I = 0; $ I <$ length; $ I ++) {$ size = mt_rand (14,18 ); $ Angle = mt_rand (-15, 15);/* the horizontal and vertical coordinates of the Verification Code */$ x = 5 + $ I * $ size; $ y = mt_rand (20, 26 ); $ color = imagecolorallocate ($ image, mt_rand (50,190), mt_rand (50,200), mt_rand (); $ fontfile = ".. /font /". $ fontfiles [mt_rand (0, count ($ fontfiles)-1)]; $ text = substr ($ chars, $ I, 1 ); /* This function writes TTF (TrueType Fonts) text to an image */imagettftext ($ image, $ size, $ angle, $ x, $ y, $ color, $ fontfile, $ text) ;}if ($ pixel) {for ($ I = 0; $ I <50; $ I ++) {/* this function can Draw a bit on the image. The x and y parameters are the coordinates of the point to be painted. The col parameter represents the color of the point */imagesetpixel ($ image, mt_rand (0, $ width-1), mt_rand (0, $ height-1), $ black) ;}if ($ line) {for ($ I = 0; $ I <10; $ I ++) {$ color = imagecolorallocate ($ image, mt_rand (50,90), mt_rand (50,200), mt_rand (50,90);/* draw line segments */imageline ($ image, mt_rand (0, $ width-1), mt_rand (0, $ height-1), mt_rand (0, $ width-1), mt_rand (0, $ height-1 ), $ color) ;}}/* output in gif format */header ("content-type: image/gif "); /* Create a GIF image and output it to the webpage */imagegif ($ image);/* release the memory associated with the image */imagedestroy ($ image );}

String. func. php

<? Php function buildRandomString ($ type = 1, $ length = 4) {if ($ type = 1) {/* join function converts an array to a string .. The join () function is the alias of the implode () function */$ chars = join ("", range ();} elseif ($ type = 2) {/* array_merge function merge array */$ chars = join ("", array_merge (range ("a", "z"), range ("", "Z");} elseif ($ type = 3) {$ chars = join ("", array_merge (range ("a", "z "), range ("A", "Z"), range (0, 9);} if ($ length> strlen ($ chars )) {exit ("the string length is not enough");}/* disrupt the string */$ chars = str_shuffle ($ chars); return substr ($ chars, 0, $ length) ;}?>

Articles you may be interested in:
  • Php image Verification Code
  • PHP dynamically and randomly generates Verification Code
  • PHP Verification Code (latest modification, fully customized! )
  • A beautiful php Verification Code Class (SHARE)
  • Solution to "the image cannot be displayed due to its own mistakes" when PHP generates the verification code
  • PHP generates image verification codes and click to switch instances
  • Php verification code generation function
  • Thinkphp3.2 click to refresh to generate the verification code
  • Php generates verification codes, thumbnails, and watermark image class sharing

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.