【问题标题】:Is there a way to have a Codeigniter controller return an image?有没有办法让 Codeigniter 控制器返回图像?
【发布时间】:2010-12-27 00:49:47
【问题描述】:

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

我要花很长时间才能解释我为什么需要这个,但这涉及到将预制的 CMS 引入 codeigniter,并让它需要某些东西才能成为现实。非常感谢!

【问题讨论】:

    标签: image codeigniter controller mime


    【解决方案1】:

    当然可以,用这个代替$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
      exit(); // or die()
    }
    

    【讨论】:

    • 我正在开发当前版本的 Codeigniter。这段代码对我不起作用
    • 我猜exit 命令没有任何意义,因为die 终止了脚本并且它们仍然执行相同的工作。顺便感谢您提供的代码。
    【解决方案2】:

    这不是一个高人一等,但 pǝlɐɥʞ 的建议是一个纯 PHP 实现,并不是所有的可重用。你想使用语法 $this->load->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->load->view一样,第三个参数设置为TRUE意味着它会返回而不是直接输出。

    希望这会有所帮助:-)

    【讨论】:

    • 库必须在 application/core/MY_Loader.php
    • @GauravGupta 是的,如果您使用的是 2.0 或更高版本,则它需要位于 core/ 而不是 libraries/。 2009 年不存在 2.0。:)
    【解决方案3】:

    使用自动 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));
    

    【讨论】:

      【解决方案4】:

      关于菲尔的代码:

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

      • 库必须在 /application/core/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();
      

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

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

      非常感谢菲尔。

      【讨论】:

      • 我从未注意到通用文件加载器。我正在浏览 CodeIgniter 的代码...感谢您指出这一点.. 使用最好的 codeigniter... :)
      • 好吧,我注意到的另一件事是在内部加载函数调用包含函数...根据 php 手册,如果它用于处理,则应该使用 include...而不是 readfile 函数应该使用... 这样我们就可以使用 File helper 中提供的 readfile 函数了!
      【解决方案5】:

      即使您将 $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);
      }
      

      【讨论】:

        【解决方案6】:

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

        // Do your logic here
        
        redirect($image_path); // Or PHP's header location function
        

        无需更改标题。您的用例可能不适合这个,但有人可能会觉得这很有用^_^

        【讨论】:

          【解决方案7】:

          我会做一个

          $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));
          

          如果这有帮助。干杯!

          【讨论】:

            猜你喜欢
            • 2017-11-16
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2020-12-24
            相关资源
            最近更新 更多