PHP Generate image Verification code, _php tutorial

Source: Internet
Author: User

PHP generates a picture verification code,


Let's take a look at the general effect.

So the next step is to put the code directly.

<?php $image = imagecreatetruecolor (100, 30); Create Canvas $imagecolor = Imagecolorallocate ($image, 255, 255, 255); Background color Imagefill ($image, 0, 0, $imagecolor);  Fill background color for ($i =0; $i <4; $i + +) {//Loop 4-digit $fontsize = 6;  $fontcolor = Imagecolorallocate ($image, rand (0, $), rand (0, $), rand (0, 200));  $fontcontent = rand (0, 9);  $x = $i *100/4 + rand (5, 15);  $y = rand (5, 10); Imagestring ($image, $fontsize, $x, $y, $fontcontent, $fontcolor); } for ($i =0; $i <200; $i + +) {//Loop add interference point $pointcolor = Imagecolorallocate ($image, rand (+), rand (50), Rand (  , 200));  $x = rand (1, 99);  $y = rand (1, 29); Imagesetpixel ($image, $x, $y, $pointcolor); } for ($i =0; $i <3; $i + +) {//Loop add interference line $linecolor = Imagecolorallocate ($image, rand (+), rand (+), rand (100  , 250));  $x 1 = rand (1, 25);  $x 2 = rand (50, 75);  $y 1 = rand (1, 15);  $y 2 = rand (15, 25); Imageline ($image, $x 1, $y 1, $x 2, $y 2, $linecolor); } header ("Content-type:image/png"); Imagepng ($image); Imagedestroy ($image);?> 

To share another one can generate Chinese verification code

<?php//1.qi Enable GD Library GD library provides a series of APIs for working with images, using the GD library to process pictures, or to generate images. The GD library on the site is often used to generate thumbnails or to add watermarks to images or generate reports on site data. Session_Start ();//Convert GBK encoded string to UTF-8 string, the first argument is written GBK, because the php file stored in the host encoding is GBK encoding//UTF-8 encoding Browser universal support, strong versatility, here to convert to UTF-8 $str = Iconv ("GBK", "Utf-8", "The mortal life of the Green Mountains and monuments open heart will clouds unroll happiness will always accompany you"); if (!is_string ($str) | |!mb_check_encoding ($STR, " Utf-8 ")) {exit (" not a string or not a Utf-8 ");} $zhongwenku _size;//The length of the string by UTF-8 encoding $zhongwenku_size = Mb_strlen ($str, "UTF-8");//import the above characters into the array $zhongwenku = Array (); for ($i =0; $i < $zhongwenku _size; $i + +) {$zhongwenku [$i] = Mb_substr ($str, $i, 1, "UTF-8");} $result = "";//The four characters to be written on the picture for ($i = 0; $i <4; $i + +) {switch (rand (0, 1)) {case 0: $result. = $zhongwenku [rand (0, $zhongwenku _size-1)];break;case 1: $result. =dechex ( Rand (0,15)); break;}} $_session["Check"] = $result;//Create a true Color picture width 100, high 30$img = Imagecreatetruecolor (100, 30);//assign background color $BG = Imagecolorallocate ( $img, 0, 0, 0);//Assign text color $te = imagecolorallocate ($img, 255,255,255);//write String//imagestring on picture ($img, Rand (3,8), rand (1,70) , Rand (1,10), $resuLT, $te);//In the picture according to the loading font can write a special font imagettftext ($img, A, rand (2, 9), A, a, $te, "Msyh." TTF ", $result); $_session[" Check "] = $result; for ($i =0; $i <3; $i + +) {//$t = Imagecolorallocate ($img, rand (0, 255), Rand ( 0, 255), rand (0, 255));//Draw Line Imageline ($img, 0, rand (0,), Rand (70,100), rand (0,), $te);} $t = Imagecolorallocate ($img, rand (0, 255), rand (0, 255), rand (0, 255));//add Noise for picture for ($i =0; $i <200; $i + +) { Imagesetpixel ($img, rand (1, +), rand (1,), $t);} Send HTTP header information specifies that the Jpegheader ("Content-type:image/jpeg") in the image is sent, and//output JPEG image to browser imagejpeg ($IMG); >

One more instance.

<?php session_start (), function random ($len) {$srcstr = "1a2s3d4f5g6hj8k9qwertyupzxcvbnm";  Mt_srand ();  $strs = "";  for ($i = 0; $i < $len; $i + +) {$strs. = $srcstr [Mt_rand (0, 30)]; } return $strs;}  Randomly generated string $str = random (4);    The width of the captcha picture $width = 50;    The height of the captcha picture $height = 25; Declare the picture format of the layer you want to create @ header ("Content-type:image/png"); Create a layer $im = Imagecreate ($width, $height); Background color $back = Imagecolorallocate ($im, 0xFF, 0xFF, 0xFF); Blur point Color $pix = Imagecolorallocate ($im, 187, 230, 247); Font Color $font = imagecolorallocate ($im, 41, 163, 238); Plot Blur point Mt_srand (); for ($i = 0; $i <; $i + +) {Imagesetpixel ($im, Mt_rand (0, $width), Mt_rand (0, $height), $pix); }//Output character imagestring ($im, 5, 7, 5, $STR, $font); Output Rectangle Imagerectangle ($im, 0, 0, $width-1, $height-1, $font); Output Image imagepng ($im); Imagedestroy ($im); $STR = MD5 ($STR); Select Cookie//setcookie ("Verification", $str, Time () + 7200, "/"); Select session$_session["Verification"] = $STR;? 

The next thing you can do is call it in the page:

If you want to realize "can't see?" Change the "effect, add the following JS to the page

function changing () {  

The above mentioned is the whole content of this article, I hope you can like.

http://www.bkjia.com/PHPjc/1013715.html www.bkjia.com true http://www.bkjia.com/PHPjc/1013715.html techarticle PHP generated image verification code, first to see the approximate effect then the next direct code bar PHP $image = imagecreatetruecolor (100, 30);//create canvas $imagecolor = imagecolor...

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