tags:

views:

49

answers:

3

Okay excuse the cryptic subject.

I have a system which dynamically fetches image data and does some modification on the fly.

Essentially, missing out the unimportant bits, this is my code:

$File->Filename = "testimage.jpg";
$File->Open();
$FileData = $File->Read();

header('Content-Type: image/jpeg');

echo $FileData;

The only output on the page is $FileData. Okay. When I run the script as is, Firefox presents me with a blank page and Chrome and IE give me a 'missing picture' box.

However oddly enough, when I remove the Content Type declaration, I can see the raw image data just fine. I have tested this with several images, granted all of the JPEG type but it clearly loads up the different pictures just fine, as the raw data changes successfully, and matches the raw content of the image itself.

Anyone have any idea why it would be failing to just display the image at this point?

+1  A: 

Try setting Content-Length appropriately. Remove the trailing ?> to make sure there is no whitespace at the end of the script and ensure that the starting it at the very start of your script.

phant0m
+3  A: 

You need to give more information in order to let the browser handle it correctly (use the correct type and length):

header('Content-Length: '.strlen($FileData),true); // EDIT: inserted the strlen function as suggested by kgb
header('Content-Type: image/jpeg');
Thariama
true is the default argument, you do not need to specify it.
phant0m
A: 

i'll combine phant0m's and Thariama's answers ;):

header('Content-Length: '.strlen($FileData), true);
header('Content-Type: image/jpeg');
die($FileData);
kgb
die() is not a good function for "normal" output and script termination, just because of its name, as it signifies an abnormal termination to the developer. print() and exit() is a better choice.
Palantir
@Palantir: i would put die/exit to be sure the script exits without outputting anything else.
kgb
If there is no other echo or print statement, all you have to do is remove the closing PHP tag and PHP ;)
phant0m
Attempted to do this, without a die but with an exit at the end. There is definately no trailing data. Still not happening for me.
David
actually, I lie. There WAS a trailing space, but it was in an include of an include, not in the file itself.Working now, sorry for the noob moment! :) thanks! Will tick this one due to my 'answer' comment being present
David