0

I have a webservice I'm connecting to. I'm getting valid responses back for my queries, and one of them is an IMAGE type. I'm not in direct control of the DB, and they don't really offer support - so I can't ask them about the specifics.

I need to offer links to display or download this image. This should be easy enough, but I've been banging my head against this for quite a while now.

I have the IMAGE data. I have put this in a var called $evidence. If I do the following, I get a corrupt/unreadable image across all browsers I've used (broken image link symbol or similar text):

header("Content-type: image/jpeg");
echo $evidence;

So, while debugging this I tried to display the image inline using some copy/pasta'd code:

echo '<img src="data:image/jpeg;base64,'.$evidence.'" />';

This works as expected... the image is shown correctly.

However - I need to be able to do this the correct way via a header, as I need to do other things, such as be able to force downloads.

Every time I've ever had to do anything like this, the basic "header, echo" approach has always worked. By the looks of almost every tutorial out there: this should work. It seems to be so simple, most tutorials don't even look at display problems (only data fetching problems).

1 Answer 1

1

Following some thinking about how data is being passed, I noticed that there was the "base64" part of the code I was using in the image tag.

I modified the echo to be as follows:

echo base64_decode($evidence);

This fixed it.

SO! 1 simple "gotcha" to help everyone else out there: make sure you have checked the data type before you display. Don't assume it's binary!

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.