public static function show() { $width = self::$width; $height = self::$height; $len = self::$len; $bgcolor = self::$bgcolor; $noise = self::$noise; $noisenum = self::$noisenum; $border = self::$border; $bordercolor = self::$bordercolor; self::$_image = imageCreate($width, $height); $back = self::getcolor($bgcolor); imageFilledRectangle(self::$_image, 0, 0, $width, $height, $back); $size = $width / $len; if ($size > $height) { $size = $height; } $left = ($width - $len * ($size + $size / 10)) / $size; for ($i = 0; $i < $len; $i++) { $randtext = self::vchar(); self::$_code .= $randtext; $textColor = imageColorAllocate(self::$_image, rand(0, 100), rand(0, 100), rand(0, 100)); $font = dirname(__FILE__) . "/3.ttf"; $randsize = rand($size - $size / 5, $size - $size / 10); $location = $left + ($i * $size + $size / 10); imagettftext(self::$_image, $randsize, rand(-18, 18), $location, rand($size - $size / 100, $size + $size / 100), $textColor, $font, $randtext); } if ($noise == true) { self::setnoise(); } self::setCode(self::$_code); //setrawcookie("code",$code,time()+3600,"/"); //setcookie("code", $code, time()+120); $bordercolor = self::getcolor($bordercolor); if ($border == true) { imageRectangle(self::$_image, 0, 0, $width - 1, $height - 1, $bordercolor); } return self::$_image; }
//单个验证码字符高度随机,避免被OCR } } else { //如果设置了背景色和前景色,则不使用随机颜色 imagecolorallocate($this->im, $this->bgColor[0], $this->bgColor[1], $this->bgColor[2]); $randString = $this->randString(); $fontColor = imagecolorallocate($this->im, $this->fontColor[0], $this->fontColor[1], $this->fontColor[2]); for ($i = 0; $i < $this->length; $i++) { imagestring($this->im, 3, $i * 10 + 8, mt_rand(0, 4), $randString[$i], $fontColor); //每个验证码字符高度仍然随机 } } // $this->drawDot();//绘制噪点 // $this->drawLine();//绘制干扰线 imagepng($this->im); imagedestroy($this->im); $randString = strtolower($randString); return md5($randString); //返回MD5加密后的验证码,可直接放入session } } session_start(); $vcode = new Vcode(); $vcode->setBgColor('#cccccc'); $vcode->setFontColor('#ffffff'); $vcode->setLength(4); $_SESSION['vcode'] = $vcode->paint(); // To be encrypted by MD5 ?>
<?php include 'vcode.class.php'; $vcode = new Vcode(80, 30, 4); $_SESSION['code'] = $vcode->getcode(); $vcode->outimg();
{ if (imagetypes() & IMG_GIF) { header("Content-type: image/gif"); imagegif($this->img); } elseif (function_exists("imagejpeg")) { header("Content-type: image/jpeg"); imagegif($this->img); } elseif (imagetypes() & IMG_PNG) { header("Content-type: image/png"); imagegif($this->img); } else { die("No image support in this PHP server"); } } private function createcode() { $codes = "ABCDEFGHIGKLMNOPQRSTUVWXYZ"; $code = ""; for ($i = 0; $i < $this->num; $i++) { $code .= $codes[rand(0, strlen($codes) - 1)]; } return $code; } function __destruct() { imagedestroy($this->img); } } $vcode = new Vcode(80, 30, 4); setcookie("captcha", str_rot13($vcode->getcode()), time() + 60 * 2); $vcode->outimg();
function getVcode() { require_once _LIBS . "Vcode/Vcode.class.php"; $image = Vcode::show(); header("Content-type: image/png"); $image = imagePng($image); imagedestroy($image); exit; }
/** * 验证码图片流 * @param void * @return image obj */ function codeAction() { $code = new Vcode(); //实例化一个对象 $code->doimg(); }
<?php header("content-type:image/png"); session_start(); /************************* * *在这里要实现的功能: *1.实例化验证码类,获得验证码对象; *2.从这个对象获取这个验证码的字符值,把这个值存入SESSION,然后在login.php这个脚本里面 比对POST过来的用户输入的验证码字符值与SESSION里面的字符值是否相同,达到验证的目的; *3.从这个对象里面获得验证码图像,从这里echo出去 * **************************/ //推荐4位长度,50*20尺寸 $vCode = new Vcode(60, 20, 4, 1); $vCodeString = $vCode->achieveString(); $_SESSION['vcode'] = $vCodeString; $vCode->achieveImage(); $vCode = NULL; class Vcode { private $width; private $height; //验证码位数 private $bitNum; //复杂度,1:全数字,2:数字字母组合 private $complexity; //验证码字符值 private $vCodeString; function __construct($width, $height, $bitNum, $complexity) {