<?php $image=imagecreatetruecolor(100,30); //创建一个100宽30高的底图,默认黑色 $bgcolor=imagecolorallocate($image,255,255,255); //修改颜色。数字对应 rgb 的三个数值。白色 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)); //字体在底图上的颜色。0-120 之间是深色区间 $fontcontent=rand(0,9); //文字内容 $x=($i*100/4)+rand(5,10); //文字在 x 轴上的位置,默认左上角0开始。 //除以 4 是因为有 4 个验证码文字 $y=rand(5,10); //文字在 y 轴上的位置 imagestring($image,$fontsize,$x,$y,$fontcontent,$fontcolor); //文字写入白色底图 } for($i=0;$i<200;$i++){ //生成干扰点,数量在 200 个 $pointcolor=imagecolorallocate($image,rand(50,200),rand(50,200),rand(50,200)); //生成点颜色。这个区间是希望点颜色比文字颜色浅 imagesetpixel($image,rand(1,99),rand(1,29),$pointcolor); //把干扰点绘入图片内。参数23对应图片宽高 } for($i=0;$i<3;$i++){ $linecolor=imagecolorallocate($image,rand(80,220),rand(80,220),rand(80,220)); //生成线的颜色。值越大颜色越浅,最大 255 imageline($image,rand(1,99),rand(1,29),rand(1,99),rand(1,29),$linecolor); //把生成的线绘入图片。两个点确定一条线,所以有四个参数 } header('content-type:image/png'); //header 头声明是 png 图片。 imagepng($image); //调用 png 函数输出 png 图片 imagedestroy($image); //脚本运行完,释放资源 ?>
利用php的GD库生成验证码
最新推荐文章于 2024-07-29 22:50:36 发布