How php generates an image Verification Code,

Source: Internet
Author: User

How php generates an image Verification Code,

This article describes how to generate an image verification code in php for your reference. The content is as follows:

First, a fixed number of characters are randomly extracted from the specified character set, and painted on the canvas in an irregular way. Then, some interference points and interference elements are added as appropriate, and the image is output, A brand new verification code is complete.

The front-end code is as follows:

<! DOCTYPE html> 

The php script file verification code is as follows:

<? Php session_start (); // enable session record verification code data vCode (4, 15); // set the number of characters in the verification code and the Image Base Width // Number of vCode characters, font size, function vCode ($ num = 4, $ size = 20, $ width = 0, $ height = 0 ){! $ Width & $ width = $ num * $ size x 4/5 + 15 ;! $ Height & $ height = $ size + 10; // set the verification code character set to $ str = "23456789 abcdefghijkmnpqrstuvwxyzABCDEFGHIJKLMNPQRSTUVW"; // Save the obtained Verification code $ code = ''; // randomly select the character for ($ I = 0; $ I <$ num; $ I ++) {$ code. = $ str [mt_rand (0, strlen ($ str)-1)];} // create a verification code canvas $ im = imagecreatetruecolor ($ width, $ height ); // background color $ back_color = imagecolorallocate ($ im, mt_rand (0,100), mt_rand (0,100), mt_rand (0,100); // text color $ text_color = imagecoloralloc Ate ($ im, mt_rand (100,255), mt_rand (100,255), mt_rand (100,255); imagefilledrectangle ($ im, 0, 0, $ width, $ height, $ back_color ); // draw interference lines for ($ I = 0; $ I <5; $ I ++) {$ font_color = imagecolorallocate ($ im, mt_rand (0,255 ), mt_rand (0,255), mt_rand (0,255); imagearc ($ im, mt_rand (-$ width, $ width), mt_rand (-$ height, $ height), mt_rand (30, $ width * 2), mt_rand (20, $ height * 2), mt_rand (0,360), mt_rand (0,360), $ font_color);} // draw interference points for ($ I = 0; $ I <50; $ I ++) {$ font_color = imagecolorallocate ($ im, mt_rand (0,255), mt_rand (0,255), mt_rand (0,255); imagesetpixel ($ im, mt_rand (0, $ width), mt_rand (0, $ height ), $ font_color);} // random rotation angle array $ array = array (5, 4, 3, 2, 1, 0,-1,-2,-3,-4,-5 ); // output verification code // imagefttext (image, size, angle, x, y, color, fontfile, text) @ imagefttext ($ im, $ size, array_rand ($ array), 12, $ Size + 6, $ text_color, 'c: \ WINDOWS \ Fonts \ simsun. ttc ', $ code); $ _ SESSION ["VerifyCode"] = $ code; // no-cache will access the server upon each request // max-age will access the server upon another 1 s request, and must-revalidate will access the server upon the first request, the server will not be accessed later // header ("Cache-Control: max-age = 1, s-maxage = 1, no-cache, must-revalidate "); header ("Cache-Control: no-cache"); header ("Content-type: image/png; charset = gb2312 "); // convert the image to png format imagepng ($ im); imagedestroy ($ im );}?>

:

The above is all the content of this article. I hope it will help you learn php programming and learn how to compile php image verification code.

Articles you may be interested in:
  • PHP uses CURL to simulate logon to websites with verification Codes
  • Captcha Verification Code instance implemented by php
  • Improvement of PHP + jQuery registration module (1): Saving the verification code to the SESSION
  • PHP graphic verification code sharing
  • Share a classic PHP Verification Code Class
  • Perfect solution to the problem that thinkphp Verification Code fails to be displayed
  • ThinkPHP on the verification code page, the solution is garbled.
  • Text message Verification Code sending class for IMEI restrictions implemented by php
  • Php generates an image verification code-five verification codes are attached
  • Php sends text message verification code to complete registration

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.