/** * 生成验证码图像,返回 QView_Output 对象 * * $param string $code * * @return QView_Output */ function generateImage($code) { // 根据选项确定绘图选项 $padding = intval($this->_options['padding']); if ($padding < 0) { $padding = 0; } $color = $this->_options['color']; $bgcolor = $this->_options['bgcolor']; $border = $this->_options['border']; $bdcolor = $this->_options['bdcolor']; $float_pixel = intval($this->_options['float_pixel']); // 确定要使用的字体 if (is_int($this->_options['font'])) { $font = intval($this->_options['font']); if ($font < 0 || $font > 5) { $font = 5; } } else { $font = imageloadfont($this->_options['font']); } // 确定字体宽度和高度 $font_width = imagefontwidth($font); $font_height = imagefontheight($font); // 确定图像的宽度和高度 $width = $font_width * strlen($code) + $padding * 2 + $border * 2 + 1; $height = $font_height + $padding * 2 + $border * 2 + 1 + $float_pixel; // 创建图像 $img = imagecreate($width, $height); // 绘制边框 if ($border) { list($r, $g, $b) = Helper_ImgCode::hex2rgb($bdcolor); $color = imagecolorallocate($img, $r, $g, $b); imagefilledrectangle($img, 0, 0, $width, $height, $color); } // 绘制背景 list($r, $g, $b) = Helper_ImgCode::hex2rgb($bgcolor); $color = imagecolorallocate($img, $r, $g, $b); imagefilledrectangle($img, $border, $border, $width - $border - 1, $height - $border - 1, $color); // 绘制文字 list($r, $g, $b) = Helper_ImgCode::hex2rgb($color); $color = imagecolorallocate($img, $r, $g, $b); for ($i = 0, $max = strlen($code); $i < $max; $i++) { imagestring($img, $font, $padding + $border + $font_width * $i, $padding + $border + mt_rand(0, $float_pixel), $code[$i], $color); } $filename = 'imgcode-' . mt_rand(); ob_start(); // 输出图像 switch (strtolower($this->_options['image_type'])) { case 'png': $filename .= '.png'; $mime = image_type_to_mime_type(IMAGETYPE_PNG); imagepng($img); break; case 'gif': $filename .= '.gif'; $mime = image_type_to_mime_type(IMAGETYPE_GIF); imagegif($img); break; case 'jpg': default: $filename .= '.jpg'; $mime = image_type_to_mime_type(IMAGETYPE_JPEG); imagejpeg($img); } imagedestroy($img); unset($img); $output = new QView_Output($filename, $mime, ob_get_clean()); $output->contentDisposition('inline')->enableClientCache(false); return $output; }