自己写验证码的时候在文字定位上拿捏不准
于是把ci验证码主要部分扣了出来,看看,一般般,干扰线是亮点
代码去除了很多判断,简化了很多,显示效果一样
'', 'img_path' => './', 'img_url' => '', 'img_width' => '150', 'img_height' => '30', 'font_path' => './instance/font/Elephant.ttf', 'expiration' => 7200, 'word_length' => 4, 'font_size' => 16, 'img_id' => '', 'pool' => '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ', 'colors' => array( 'background' => array(255,255,255), 'border' => array(153,102,102), 'text' => array(204,153,153), 'grid' => array(255,182,182) ));// 将变量从数组中取出foreach ($defaults as $key => $val){ $$key = $val;}// 生成随机码$word = '';$pool_length = strlen($pool);$rand_max = $pool_length - 1;for ($i = 0; $i < $word_length; $i++){ $word .= $pool[mt_rand(0, $rand_max)];}// 验证码长度$length = strlen($word);//角度$angle = ($length >= 6) ? mt_rand(-($length-6), ($length-6)) : 0;$x_axis = mt_rand(6, (360/$length)-16);$y_axis = ($angle >= 0) ? mt_rand($img_height, $img_width) : mt_rand(6, $img_height);// 创建图片句柄$im = p_w_picpathcreatetruecolor($img_width, $img_height);// 创建颜色foreach ($colors as $key => $values){ $colors[$key] = p_w_picpathcolorallocate($im, $colors[$key][0], $colors[$key][1], $colors[$key][2]);}// 画了个和图片等大的矩形,就是背景ImageFilledRectangle($im, 0, 0, $img_width, $img_height, $colors['background']);// -----------------干扰线--------------------------$theta = 1;$thetac = 7;$radius = 16;$circles = 20;$points = 32;for ($i = 0, $cp = ($circles * $points) - 1; $i < $cp; $i++){ $theta += $thetac; $rad = $radius * ($i / $points); $x = ($rad * cos($theta)) + $x_axis; $y = ($rad * sin($theta)) + $y_axis; $theta += $thetac; $rad1 = $radius * (($i + 1) / $points); $x1 = ($rad1 * cos($theta)) + $x_axis; $y1 = ($rad1 * sin($theta)) + $y_axis; p_w_picpathline($im, $x, $y, $x1, $y1, $colors['grid']); $theta -= $thetac;}// --------------------------------------------------------------// 无字体// ($font_size > 5) && $font_size = 5;// $x = mt_rand(0, $img_width / ($length / 3));// $y = 0;// for ($i = 0; $i < $length; $i++){// $y = mt_rand(0 , $img_height / 2);// p_w_picpathstring($im, $font_size, $x, $y, $word[$i], $colors['text']);// $x += ($font_size * 2);// }// 有字体// x 第一个字符随机在图片的前面一小段,后面依次加一个字符大小的宽度// y 图片的1/2到距离底部3px之间随机($font_size > 30) && $font_size = 30;$x = mt_rand(0, $img_width / ($length / 1.5));$y = $font_size + 2;for ($i = 0; $i < $length; $i++){ $y = mt_rand($img_height / 2, $img_height - 3); p_w_picpathttftext($im, $font_size, $angle, $x, $y, $colors['text'], $font_path, $word[$i]); $x += $font_size;}//画矩形框p_w_picpathrectangle($im, 0, 0, $img_width - 1, $img_height - 1, $colors['border']);// 删除以前保存的图片$now = microtime(TRUE);$current_dir = opendir($img_path);while ($filename = readdir($current_dir)){ if (in_array(substr($filename, -4), array('.jpg', '.png'))&& (str_replace(array('.jpg', '.png'), '', $filename) + 0.1) < $now){ @unlink($img_path.$filename); }}@closedir($current_dir);// 拼接文件路径并保存$img_url = rtrim($img_url, '/').'/';$img_filename = $now.'.jpg';p_w_picpathjpeg($im, $img_path.$img_filename);$img = ' ';// ImageDestroy($im);// return array('word' => $word, 'time' => $now, 'p_w_picpath' => $img, 'filename' => $img_filename);// echo $img;//通知浏览器是png格式header('Content-type:p_w_picpath/jpg');//以png格式输出p_w_picpathjpeg($im);//销毁内存中的图片资源p_w_picpathdestroy($im);