有没有办法让Codeigniter控制器返回图像?

时间:2009-12-15 23:04:07

标签: image codeigniter controller mime

我想知道控制器是否有办法,而不是返回字符串或视图,返回图像(无论是JPG,PNG等)。例如,我不想以$ this-> load->视图('folder / special_view.php)结尾,而是想做像$ this-> load-> image('images / gorilla)这样的事情。 .png'),并且如果我的用户要去那个控制器他们会看到一个图像,好像他们去了正常的.png或jpeg。我可以设置标头,以便它需要不同的MIME吗?这个代码示例很棒。

我需要永远解释为什么我需要这个,但它涉及将预制的CMS带入codeigniter,并让它需要认证的事情。非常感谢你!

7 个答案:

答案 0 :(得分:22)

确定可以使用此代替$this->load->view()

$filename="/path/to/file.jpg"; //<-- specify the image  file
if(file_exists($filename)){ 
  $mime = mime_content_type($filename); //<-- detect file type
  header('Content-Length: '.filesize($filename)); //<-- sends filesize header
  header("Content-Type: $mime"); //<-- send mime-type header
  header('Content-Disposition: inline; filename="'.$filename.'";'); //<-- sends filename header
  readfile($filename); //<--reads and outputs the file onto the output buffer
  die(); //<--cleanup
  exit; //and exit
}

答案 1 :(得分:13)

这不是一次性的,但pǝlɐɥʞ的建议是一个纯粹的PHP实现,并不是所有可重用的。你想使用语法$ this-&gt; load-&gt; image('images / gorilla.png'),所以你可以这样做。

创建/application/libraries/MY_Loader.php

<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');

/**
 * Loader Class
 *
 * Loads views and files
 *
 * @package     CodeIgniter
 * @subpackage  Libraries
 * @author      Phil Sturgeon
 * @category    Loader
 * @link        http://codeigniter.com/user_guide/libraries/loader.html
 */
class MY_Loader extends CI_Loader {

    function image($file_path, $mime_type_or_return = 'image/png')
    {
        $this->helper('file');

        $image_content = read_file($file_path);

        // Image was not found
        if($image_content === FALSE)
        {
            show_error('Image "'.$file_path.'" could not be found.');
            return FALSE;
        }

        // Return the image or output it?
        if($mime_type_or_return === TRUE)
        {
            return $image_content;
        }

        header('Content-Length: '.strlen($image_content)); // sends filesize header
        header('Content-Type: '.$mime_type_or_return); // send mime-type header
        header('Content-Disposition: inline; filename="'.basename($file_path).'";'); // sends filename header
        exit($image_content); // reads and outputs the file onto the output buffer
    }

有几种方法可以使用它:

基本输出(默认为jpeg)

$this->load->image('/path/to/images/gorilla.png');

发送mime-type以使用其他图像类型

$this->load->image('/path/to/images/gorilla.jpg', 'image/jpeg');

返回图片

$image = $this->load->image('/path/to/images/gorilla.php', TRUE);

就像$ this-&gt; load-&gt;视图一样,第三个参数设置为TRUE意味着它将返回而不是直接输出。

希望这会有所帮助: - )

答案 2 :(得分:11)

使用自动mime-type更简单的方法。

$this->load->helper('file');

$image_path = '/path/to/image/file';

$this->output->set_content_type(get_mime_by_extension($image_path));
$this->output->set_output(file_get_contents($image_path));

答案 3 :(得分:2)

关于Phil的代码:

在CodeIgniter 2.0中,今天必须进行一项更改才能使其正常工作:

  • 图书馆必须位于/ application / 核心 /MY_Loader.php

我想谈一下关于图书馆解释的小错误:

  • 标题“基本输出(默认为jpeg)”中有错误,因为实际上默认为.png

该问题的另一个解决方案是:

我已经制作了一个小代码,以使其与核心codeIgniter库一起使用:

    $this->output->set_header("Content-Type: image/png");
    $this->load->file('../images/example.png');

或使用图像处理库

    $config['image_library'] = "GD2";
    $config['source_image'] = "../images/example.png";
    $config['maintain_ratio'] = TRUE;
    $config['dynamic_output'] = TRUE;

    $this->load->library('image_lib', $config);
    $image = $this->image_lib->resize();

在这两种情况下,您都可以获得与源相同但在输出中的相同图像。

但对我来说,我更喜欢核心库的扩展: - )

非常感谢Phil。

答案 4 :(得分:1)

如果它适合您的使用案例,只需重定向到它就好了。例如,使用图像进行跟踪就像:

// Do your logic here

redirect($image_path); // Or PHP's header location function

无需更改标题。您的用例可能不适合这种情况,但有人可能会发现这个有用^ _ ^

答案 5 :(得分:1)

我会做一个

$computedImage = 'path/to/the/image.ext';
$this->load->helper('file');
$this->output->set_content_type(get_mime_by_extension($computedImage))->set_output(file_get_contents($computedImage));

如果这有帮助。干杯!

答案 6 :(得分:0)

即使您将$ config ['compress_output']设置为TRUE

,此方法仍然有效
$filename="/path/to/file.jpg"; //<-- specify the image  file
if(file_exists($filename)){ 
  header('Content-Length: '.filesize($filename])); //<-- sends filesize header
  header('Content-Type: image/jpg'); //<-- send mime-type header
  header('Content-Disposition: inline; filename="'.$filename.'";'); //<-- sends filename     header
  $jpg = file_get_contents($filename); 
  $this->output->set_output($jpg);
}
相关问题