实现数字验证码
在底图上显示随机数字
for循环
变色验证码
随机的点——干扰元素
实现数字验证码
在底图上显示随机数字
for循环
变色验证码
随机的点——干扰元素
<?php
//phpinfo();
//生成验证码底图
$image=@imagecreatetruecolor(100, 30);//返回一个黑色的图片
$text_color = imagecolorallocate($image, 255, 255, 255);
// $bgcolor=imagecolorallocate($image,255,255,255);
// imagefill($image,0,0,$bgcolor);
// header('content-type: image/png');
// imagedestroy($image);
imagefill($image,0,0,$text_color);//区域填充
for($i=0;$i<4;$i++)//利用for循环生成四位数字
{
$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<200;$i++)
{
$pointcolor=imagecolorallocate($image,rand(50,200),rand(50,200),rand(50,200));
//生成随机点干扰颜色(较浅),给恶意破解程序增加难度
imagesetpixel($image,rand(1,99),rand(1,99),$pointcolor);
}
header ('Content-Type: image/png');
imagepng($image);
imagedestroy($image);
https://bbs.csdn.net/topics/390932524
这表示你的程序前面有输出,<?php 前有空格、空行、文件有BOM头
增加干扰元素
<?php /*php之验证制作*/ //1、创建一个真空彩色图像 $image=imagecreatetruecolor(100,30); //2、为图像分配颜色 $bgcolor=imagecolorallocate($image,255,255,255); //3、填充图像 imagefill($image,0,0,$bgcolor); //4、在图像中添加随机数字 $code=''; $content=''; for($i=0;$i<4;$i++){ $fontsize=20; $code=rand(0,9); $fontcolor=imagecolorallocate($image,0,0,0); $x=$i*(100/4)+rand(5,10); $y=rand(5,15); $content.=$code; imagestring($image,$fontsize,$x,$y,$code,$fontcolor); } //增加干扰元素 for($i=0;$i<400;$i++){ $pointcolor=imagecolorallocate($image,rand(0,200),rand(0,200),rand(0,200)); imagesetpixel($image,rand(0,100),rand(0,30),$pointcolor); } session_start(); $_SESSION["content"]=$content; header('content-type:image/png'); imagepng($image); imagedestroy($image);