php中动态生成不同字体颜色的png格式验证图片

轉載於 : [url]http://dev.csdn.net/article/80/80154.shtm[/url]

<?
session_start();
//生成验证码图片
Header("Content-type: image/PNG");
$authnum=$_SESSION["authnum"];
srand((double)microtime()*1000000);
$len = strlen($authnum)*12+10;
$im = imagecreate($len,25);
$color[]= array ();
$color[1] = ImageColorAllocate($im, 0x00,0x00,0x00);
$color[2] = ImageColorAllocate($im, 0x00,0x00,0xff);
$color[3] = ImageColorAllocate($im, 0xff,0x33,0x00);
$color[4] = ImageColorAllocate($im, 0x00,0x00,0x99);
$color[5] = ImageColorAllocate($im, 0xff,0x00,0xff);
$color[6] = ImageColorAllocate($im, 0x99,0x66,0xff);
$color[7] = ImageColorAllocate($im, 0x00,0x99,0x99);
$color[8] = ImageColorAllocate($im, 0xff,0xff,0x00);

if($background)
{
$r = substr($background, 0, 2);
$g = substr($background, 2, 2);
$b = substr($background, 4, 2);
$bg = ImageColorAllocate($im, hexdec("0x".$r),hexdec("0x".$g),hexdec("0x".$b));
}
else
{
$bg = ImageColorAllocate($im, 0xcc,0xcc,0xff);
}
imagefill($im, 0, 0, $bg);

for($i=0,$x=5;$i<strlen($authnum);$i++)
{
imagestring($im, 5, $x, rand(2,8), $authnum[$i], $color[rand(1,8)]);
$x+=12;
}
for($i=0;$i<100;$i++) //加入干扰象素
{
$randcolor = ImageColorallocate($im,rand(100,255),rand(100,255),rand(100,255));
imagesetpixel($im, rand()%$len , rand()%30 , $randcolor);
}
ImagePNG($im);
ImageDestroy($im);
?>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值