0

I have an index.php file which contains :

<img src="photo.php" />

And photo.php :

header("Content-type: image/jpg");
$result = base64_encode($arr['FOTO']);
echo "data:image/jpg;base64,$result" // 1

//echo "<img src='data:image/jpg;base64,$result' />"; // 2

The code doesn't work, can someone explain why? The image is displayed when I comment the line 1 and uncomment the line 2 and request photo.php directly.

3

1 Answer 1

2

You could just output the file contents in photo.php, without base64_encoding it.

echo $arr['FOTO'];

This would only work when included within an tag on another page, when called directly you would probably only see some gibberish. This can be fixed by setting the correct Content-Type header:

Header('Content-Type: image/png'); // (Or image/jpeg, image/gif etc.)
echo $arr['FOTO'];

Or you could output it directly within index.php as, given you have the data available there:

<img src='data:image/jpg;base64,$result' />
Sign up to request clarification or add additional context in comments.

3 Comments

I edited the question now, it actually has header , and I have also tried first solution it doesn't work.Third one could solve probably, but I want to know what is problem with the approach?
Giving a base64 encoded value the mime type of an image, will not make it an image (the mime type is wrong anyway, should be image/jpeg in this case). Try to skip the whole base64 encoding, it's not necessary. Just echo $arr['photo'] with the correct Content-Type header.
Previously the code was as you said but it didn't work so i tried base64_encode.I tried your solution again, it doesn't work.

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.