PHP基本的 验证码代码

header('content-type:image/png');
//创建画布并设置颜色,推荐使用下面那个方法
//$im=imagecreate($width,$height);
$im=imagecreatetruecolor(200,100) or die('Cannot Initialize GD image');

//创建背景颜色
$bg=imagecolorallocate($im,255,255,255);
$line=imagecolorallocatealpha($im,mt_rand(50,255),mt_rand(50,255),mt_rand(50,255),mt_rand(20,60));
imagefill($im,0,0,$bg);

for($i=0;$i<4;$i++){
    $fontSize=mt_rand(40,80);
    $fontColor=imagecolorallocatealpha($im,rand(0,120),rand(0,120),rand(0,120),rand(20,80));
    $data="abcdefghijklmnopqrstuvwyzABCDEFGHIJKLMNOPQRSTUVWYZ0123456789";
    $fontContent=substr($data,rand(0,strlen($data)),1);
    $captcha_code.=$fontContent;
    $x=($i*100/4)+rand(10,30);
    $y=10*rand(2,6);
    imagestring($im,$fontSize,$x,$y,$fontContent,$fontColor);
}


//增加干扰点
for($i=0;$i<200;$i++){
    $pixel=imagecolorallocatealpha($im,mt_rand(50,255),mt_rand(50,255),mt_rand(50,255),mt_rand(20,60));
    imagesetpixel($im,mt_rand(0,200),mt_rand(0,100),$pixel);
}
//增加干扰线
for($i=0;$i<5;$i++){
    imagesetthickness($im,mt_rand(1,5));
    $line=imagecolorallocatealpha($im,rand(0,120),rand(0,120),rand(0,120),rand(40,80));
    imageline($im,mt_rand(0,200),mt_rand(0,100),mt_rand(0,200),mt_rand(0,100),$line);
}


imagepng($im);


//销毁对象,便于系统资源回收
imagedestroy($im);

  这是最基础的 php 验证码代码

posted @ 2015-07-31 18:09  风烨漾  阅读(206)  评论(0编辑  收藏  举报