protected function _output($filename, $mime) { $output = new QView_Output($filename, $mime); $output->addFile(dirname(__FILE__) . "/../..//static/{$filename}"); $output->enableClientCache(true); return $output; }
protected function _output($filename, $mime) { $output = new QView_Output($filename, $mime); $output->addFile($this->app()->ROOT_DIR() . "/static/{$filename}"); $output->enableClientCache(true); return $output; }
/** * 生成验证码图像,返回 QView_Output 对象 * * $param string $code * * @return QView_Output */ function generateImage($code) { // 确定要使用的字体 if (isset(self::$_font_list[$this->_options['font']])) { $font = Q_DIR . '/_resources/' . self::$_font_list[$this->_options['font']]; } else { $font = $this->_options['font']; } // 确定图像的宽度和高度 $border = $this->_options['border']; $float_pixel = intval($this->_options['float_pixel']); $padding = intval($this->_options['padding']); $width = intval($this->_options['width']) + $padding * 2 + $border * 2 + 1; $width *= 2; $height = intval($this->_options['height']) + $padding * 2 + $border * 2 + 1 + $float_pixel; // 创建图像 $img = imagecreatetruecolor($width, $height); // 绘制背景 list($r, $g, $b) = Helper_ImgCode::hex2rgb($this->_options['bgcolor']); imagefilledrectangle($img, $border, $border, $width - $border - 1, $height - $border - 1, imagecolorallocate($img, $r, $g, $b)); // 绘制文字 $max_angle = intval($this->_options['max_angle']); $x = $padding + $padding + $border; $bottom = $height - $padding + $border - $float_pixel * 2; $font_space = $this->_options['font_space']; $color_arr = Helper_ImgCode::hex2rgb($this->_options['color']); $arr = array(); $font_size = $this->_options['font_size']; $min_r = $r + 50; $min_g = $g + 50; $min_b = $b + 50; for ($i = 0, $max = strlen($code); $i < $max; $i++) { $arr[$i]['font_size'] = mt_rand(50, 150) / 100 * $font_size; $arr[$i]['angle'] = rand(0, $max_angle) - $max_angle / 2; list($r, $g, $b) = $color_arr; $r = ($r + rand(0, 2550)) % (255 - $min_r) + $min_r; $g = ($g + rand(0, 2550)) % (255 - $min_g) + $min_g; $b = ($b + rand(0, 2550)) % (255 - $min_b) + $min_b; $arr[$i]['color'] = imagecolorallocate($img, $r, $g, $b); } for ($i = 0; $i < $max; $i++) { $x += $font_space; $y = $bottom; list(, , $x) = imagettftext($img, $arr[$i]['font_size'], $arr[$i]['angle'], $x, $y, $arr[$i]['color'], $font, $code[$i]); imagecolordeallocate($img, $arr[$i]['color']); } $new_width = intval($this->_options['width']) + $padding * 2 + $border * 2 + 1; $img_output = imagecreatetruecolor($new_width, $height); imagecopyresampled($img_output, $img, 0, 0, 0, 0, $new_width, $height, $x + $padding + $border, $height); imagedestroy($img); // 绘制边框 if ($border) { list($r, $g, $b) = Helper_ImgCode::hex2rgb($this->_options['bdcolor']); imagerectangle($img_output, 0, 0, $width, $height, imagecolorallocate($img_output, $r, $g, $b)); } $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_output); break; case 'gif': $filename .= '.gif'; $mime = image_type_to_mime_type(IMAGETYPE_GIF); imagegif($img_output); break; case 'jpg': default: $filename .= '.jpg'; $mime = image_type_to_mime_type(IMAGETYPE_JPEG); imagejpeg($img_output); } imagedestroy($img_output); $output = new QView_Output($filename, $mime, ob_get_clean()); $output->enableClientCache(false); return $output; }