自己学建网站,网站为什么要seo?,中国建设银行中国网站,建设银行泰安分行网站PHP 实现图片验证码
步骤
产生随机字符串创建一张简单的图片#xff0c;设置背景色#xff0c;文本色再加一些干扰线#xff0c;干扰素输出图像销毁图像资源 ?php//PHP生成图片验证码class VerifyImage{private $verifyCode;private $image;//生成随机字串private fu…PHP 实现图片验证码
步骤
产生随机字符串创建一张简单的图片设置背景色文本色再加一些干扰线干扰素输出图像销毁图像资源 ?php//PHP生成图片验证码class VerifyImage{private $verifyCode;private $image;//生成随机字串private function createCode($type1,$length4){if ($type 1) {$verifyCode implode(, range(0, 9));}elseif ($type 2) {$verifyCode implode(, array_merge(range(a, z),range(A, Z)));}else{$verifyCode implode(, array_merge(range(a, z),range(0, 9),range(A, Z)));}//判断生成字符是否符合要求if (strlen($verifyCode)$length) {return false;}//打乱字符串$verifyCode str_shuffle($verifyCode);return substr($verifyCode, 0,$length);}//生成图片,并加入干扰线干扰素public function createImage($type1,$length4,$width 80,$height 40){$verifyCode $this-createCode($type,$length);$image imagecreatetruecolor($width, $height);//白色背景$white imagecolorallocate($image, 255, 255, 255);//字体颜色$fontStyle imagecolorallocate($image, rand(0, 255),rand(0, 255), rand(0, 255));;imagefill($image, 0, 0, $white);imagestring($image, 5, 10, 10, $verifyCode, $fontStyle);//加入干扰点for($i 0; $i 80; $i) { $color imagecolorallocate($image, rand(0, 255), rand(0, 255), rand(0, 255)); imagesetpixel($image, rand(0, $width), rand(0,$height), $color); }//干扰线 for($i 0; $i 5; $i) { $color imagecolorallocate($image, rand(0, 255), rand(0, 255), rand(0, 255)); imageline($image, rand(0, $width), rand(0, $height), rand(0, $width), rand(0, $height), $color); } //输出图片header(Content-type: image/png); imagepng($image);//释放资源imagedestroy($image);}
}$im new VerifyImage();;
$im-createImage(3);?