简体   繁体   中英

How to set Content-Type in Codeigniter

I use this code to show an image in php

header('Content-Type: image/jpeg');
$img = imagecreatefromjpeg('test.jpg');
imagejpeg($img);
imagedestroy($img);

It is working in a simple php file, but it's not working in a codeigniter controller. I'm using this method:

public function render_image() {
    header("Content-type: image/jpeg");
    $img = imagecreatefromjpeg('uploads/test.jpg');

    imagejpeg($img);
    imagedestroy($img);
}

I also tried using this code, but its not helping:

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

How do I solve this problem?

It was because of the hook. i'm using "post_controller_constructor" hook. i put my code in __construct() and everything is working.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM