PHP Verification Code

Source: Internet
Author: User

<?PHP/** Implementation of verification code in PHP*///Create a canvas$width=500;$height=200;//Create a true Color canvas//$img =imagecreatetruecolor ($width, $height);//Assign Color//$green =imagecolorallocate ($img, 0, 255, 0);// Fill Canvas//imagefill ($img, 0, 0, $green);//Create a background image based on a picture$BG _file= './bg '.Mt_rand(1,3). jpg;$img=imagecreatefromjpeg ($BG _file);//the value of the verification code$chars= ' ABCDEFGHI1234567890 ';$chars _len=strlen($chars);$code _len= 4;//the length of the code value$code= ";//the string of the initial code value for($i= 0;$i<$code _len;$i++){    $rand _index=Mt_rand(0,$chars _len-1); $code.=$chars[$rand _index];}//randomly assigning string colors$str _color=Mt_rand(1, 2) ==1?imagecolorallocate ($img, 0, 0, 0): Imagecolorallocate ($img, 255, 255, 255);//string$font=5;$x=55;$y=2;//Centered Way$img _w=imagesx ($img);$img _h=imagesy ($img);//Font Size$font _w=imagefontwidth ($font);$font _y=imagefontheight ($font);//String Width$code _w=$font _w*$code _len;$code _h=$font _y;$x=($img _w-$code _w)/2;$y=($img _h-$code _h)/2; Imagestring ($img,$font,$x,$y,$code,$str _color);//OutputHeader(' Content-type:image/jpeg '); Imagejpeg ($img);//output to a file if the second parameter is written output to a file, do not write the direct output//imagepng ($img, './cc.png ');//header (' content-type:image/png ');//imagepng ($img) ;?>

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