时间:2019-12-05
浏览量:
php创建图像验证码源码:
<?php /** * 图像验证码 * 1、生成验证码字符串 * 2、创建画布大小,填充颜色 * 3、创建杂色到画布 * 4、创建干扰线到画布 * 5、创建字符串到画布上 * 6、输出图像 */ //0-9 a-z A-Z $arr = array_merge(range(0,9),range('a','z'),range('A','Z')); //打乱数组 shuffle($arr); //从数组中从下标0开始,取出4个元素 $arrlen = array_splice($arr,0,4); //将数组转成字符串 $text = implode($arrlen); //创建画布 $width = 120; $height = 40; $image = imagecreatetruecolor($width, $height); $reccolor = imagecolorallocate($image,mt_rand(130,255),mt_rand(130,255),mt_rand(130,255)); imagefilledrectangle($image,0,0,$width,$height,$reccolor); //添加杂色到画布 for ($i=1;$i<=450;$i++) { $pixcolor = imagecolorallocate($image,mt_rand(60,160),mt_rand(60,160),mt_rand(60,160)); imagesetpixel($image,mt_rand(0,$width),mt_rand(0,$height), $pixcolor); } //将字符串写到画布 $ttfpath = dirname(__FILE__).'/arial.ttf'; $ttfcolor = imagecolorallocate($image,mt_rand(0,120),mt_rand(0,120),mt_rand(0,120)); imagettftext($image,32,0,5,35, $ttfcolor, $ttfpath, $text); //创建干扰线到画布 for ($i=1;$i<=15;$i++) { $linecolor = imagecolorallocate($image,mt_rand(30,150),mt_rand(30,150),mt_rand(30,150)); imageline($image,mt_rand(0,$width),mt_rand(0,$height),mt_rand(0,$width),mt_rand(0,$height),$linecolor); } //输出图像 header("Content-Type:image/png"); imagepng($image); imagedestroy($image);
RELATED RECOMMEND