现在的位置: 首页 > 综合 > 正文

PHP简单的图片验证码

2013年10月01日 ⁄ 综合 ⁄ 共 955字 ⁄ 字号 评论关闭

如有代码不正确,请在品论中指出,谢谢!

<?php
$img=imagecreate(70,25);//创建画布
$background=imagecolorallocate($img,100,125,255);//画布背景颜色
$black=imagecolorallocate($img,0,0,0);//创建黑色
$white=imagecolorallocate($img,255,255,255);//创建白色

for ($i=1;$i<=100;$i++){
 imagestring($img, 1 , rand(0,100) , rand(0,50) , './#' ,imagecolorallocate($img, rand(200,225), rand(225,250), rand(180,225)));
}

//创建随即字符串

function creaStr($len){
       $arr1=range(0,9);
       $arr2=range(a,z);
       $arr3=range(A,Z);
       $arr=array_merge($arr1,$arr2,$arr3);
       $str="";
       for($i=0;$i<$len;$i++){

           str_shuffle($arr);//打乱数组$arr
           $str.=$arr[rand(0,61)];
       }
      return $str;
}

$str=creaStr(4);
for($i=0,$j=5;$i<4;$i++){ //$j画布的Y轴坐标
     imagestring($img,5,$j,rand(0,11),$str[$i],imagecolorallocate($img,rand(0,100),rand(0,100),rand(0,100)));//把字符串画在画布上

    $j+=15;
}
imagerectangle($img,0,0,70-1,25-1,$black);//创建画布边框
header('Content-type:image/png');//网页显示内容的类型
imagepng($img);//创建png格式图片
imagedestroy($img);//关闭文件流
?>

抱歉!评论已关闭.