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

后端 未结 7 1309
隐瞒了意图╮
隐瞒了意图╮ 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:25

    An easier way with automatic 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));
    

提交回复
热议问题