随即中文验证码
文字无法显示
无字典版
<?php
session_start();
$image =imagecreatetruecolor(200,60);
$bgcolor=imagecolorallocate($image,255,255,255);//#ffffff
imagefill($image,0,0,$bgcolor);
$fontface='simfang.ttf';//这里我用仿宋
$strdb=array('慕','课','网','赞');
//1.画数字(字典)
$captcha_code='';
for($i=0;$i<4;$i++){
$fontcolor = imagecolorallocate($image, mt_rand(0,120), mt_rand(0,120), mt_rand(0,120));
$cn=$strdb[$i];
$captcha_code.=$cn;
imagettftext($image,mt_rand(20,24),mt_rand(-60,60),($i*50+10),mt_rand(35,40),$fontcolor,$fontface,$cn);
}//错误:这里之前少了一个参数“字体大小”;字体大小是20-24,所以i=0的时候X的值太小的话有可能没边,要考虑到。
$_SESSION['authcode']=$captcha_code;
//2.画干扰点
for($i=0;$i<200;$i++){
$pointcolor=imagecolorallocate($image,mt_rand(50,200),mt_rand(50,100),mt_rand(50,200));
imagesetpixel($image,mt_rand(1,199),mt_rand(1,59),$pointcolor);
}
//3.画干扰线
for($i=0;$i<3;$i++){
$linecolor=imagecolorallocate($image,mt_rand(80,220),mt_rand(80,220),mt_rand(80,220));
imageline($image,mt_rand(1,199),mt_rand(1,59),mt_rand(1,199),mt_rand(1,59),$linecolor);
}
header('content-type:image/png');
imagepng($image);
//end
imagedestroy();
<?php
session_start();
$image =imagecreatetruecolor(200,60);
$bgcolor=imagecolorallocate($image,255,255,255);//#ffffff
imagefill($image,0,0,$bgcolor);
$fontface='simfang.ttf';//这里我用仿宋
$str="的一了是我不在人们有来他这上着个地到大里说去子得也和那要下看天时过出小么起你都把好还多没为又可家学只以主会样年想能生同老中从自面前头到它后然走很像见两用她国动进成回什边作对开而已些现山民候经发工向事命给长水几义三声于高正妈手知理眼志点心战二问但身方实吃做叫当住听革打呢真党全才四已所敌之最光产情路分总条白话东席次亲如被花口放儿常西气五第使写军吧文运在果怎定许快明行因别飞外树物活部门无往船望新带队先力完间却站代员机更九您每风级跟笑啊孩万少直意夜比阶连车重便斗马哪化太指变社似士者干石满决百原拿群究各六本思解立河爸村八难早论吗根共让相研今其书坐接应关信觉死步反处记将千找争领或师结块跑谁草越字加脚紧爱等习阵怕月青半火法题建赶位唱海七女任件感准张团屋爷离色脸片科倒睛利世病刚且由送切星晚表够整认响雪流未场该并底深刻平伟忙提确近亮轻讲农古黑告界拉名呀土清阳照办史改历转画造嘴此治北必服雨穿父内识验传业菜爬睡兴";//这里字间不能有空格,有在外面删掉再拿进来,注意视图切自动换行
$strdb=str_split($str,3);
/* header('content-type:text/html;charset=utf-8');
var_dump($strdb);//这种打印并中断的方法很实用,要注意
die();//少一个分号都能把自己给搞蒙!! */
//1.画数字(字典)
$captcha_code='';
for($i=0;$i<4;$i++){
$fontcolor = imagecolorallocate($image, mt_rand(0,120), mt_rand(0,120), mt_rand(0,120));
$index=mt_rand(0,count($strdb)-1);
$cn=$strdb[$index];
$captcha_code.=$cn;
imagettftext($image,mt_rand(20,24),mt_rand(-60,60),($i*50+10),mt_rand(35,40),$fontcolor,$fontface,$cn);
}//错误:这里之前少了一个参数“字体大小”;字体大小是20-24,所以i=0的时候X的值太小的话有可能没边,要考虑到。
$_SESSION['authcode']=$captcha_code;
//2.画干扰点
for($i=0;$i<200;$i++){
$pointcolor=imagecolorallocate($image,mt_rand(50,200),mt_rand(50,100),mt_rand(50,200));
imagesetpixel($image,mt_rand(1,199),mt_rand(1,59),$pointcolor);
}
//3.画干扰线
for($i=0;$i<3;$i++){
$linecolor=imagecolorallocate($image,mt_rand(80,220),mt_rand(80,220),mt_rand(80,220));
imageline($image,mt_rand(1,199),mt_rand(1,59),mt_rand(1,199),mt_rand(1,59),$linecolor);
}
header('content-type:image/png');
imagepng($image);
//end
imagedestroy();
session_start(); header('content-type:text/html;charset=utf-8'); // 定义字符集路径 $fontface = "SIMYOU.TTF"; // 定义字符库 $str = "中文字库"; // 如果字符必须保证为 UTF-8 编码格式, 如果不是,需要将其转换UTF-8 编码 // iconv ( string $in_charset , string $out_charset , string $str ) 将 $in_charset 编码的 $str 字符串按要求的 $out_charset 字符编码来转换 并返回 // $str = iconv("GBK", "UTF-8", $text); // 将字符库中的字符截取成数组 // str_split(string,length) 把字符串分割到数组中。string 要分割的字符串。length 每个数组元素的长度。默认是 1。 // 因为 utf-8 编码的字符 是三个字节一个汉字,所以 length = 3. $strdb = str_split($str,3); for ($i=0; $i < 4; $i++) { // 0-120 为深色区,能更好的在浅色背景中识别验证码 $fontcolor = imagecolorallocate($image, rand(0,120), rand(0,120), rand(0,120)); $index = rand(0,count($strdb)-1); $cn = $strdb[$index]; $captch .=$cn; // imagettftext ( 画布资源 , 字体大小 , 倾斜角度 , X轴位置 , Y轴位置 , 字体颜色 , 字符集路径 , 文字内容 ) 图像写入文本 imagettftext($image, rand(14,18), mt_rand(-60,60), (40*$i+20), mt_rand(30,35), $fontcolor, $fontface, $cn); } // 保存验证码,并全部转换在小写 $_SESSION['authcode'] = strtolower($captch);
header("content-type:text/html;charset=utf-8");代表浏览器输出文本类型