Welcome Guest, Not a member yet? Register   Sign In
Serving Image from controller (warning newbie alert)
#1

[eluser]MattEth[/eluser]
Hi guys,

I'm very new to PHP and CI (I'm a java programmer), so please be patient Smile

I am attempting to load an image (via http from another site - this will eventually just be from a database), then I want to serve the image from the controller so that pages can have the following:

Code:
<img src="mycontroller/serve_images/1"/>

My controller method currently has something like:

Code:
function serve_images {
  $imageString = file_get_contents('http://blah.com/myimage.gif');
  header("Content-Type: image/gif");

  // What goes here to get image into output stream?
}

My problem is: what comes next? How do I get the image's bytes/data into the output stream so the browser will recognise the data as an image and show it? I'm also not sure whether the "image string" is the right data format - or whether I need to convert the image string into a different format.

Again, apologies for my PHP/CI ignorance!

Thanks!

Matt.

[edit] - and I think I might have put this into the wrong forum! sorry!
#2

[eluser]graf[/eluser]
did you try to echo out the file_get_contents after you sent the header type?

This should work ;-]


Code:
function serve_images {
  $imageString = file_get_contents('http://blah.com/myimage.gif');
  header("Content-Type: image/gif");
  echo $imageString;
}
#3

[eluser]MattEth[/eluser]
[quote author="graf" date="1251778068"]did you try to echo out the file_get_contents after you sent the header type?

This should work ;-]

Code:
function serve_images {
  $imageString = file_get_contents('http://blah.com/myimage.gif');
  header("Content-Type: image/gif");
  echo $imageString;
}
[/quote]

Thanks for the reply. Yes, I did try this, however, the image is still broken (ie. the browser things the image is not valid). Anyone else have any ideas? Maybe there is a better way to do what I'm trying to do?

Thanks! Smile
#4

[eluser]MattEth[/eluser]
... figured it out eventually. In my controller, I had some debugging code in the constructor that echoed a debug statement everytime the controller was called. This debug string was being merged with the image data causing the browser to think the data was not a valid image!

Something else interesting that I found here was that I did not need to set the content type header to get the image to show correctly. My controller method merely had:

Code:
$imageString = file_get_contents('http://blah.com/myimage.jpg');
echo $imageString;

Does anyone know why this is? I thought I had to specify the content type for this to work properly?

Cheers.
#5

[eluser]graf[/eluser]
You should set the header incase the browser/agent fails to recognize the data as an image. Some browsers will 'assume' the file is an image if it has certain endings in the url (.jpg/.gif) etc. I usually add a dumby file name to the end of the controller.. i.e.... controller/view/dumby.jpg when serving up my images to help button up any browser inconsistencies.
#6

[eluser]Aken[/eluser]
You should definitely include some other verifications and information to make sure everything goes smoothly.

- Check to see what image you're trying to pull to see if you even want to allow it.
- Pull the remote file's extension and verify it's an image.
- Take the file's extension and add the appropriate headers before echoing the file.
#7

[eluser]MattEth[/eluser]
Thanks for the extra tips guys, much appreciated!




Theme © iAndrew 2016 - Forum software by © MyBB