/** * * @return void */ public static function show() { self::$_image = imagecreate(self::$_width, self::$_height); $white = imagecolorallocate(self::$_image, 255, 255, 255); $black = imagecolorallocate(self::$_image, 0, 0, 0); imagefill(self::$_image, 0, 0, $white); for ($i = 0; $i < 20; $i++) { $pos_x = rand(0, 144); $pos_y = rand(0, 32); $width = rand(3, 100); $height = rand(3, 100); $color = imagecolorallocate(self::$_image, rand(200, 255), rand(200, 255), rand(200, 255)); imagefilledellipse(self::$_image, $pos_x, $pos_y, $width, $height, $color); } $rand = mt_rand(); $code = substr($rand, 2, 2) . substr($rand, -3); Session::set(self::$_sessionName, $code); imagettftext(self::$_image, self::$_fontSize, 1, self::$_positionX, self::$_positionY, $black, self::$_fontPath, $code); imagegif(self::$_image); imagedestroy(self::$_image); }
/** * Generate image colors. */ private static function _makeImage() { self::$_image = imagecreate(self::$_img["width"], self::$_img["height"]); }
/** * 输出验证码并把验证码的值保存的cookie中 * 验证码保存到session的格式为: $_COOKIE[self::$seKey] = array('code' => '验证码值', 'time' => '验证码创建时间'); * * @return void */ public static function entry($id = '') { // 图片宽(px) self::$imageL || (self::$imageL = self::$length * self::$fontSize * 1.5 + self::$fontSize * 1.5); // 图片高(px) self::$imageH || (self::$imageH = self::$fontSize * 2); // 建立一幅 self::$imageL x self::$imageH 的图像 self::$_image = imagecreate(self::$imageL, self::$imageH); // 设置背景 imagecolorallocate(self::$_image, self::$bg[0], self::$bg[1], self::$bg[2]); // 验证码字体随机颜色 self::$_color = imagecolorallocate(self::$_image, mt_rand(1, 120), mt_rand(1, 120), mt_rand(1, 120)); // 验证码使用随机字体 $ttfPath = dirname(__FILE__) . '/Captcha/' . (self::$useZh ? 'zhttfs' : 'ttfs') . '/'; $dir = dir($ttfPath); $ttfs = array(); while (false !== ($file = $dir->read())) { if ($file[0] != '.' && substr($file, -4) == '.ttf') { $ttfs[] = $ttfPath . $file; } } $dir->close(); $ttf = $ttfs[array_rand($ttfs)]; if (self::$useImgBg) { self::_background(); } if (self::$useNoise) { // 绘杂点 self::_writeNoise(); } if (self::$useCurve) { // 绘干扰线 self::_writeCurve(); } // 绘验证码 $code = array(); // 验证码 $codeNX = 0; // 验证码第N个字符的左边距 for ($i = 0; $i < self::$length; $i++) { if (self::$useZh) { $code[$i] = chr(mt_rand(0xb0, 0xf7)) . chr(mt_rand(0xa1, 0xfe)); } else { $code[$i] = self::$_codeSet[mt_rand(0, 27)]; $codeNX += mt_rand(self::$fontSize * 1.2, self::$fontSize * 1.6); // 写一个验证码字符 self::$useZh || imagettftext(self::$_image, self::$fontSize, mt_rand(-40, 40), $codeNX, self::$fontSize * 1.5, self::$_color, $ttf, $code[$i]); } } $data = array(); // 保存验证码 if ($id) { $data[$id]['code'] = join('', $code); // 把校验码保存到session\ $data[$id]['time'] = time(); // 验证码创建时间 } else { $data['code'] = join('', $code); // 把校验码保存到session\ $data['time'] = time(); // 验证码创建时间 } setcookie(self::$seKey, json_encode($data)); self::$useZh && imagettftext(self::$_image, self::$fontSize, 0, (self::$imageL - self::$fontSize * self::$length * 1.2) / 3, self::$fontSize * 1.5, self::$_color, $ttf, iconv("GB2312", "UTF-8", join('', $code))); header('Cache-Control: private, max-age=0, no-store, no-cache, must-revalidate'); header('Cache-Control: post-check=0, pre-check=0', false); header('Pragma: no-cache'); header("content-type: image/png"); // 输出图像 imagepng(self::$_image); imagedestroy(self::$_image); }