Example #1
0
 /**
  * @param string $length  字符串长度
  * @param string $mode  类型 0:大小写混写 1:纯数字 2:全部大写 3:全小写 4:汉字
  * @param string $type 图像格式
  * @param string $width  宽度
  * @param string $height  高度
  */
 static function buildImageVerify($length = 4, $mode = 1, $width = 48, $height = 22, $verifyName = 'checkcode')
 {
     session_start();
     //生成随机数
     $randval = self::rand_string($length, $mode);
     //写入session 或写入全局变量
     $_SESSION[$verifyName] = $randval;
     //确定图片最适合长度
     $width = $length * 10 + 10 > $width ? $length * 10 + 10 : $width;
     $im = imagecreatetruecolor($width, $height);
     $r = array(225, 255, 255, 223);
     $g = array(225, 236, 237, 255);
     $b = array(225, 236, 166, 125);
     //生成随机数
     $key = mt_rand(0, 3);
     //为图像分配颜色
     $backColor = imagecolorallocate($im, 225, 225, 225);
     //背景色(随机)
     $borderColor = imagecolorallocate($im, 100, 100, 100);
     //边框色
     $pointColor = imagecolorallocate($im, mt_rand(0, 255), mt_rand(0, 255), mt_rand(0, 255));
     //点颜色
     // 画一矩形并填充
     @imagefilledrectangle($im, 0, 0, $width - 1, $height - 1, $backColor);
     // 画一个矩形
     @imagerectangle($im, 0, 0, $width - 1, $height - 1, $borderColor);
     // 分配颜色
     $stringColor = imagecolorallocate($im, mt_rand(0, 200), mt_rand(0, 120), mt_rand(0, 120));
     // 圆弧干扰
     for ($i = 0; $i < 10; $i++) {
         $fontcolor = imagecolorallocate($im, mt_rand(0, 255), mt_rand(0, 255), mt_rand(0, 255));
         imagearc($im, mt_rand(-10, $width), mt_rand(-10, $height), mt_rand(30, 300), mt_rand(20, 200), 55, 44, $fontcolor);
     }
     // 点干扰
     for ($i = 0; $i < 25; $i++) {
         $fontcolor = imagecolorallocate($im, mt_rand(0, 255), mt_rand(0, 255), mt_rand(0, 255));
         imagesetpixel($im, mt_rand(0, $width), mt_rand(0, $height), $pointColor);
     }
     // 不水平画
     for ($i = 0; $i < $length; $i++) {
         $temp = $width / $length;
         $size = mt_rand(4, 5);
         imagestring($im, $size, $i * $temp + 5, mt_rand(1, 5), $randval[$i], $stringColor);
     }
     Seccode::output($im);
     exit;
 }