<?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