码迷,mamicode.com
首页 > 其他好文 > 详细

一个简单的验证码

时间:2014-10-03 16:01:25      阅读:236      评论:0      收藏:0      [点我收藏+]

标签:des   color   java   for   c   on   r   ad   ef   

//设置画布宽度
$image = imagecreatetruecolor(100, 50);
//画布颜色
$bgcolor = imagecolorallocate($image, 255, 255, 255);

// imagefill — 区域填充
imagefill($image, 0, 0, $bgcolor);

//四个随机验证码
// for($i=0;$i<4;$i++){
//  $fontsize = 6;//字体大小
//  $fontcolor = imagecolorallocate($image, rand(0,120), rand(0,120), rand(0,120)); //字体颜色
//  $fontcontent = rand(0,9);//随机数字
//  //定位
//  $x = ($i * 100 / 4)+ rand(5,10); //
//  $y = rand(5,10);
//  imagestring($image, $fontsize, $x, $y, $fontcontent, $fontcolor);
// }
//包含英文字母的验证码
for($i=0;$i<4;$i++){
    $fontsize = 6;
    $data =‘123456789abcdefghijklmnopqrstuvwxy‘ ;//建立字典
    $fontcolor = imagecolorallocate($image, rand(0,120), rand(0,120), rand(0,120));
    $fontcontent = substr($data, rand(0,strlen($data)),1);
    $x = ($i * 100 / 4)+ rand(5,10); 
    $y = rand(5,10);
    imagestring($image, $fontsize, $x, $y, $fontcontent, $fontcolor);
}
//增加干扰点
for($i=0;$i<200;$i++){
    $pointcolor = imagecolorallocate($image, rand(50,120), rand(50,120), rand(50,120));
    imagesetpixel($image, rand(0,99), rand(0,29), $pointcolor);
}
//增加干扰线
for ($i=0; $i < 4; $i++) { 
    $linecolor = imagecolorallocate($image, rand(80,220), rand(80,180), rand(90,250)); //线的颜色是随机的
    imageline($image, rand(0,99), rand(0,29), rand(0,99), rand(0,29), $linecolor);
}
header("content-type:image/png");

//输出图像
imagepng($image);
//销毁图像
imagedestroy($image);

一个简单的验证码

标签:des   color   java   for   c   on   r   ad   ef   

原文地址:http://my.oschina.net/kopa/blog/324036

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!