花了一个上午的时间,终于提取出来了,效果演示:
<?php session_start(); class seccode { var $code ; // 验证码 var $type = 0; // 只能是0 var $width = 70; // 图像宽度 var $height = 21; // 图像高度 var $background = 1; // 1背景随机变换,0不变换 var $adulterate = 1; // 1有干扰线,0没有干扰线 var $ttf = 1; // 只能是1 var $angle = 0; // 1角度随机旋转 0不旋转 var $color = 1; // 1字体颜色随机 0不随机 var $size = 0; // 1字体大小随机 0不随机 var $shadow = 1; // 1文字有阴影 0没有阴影 var $animator = 0; //只能是0 var $fontpath = ''; // 字体路径 var $fontcolor; var $im; function display() { $this->code = $this->gencode(); $_SESSION['vdc'] = $this->code; $this->width = $this->width >= 0 && $this->width <= 200 ? $this->width : 150; $this->height = $this->height >= 0 && $this->height <= 80 ? $this->height : 60; if($this->type < 2 && function_exists('imagecreate') && function_exists('imagecolorset') && function_exists('imagecopyresized') && function_exists('imagecolorallocate') && function_exists('imagechar') && function_exists('imagecolorsforindex') && function_exists('imageline') && function_exists('imagecreatefromstring') && (function_exists('imagegif') || function_exists('imagepng') || function_exists('imagejpeg'))) { $this->image(); } } function gencode() { $arr=array('3','4','5','6','7','8','9','Q','W','E','R','T','Y','U','P','L','K','J','H','G','F','D','S','A','X','C','V','B','N','M');//声明一个数组,中文验证码的来源 $ss1=rand(0,29); $ss2=rand(0,29); $ss3=rand(0,29); $ss4=rand(0,29); $string=$arr[$ss1].$arr[$ss2].$arr[$ss3].$arr[$ss4];//根据随机数,输出四个随机数组元素,组成字符串 return $string; } function fileext($filename) { return trim(substr(strrchr($filename, '.'), 1, 10)); } function image() { $bgcontent = $this->background(); $this->im = imagecreatefromstring($bgcontent); $this->adulterate && $this->adulterate(); $this->ttf && function_exists('imagettftext') || $this->type == 1 ? $this->ttffont() : $this->giffont(); if(function_exists('imagepng')) { header('Content-type: image/png'); imagepng($this->im); } else { header('Content-type: image/jpeg'); imagejpeg($this->im, '', 100); } imagedestroy($this->im); } function background() { $this->im = imagecreatetruecolor($this->width, $this->height); $backgroundcolor = imagecolorallocate($this->im, 255, 255, 255); $backgrounds = $c = array(); if($this->background && function_exists('imagecreatefromjpeg') && function_exists('imagecolorat') && function_exists('imagecopymerge') && function_exists('imagesetpixel') && function_exists('imageSX') && function_exists('imageSY')) { if($handle = @opendir($this->datapath.'background/')) { while($bgfile = @readdir($handle)) { if(preg_match('/\.jpg$/i', $bgfile)) { $backgrounds[] = $this->datapath.'background/'.$bgfile; } } @closedir($handle); } if($backgrounds) { $imwm = imagecreatefromjpeg($backgrounds[array_rand($backgrounds)]); $colorindex = imagecolorat($imwm, 0, 0); $this->c = imagecolorsforindex($imwm, $colorindex); $colorindex = imagecolorat($imwm, 1, 0); imagesetpixel($imwm, 0, 0, $colorindex); $c[0] = $c['red'];$c[1] = $c['green'];$c[2] = $c['blue']; imagecopymerge($this->im, $imwm, 0, 0, mt_rand(0, 200 - $this->width), mt_rand(0, 80 - $this->height), imageSX($imwm), imageSY($imwm), 100); imagedestroy($imwm); } } if(!$this->background || !$backgrounds) { for($i = 0;$i < 3;$i++) { $start[$i] = mt_rand(200, 255);$end[$i] = mt_rand(100, 150);$step[$i] = ($end[$i] - $start[$i]) / $this->width;$c[$i] = $start[$i]; } for($i = 0;$i < $this->width;$i++) { $color = imagecolorallocate($this->im, $c[0], $c[1], $c[2]); imageline($this->im, $i, 0, $i-(isset($angle) ? $angle : 0), $this->height, $color); $c[0] += $step[0];$c[1] += $step[1];$c[2] += $step[2]; } $c[0] -= 20;$c[1] -= 20;$c[2] -= 20; } ob_start(); if(function_exists('imagepng')) { imagepng($this->im); } else { imagejpeg($this->im, '', 100); } imagedestroy($this->im); $bgcontent = ob_get_contents(); ob_end_clean(); $this->fontcolor = $c; return $bgcontent; } function adulterate() { $linenums = $this->height / 10; for($i=0; $i <= $linenums; $i++) { $color = $this->color ? imagecolorallocate($this->im, mt_rand(0, 255), mt_rand(0, 255), mt_rand(0, 255)) : imagecolorallocate($this->im, $this->fontcolor[0], $this->fontcolor[1], $this->fontcolor[2]); $x = mt_rand(0, $this->width); $y = mt_rand(0, $this->height); if(mt_rand(0, 1)) { imagearc($this->im, $x, $y, mt_rand(0, $this->width), mt_rand(0, $this->height), mt_rand(0, 360), mt_rand(0, 360), $color); } else { $linemaxlong = isset($linemaxlong) ? $linemaxlong : 0; $linex = isset($linex) ? $linex : 0; $liney = isset($liney) ? $liney : 0; imageline($this->im, $x, $y, $linex + mt_rand(0, $linemaxlong), $liney + mt_rand(0, mt_rand($this->height, $this->width)), $color); } } } function ttffont() { $seccode = $this->code; $charset = isset($GLOBALS['charset']) ? $GLOBALS['charset'] : ''; $seccoderoot = $this->fontpath; $dirs = opendir($seccoderoot); $seccodettf = array(); while($entry = readdir($dirs)) { if($entry != '.' && $entry != '..' && in_array(strtolower($this->fileext($entry)), array('ttf', 'ttc'))) { $seccodettf[] = $entry; } } $seccodelength = 4; $widthtotal = 0; for($i = 0; $i < $seccodelength; $i++) { $font[$i]['font'] = $seccoderoot.$seccodettf[array_rand($seccodettf)]; $font[$i]['angle'] = $this->angle ? mt_rand(-30, 30) : 0; $font[$i]['size'] = $this->type ? $this->width / 7 : $this->width / 6; $this->size && $font[$i]['size'] = mt_rand($font[$i]['size'] - $this->width / 40, $font[$i]['size'] + $this->width / 20); $box = imagettfbbox($font[$i]['size'], 0, $font[$i]['font'], $seccode[$i]); $font[$i]['zheight'] = max($box[1], $box[3]) - min($box[5], $box[7]); $box = imagettfbbox($font[$i]['size'], $font[$i]['angle'], $font[$i]['font'], $seccode[$i]); $font[$i]['height'] = max($box[1], $box[3]) - min($box[5], $box[7]); $font[$i]['hd'] = $font[$i]['height'] - $font[$i]['zheight']; $font[$i]['width'] = (max($box[2], $box[4]) - min($box[0], $box[6])) + mt_rand(0, $this->width / 8); $font[$i]['width'] = $font[$i]['width'] > $this->width / $seccodelength ? $this->width / $seccodelength : $font[$i]['width']; $widthtotal += $font[$i]['width']; } $x = mt_rand($font[0]['angle'] > 0 ? cos(deg2rad(90 - $font[0]['angle'])) * $font[0]['zheight'] : 1, $this->width - $widthtotal); !$this->color && $text_color = imagecolorallocate($this->im, $this->fontcolor[0], $this->fontcolor[1], $this->fontcolor[2]); for($i = 0; $i < $seccodelength; $i++) { if($this->color) { $this->fontcolor = array(mt_rand(0, 255), mt_rand(0, 255), mt_rand(0, 255)); $this->shadow && $text_shadowcolor = imagecolorallocate($this->im, 255 - $this->fontcolor[0], 255 - $this->fontcolor[1], 255 - $this->fontcolor[2]); $text_color = imagecolorallocate($this->im, $this->fontcolor[0], $this->fontcolor[1], $this->fontcolor[2]); } elseif($this->shadow) { $text_shadowcolor = imagecolorallocate($this->im, 255 - $this->fontcolor[0], 255 - $this->fontcolor[1], 255 - $this->fontcolor[2]); } $y = $font[0]['angle'] > 0 ? mt_rand($font[$i]['height'], $this->height) : mt_rand($font[$i]['height'] - $font[$i]['hd'], $this->height - $font[$i]['hd']); $this->shadow && imagettftext($this->im, $font[$i]['size'], $font[$i]['angle'], $x + 1, $y + 1, $text_shadowcolor, $font[$i]['font'], $seccode[$i]); imagettftext($this->im, $font[$i]['size'], $font[$i]['angle'], $x, $y, $text_color, $font[$i]['font'], $seccode[$i]); $x += $font[$i]['width']; } } } $code = new seccode(); $code->width = 80; // 宽度 $code->height = 25; // 高度 $code->background = 1; // 1背景随机变换,0不变换 $code->adulterate = 1; // 1有干扰线,0没有干扰线 $code->angle = 0; // 1旋转 0不旋转 $code->color = 1; // 1字体颜色随机 0不随机 $code->size = 1; // 1字体大小随机 0不随机 $code->shadow = 1; // 1文字有阴影 0没有阴影 $code->fontpath = $_SERVER['DOCUMENT_ROOT'].'\\vdtest\\'; // 字体路径 $code->display(); ?>
完全就是从discuz中复制出来的,然后把一些没必要的代码都删除了的,本地预览和在网上预览的时候注意要更改那个字体路径的就行,路径中可以放多个ttf字体文件,这样就能随机字体也不同了。
在我要把我提取出来的整合到niunan.org上时,发现在本地预览出错,晕,后来查了下,原来不是验证码的问题,是我本机的WAMP升级了,本机现在是PHP5.3了,但是空间上的还是PHP5.2.9,在判断是否有URL参数的时候我是写
if($_GET["caid])...
但是在PHP5.3中这样写就报错了,应该写成:
if(!empty($_GET['caid']))....
唉。。还得找时间再改改哦。。。