createImage($code); return $captcha->getText(); } /** * setWidth * 设置宽度 * * @param integer $num * @return void */ public function setWidth($num) { $this->width = $num; } /** * setHeight * 设置高度 * * @param integer $num * @return void */ public function setHeight($num) { $this->height = $num; } /** * 生成图片 * * @param strng $code 验证码 * @return void */ public function createImage($code = null) { if (!$code) { $code = $this->getText(); } $this->renderImage($code); } /** * getText * 获取验证码 * * @return string 验证码 */ public function getText() { if (!$this->code) { $this->code = $this->generateVerifyCode(); } return $this->code; } /** * generateVerifyCode * 产生一个随机验证码 * * @return string 验证码 */ protected function generateVerifyCode() { if($this->minLength < 3) { $this->minLength = 3; } if($this->maxLength > 20) { $this->maxLength = 20; } if($this->minLength > $this->maxLength) { $this->maxLength = $this->minLength; } $length = rand($this->minLength, $this->maxLength); $letters = 'bcdfghjkmnpqrstvwxyz23456789'; $vowels = 'aeiu'; $code = ''; for($i = 0; $i < $length; ++$i) { if($i % 2 && rand(0, 10) > 2 || !($i % 2) && rand(0, 10) > 9) { $code .= $vowels[rand(0, 3)]; } else { $code .= $letters[rand(0, 27)]; } } return $code; } /** * renderImage * 生成验证码图片 * * @param string $code 验证码 * @return void */ protected function renderImage($code) { $image = imagecreatetruecolor($this->width, $this->height); $backColor = imagecolorallocate($image, (int)($this->backColor % 0x1000000 / 0x10000), (int)($this->backColor % 0x10000 / 0x100), $this->backColor % 0x100); imagefilledrectangle($image, 0, 0, $this->width, $this->height, $backColor); imagecolordeallocate($image, $backColor); $foreColor=imagecolorallocate($image, (int)($this->foreColor % 0x1000000 / 0x10000), (int)($this->foreColor % 0x10000 / 0x100), $this->foreColor % 0x100); if($this->fontFile === null) { $this->fontFile = dirname(__FILE__).'/Captcha/Duality.ttf'; } $offset = 1; $length = strlen($code); $box = imagettfbbox(30, 1, $this->fontFile, $code); $w = $box[4] - $box[0] - $offset * ($length-1); $h = $box[1] - $box[5]; $scale = min(($this->width - $this->padding * 2) / $w, ($this->height - $this->padding * 2) / $h); $x = 10; $y = round($this->height * 27 / 40); for($i = 0; $i <= $length; ++$i) { $fontSize = (int)(rand(26, 32) * $scale * 0.8); $angle = rand(-10, 10); $letter = $code[$i]; $box = imagettftext($image, $fontSize, $angle, $x, $y, $foreColor, $this->fontFile, $letter); $x = $box[2] - $offset; } imagecolordeallocate($image, $foreColor); header('Pragma: public'); header('Expires: 0'); header('Cache-Control: must-revalidate, post-check=0, pre-check=0'); header('Content-Transfer-Encoding: binary'); header("Content-type: image/png"); imagepng($image); imagedestroy($image); } }