您的位置:首页 > 编程语言 > PHP开发

php中利用gd图片验证码生成和保存

2016-03-02 16:00 666 查看
版权声明:本文为博主原创文章,欢迎转载。 https://blog.csdn.net/wwx920395962/article/details/50780711
<?php
//画画布
$img = imagecreatetruecolor(100, 40);
//三种颜色
$black = imagecolorallocate($img, 0x00, 0x00, 0x00);
$green = imagecolorallocate($img, 0x00, 0xFF, 0x00);
$white = imagecolorallocate($img, 0xFF, 0xFF, 0xFF);
//填充白色
imagefill($img,0,0,$white);
//生成随机的验证码
$code = '';
for($i = 0; $i < 4; $i++) {
$code .= rand(0, 9);
}
imagestring($img, 5, 10, 10, $code, $black);
//加入噪点干扰
for($i=0;$i<50;$i++) {
imagesetpixel($img, rand(0, 100) , rand(0, 100) , $black);
imagesetpixel($img, rand(0, 100) , rand(0, 100) , $green);
}
//png输出验证码jpeg输出到当前文件夹
header("content-type: image/png");
imagepng($img);
imagejpeg($img, "./ok.jpeg", 75);
imagedestroy($img);

```<?php
//画画布
$img = imagecreatetruecolor(100, 40);
//三种颜色
$black = imagecolorallocate($img, 0x00, 0x00, 0x00);
$green = imagecolorallocate($img, 0x00, 0xFF, 0x00);
$white = imagecolorallocate($img, 0xFF, 0xFF, 0xFF);
//填充白色
imagefill($img,0,0,$white);
//生成随机的验证码
$code = '';
for($i = 0; $i < 4; $i++) {
$code .= rand(0, 9);
}
imagestring($img, 5, 10, 10, $code, $black);
//加入噪点干扰
for($i=0;$i<50;$i++) {
imagesetpixel($img, rand(0, 100) , rand(0, 100) , $black);
imagesetpixel($img, rand(0, 100) , rand(0, 100) , $green);
}
//png输出验证码jpeg输出到当前文件夹
header("content-type: image/png");
imagepng($img);
imagejpeg($img, "./ok.jpeg", 75);
imagedestroy($img);
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: