Home > Article > Backend Development > PHP Chinese and English verification code program code_PHP tutorial
php tutorial Chinese and English verification code program code
//The English verification code is relatively simple, do not do hex processing, just use the color value directly. If
session_start();
function rand_create()
{
//Notify the browser that a PNG image will be output
Header("Content-type: image/PNG");
//Prepare the random number generator seed
srand((double)microtime()*1000000);
//Prepare the relevant parameters of the image
$im = imagecreate(62,22);
$black = ImageColorAllocate($im, 0,0,0); //RGB black identifier
$white = ImageColorAllocate($im, 255,255,255); //RGB white identifier
$gray = ImageColorAllocate($ im, 200,200,200); //RGB gray identifier
//Start drawing
imagefill($im,0,0,$gray);
while(($randval=rand()%100000) <10000);{
$_SESSION["Auth_code"] = $randval;
// Draw the four-digit integer verification code into the image
imagestring($im, 5, 10, 3, $randval , $black);
}
//Add interference pixels
for($i=0;$i<200;$i++){
$randcolor = ImageColorallocate($im,rand( 0,255),rand(0,255),rand(0,255));
imagesetpixel($im, rand()%70, rand()%30, $randcolor);
}
//Output verification image
ImagePNG($im);
//Destroy the image identifier
ImageDestroy($im);
}
rand_create();
//Call method< img src=www.bkjia.com.php />
//The Chinese verification code program is as follows
Header("Content-type: image/PNG");
$str = "Set a Chinese here if the first website of China WEB is www.bkjia.com";
$imagesW = 140;
$imagesH = 40;
//
$Auimg = imagecreate($imagesW ,$imagesH);
$bgc = ImageColorAllocate($Auimg,255,255,255);
$font = "heiti.ttf";//Set the font here, you can download any font you want.
$white=imagecolorallocate($Auimg,234,185,95);
imagearc($Auimg, 150, 8, 20, 20, 75, 170, $white);
imagearc($Auimg, 180, 7,50, 30, 75, 175, $white);
imageline($Auimg,20,20,180,30,$white);
imageline($Auimg,20,18,170,50,$white);
imageline($Auimg,25,50,80,50,$white);
$noise_num = 800;
$line_num = 20;
imagecolorallocate($Auimg,0xff,0xff,0xff) ;
$rectangle_color=imagecolorallocate($Auimg,0xAA,0xAA,0xAA);
$noise_color=imagecolorallocate($Auimg,0x00,0x00,0x00);
$font_color=imagecolorallocate($Auimg,0x00, 0x00,0x00);
$line_color=imagecolorallocate($Auimg,0x00,0x00,0x00);
for($i=0;$i<$noise_num;$i++){
imagesetpixel($Auimg ,mt_rand(0,$imagesW),mt_rand(0,$imagesH),$noise_color);
}
for($i=0;$i<$line_num;$i++){
imageline( $Auimg,mt_rand(0,$imagesW),mt_rand(0,$imagesH),mt_rand(0,$imagesW),mt_rand(0,$imagesH),$line_color);
}
$mtRnd=rand (0,strlen($str)-4);
if($mtRnd%2)$mtRnd+=1;
$str = substr($str,$mtRnd,8);
$str = iconv("GB2312","UTF-8",$str);
ImageTTFText($Auimg, 20, 0, 16, 30, $font_color, $font, $str);
ImagePNG($Auimg) ;
ImageDestroy($Auimg);
/*
The common point is that the verification codes are saved with the help of other containers such as session, cookie, etc., otherwise there will be no meaning of verification
This article is written by www .bkjia.com. Please indicate the source when reprinting. Thank you for your cooperation.