Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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 $this->load->view('folder/special_view.php), I'd like to do something like $this->load->image('images/gorilla.png'), and have it so if my user were to go to that controller they would see an image as if they'd gone to a normal .png or jpeg. Can I set the headers so it expects a different MIME? Example code of this would be fantastic.

It would take forever for me to explain why I need this, but it involves bringing a premade CMS into codeigniter, and having it need certian things to be true. Thank you so much!

like image 227
Ethan Avatar asked Dec 15 '09 23:12

Ethan


People also ask

What is the use of controller in CodeIgniter?

A Controller is simply a class file that is named in a way that can be associated with a URI. In the above example, CodeIgniter would attempt to find a controller named Blog. php and load it. When a controller's name matches the first segment of a URI, it will be loaded.

What is default controller in CodeIgniter?

Defining a Default ControllerCodeIgniter can be told to load a default controller when a URI is not present, as will be the case when only your site root URL is requested. To specify a default controller, open your application/config/routes. php file and set this variable: $route['default_controller'] = ' Blog ';

What is views in CodeIgniter?

A view is simply a web page, or a page fragment, like a header, footer, sidebar, etc. In fact, views can flexibly be embedded within other views (within other views, etc., etc.) if you need this type of hierarchy. Views are never called directly, they must be loaded by a controller.


2 Answers

sure you can, use this instead of $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()
}
like image 59
ekhaled Avatar answered Sep 22 '22 14:09

ekhaled


This method works even if you have $config['compress_output'] set to 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);
}
like image 34
JohnWright Avatar answered Sep 23 '22 14:09

JohnWright