Is there a way to have a Codeigniter controller return an image?

后端 未结 7 1320
隐瞒了意图╮
隐瞒了意图╮ 2021-02-08 01:59

I was wondering if there was a way for a controller to, instead of returning a string, or a view, return an image (be it JPG, PNG etc). For example, instead of ending with a $t

7条回答
  •  暗喜
    暗喜 (楼主)
    2021-02-08 02:14

    About the Phil's code:

    In CodeIgniter 2.0, today, there are a one change that have to be made in order to make it work:

    • The library has to be in /application/core/MY_Loader.php

    I like to remark a small typo about the library's explanation:

    • There is a mistake in the header "Basic output (default is jpeg)" because in fact the default is .png

    Another solutions to the problem are:

    I've made a small code to make it work with the core codeIgniter libraries:

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

    Or using the Image Manipulation Library

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

    In both cases you get the same image you get from the source but in the output.

    But for me, I liked more the extension to the core library :-)

    Thank you very much Phil.

提交回复
热议问题