- require_once('string.func.php');
- 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 graphics and can be used by other drawing functions. The parameter image represents the handle of the graphic. The parameters red, green, and blue are the three primary colors of color, with values from 0 to 255....I define black and white here*/
- $white=imagecolorallocate($image, 255, 255, 255);
- $black=imagecolorallocate ($image,0,0,0);
- /*This function colors the closed rectangular area of the image. The parameters x1, y1 and x2, y2 are the coordinates of the diagonal lines of the rectangle respectively. The parameter col represents the color to be painted */
- imagefilledrectangle($image, 1, 1, $width-2, $height-2, $white);
- /*the buildRandomString function is used to generate a verification code*/
- $ chars=buildRandomString($type,$length);
- /*Give the verification code to the session to determine whether the user input is correct*/
- $_SESSION[$sess_name]=$chars;
- /*Define the font library*/
- $fontfiles=array('msyh.ttf','msyhbd.ttf','simsun.ttc','SIMYOU.TTF','STHUPO.TTF','STKAITI.TTF','STLITI.TTF');
-
- /*Use a loop to write the 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 abscissa and ordinate 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 image*/
- imagettftext($image, $size, $ angle, $x, $y, $color, $fontfile, $text);
- }
- if($pixel)
- {
- for($i=0;$i<50;$i++)
- {
- /*this The function can be plotted as a point on the picture. The parameters x and y are the coordinates of the point to be drawn, and the 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 segments*/
- 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 form*/
- header("content-type:image/gif");
- /*Create a GIF image and output it Go to the web page*/
- imagegif($image);
- /*Release the memory associated with image*/
- imagedestroy($image);
- }
Copy the code
- function buildRandomString($type=1,$length=4){
- if($type==1)
- {
- /*join function converts the array into a string. . The join() function is an alias of the implode() function*/
- $chars=join("",range(0,9));
- }elseif ($type==2) {
- /*array_merge function merges arrays*/
- $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("String length is not enough");
- }
- /*Shuffle the string*/
- $chars=str_shuffle($chars);
- return substr($chars,0,$length) ;
-
- }
- ?>
Copy code
|