PHP Generate Verification Code

Source: Internet
Author: User

<?PHP/** * PHP generated Verification code * @param $width Canvas width * @param $height Canvas Height * @param $vcodelen captcha length * @param $pointnum number of interference pixels * @param $li Nenum number of interfering lines * * idea: Create Captcha canvas, generate and fill background color, generate CAPTCHA content/interference pixel dots/lines, populate to canvas, output. */    $width= 100; $height= 30; $vcodelen= 4; $pointnum= 200; $linenum= 3; //Create a canvas    $image= Imagecreatetruecolor ($width,$height); //Create a color block    $bgcolor= Imagecolorallocate ($image, 255, 255, 255); //Fill Canvas background colorImagefill ($image, 0, 0,$bgcolor); //Verification Code Content     for($i= 0;$i<$vcodelen;$i++) {         //Font Size        $fontsize= 5; //font color, color within the bounds of the random        $fontcolor= Imagecolorallocate ($image,Rand(0,120),Rand(0,120),Rand(0,120)); $data= ' abcdefghijklmnopqrstuvwxyz0123456789 '; //Verification code content is randomly intercepted within the above string        $fontcontent=substr($data,Rand(0,strlen($data)), 1); //String Display location        $x= ($i*$width/4) +Rand(5,15); $y=Rand(5,10); //string Fill picture//imagestring font size optional 1-5, font larger need to use Imagettftext function (font file required)Imagestring ($image,$fontsize,$x,$y,$fontcontent,$fontcolor); //Imagettftext ($image, $fontsize, 0, $x, $y, $fontcolor, '/font/geneva.dfont ', $fontcontent);    }    //interfering pixel points     for($i= 0;$i<$pointnum;$i++) {         $pointcolor= Imagecolorallocate ($image,Rand(0,120),Rand(0,120),Rand(0,120)); //Canvas fill pixel point functionImagesetpixel ($image,Rand(0,$width),Rand(0,$height),$pointcolor); }    //interfering Lines     for($i= 0;$i<$linenum;$i++) {         $linecolor= Imagecolorallocate ($image,Rand(0,120),Rand(0,120),Rand(0,120)); //Canvas fill Line functionImageline ($image,Rand(0,$width),Rand(0,$height),Rand(0,$width),Rand(0,$height),$linecolor); }    //Picture output Format    Header(' Content-type:image/png '); //output Verification Code pictureImagepng ($image); //destroying the canvasImagedestroy ($image);?>

PHP Generate Verification 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.