<?php require_once(''); function verifyImage( $type=1,$length=4,$pixel=0,$line=0,$sess_name="verify"){ session_start(); /*Define length and width*/ $width=80; $height=30; /* Create canvas*/ $image=imagecreatetruecolor($width, $height); /*This function is used to match the color of the figure and is used by other drawing functions. Parameter image represents the handle of the figure. Parameters red, green, and blue are the three primary colors of color, with values ranging from 0 to 255... I define black and white here*/ $white=imagecolorallocate($image, 255, 255, 255); $black=imagecolorallocate($image,0,0,0); /*This function colores the closed rectangular area of the picture. Parameters x1, y1, x2, and y2 are the coordinates of the diagonal lines of the rectangle. Parameter col means the color to be painted*/ imagefilledrectangle($image, 1, 1, $width-2, $height-2, $white); /*buildRandomString function is used to generate a verification code*/ $chars=buildRandomString($type,$length); /*The verification code is given to the session to determine whether the user input is correct*/ $_SESSION[$sess_name]=$chars; /*Define Font Library*/ $fontfiles=array('','','','','','',''); /*Use loops to write verification codes into the picture one by one*/ for($i=0;$i<$length;$i++) { $size=mt_rand(14,18); $angle=mt_rand(-15,15); /*The horizontal coordinate and vertical coordinate of the verification code*/ $x=5+$i*$size; $y=mt_rand(20,26); $color=imagecolorallocate($image,mt_rand(50,190),mt_rand(50,200),mt_rand(50,90)); $fontfile="../font/".$fontfiles[mt_rand(0,count($fontfiles)-1)]; $text=substr($chars,$i,1); /*This function writes TTF (TrueType Fonts) font text into the picture*/ imagettftext($image, $size, $angle, $x, $y, $color, $fontfile, $text); } if($pixel) { for($i=0;$i<50;$i++) { /*This function can draw a little on the picture. Parameters x and y are the coordinates of the point to be drawn, and parameter col represents the color of the point*/ imagesetpixel($image, mt_rand(0,$width-1), mt_rand(0,$height-1), $black); }} if($line) { for($i=0;$i<10;$i++) { $color=imagecolorallocate($image,mt_rand(50,90),mt_rand(50,200),mt_rand(50,90)); /*Draw line segment*/ imageline($image, mt_rand(0,$width-1), mt_rand(0,$height-1), mt_rand(0,$width-1), mt_rand(0,$height-1), $color); } } /*Output in gif*/ header("content-type:image/gif"); /*Create GIF diagram and output it to the web page*/ imagegif($image); /*Release memory associated with image*/ imagedestroy($image); }
<?php function buildRandomString($type=1,$length=4){ if($type==1) { /*join function converts arrays into strings. . The join() function is an alias for the implode() function*/ $chars=join("",range(0,9)); }elseif ($type==2) { /*array_merge function merges array*/ $chars=join("",array_merge(range("a","z"),range("A","Z"))); }elseif($type==3) { $chars=join("",array_merge(range("a","z"),range("A","Z"),range(0,9))); } if($length>strlen($chars)) { exit("The string is not long enough"); } /*Scrap string*/ $chars=str_shuffle($chars); return substr($chars,0,$length); } ?>