Home > Article > Backend Development > Use gd image verification code to generate and save in 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);
```//画画布$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);
').addClass('pre-numbering').hide();
$(this).addClass('has-numbering').parent().append($numbering);
for (i = 1; i ').text(i));
};
$numbering.fadeIn(1700);
});
});
The above introduces the use of gd image verification code generation and saving in PHP, including aspects of the content. I hope it will be helpful to friends who are interested in PHP tutorials.