Image display in the Browser issue using php
one text
Solution:
By returning an <img>
tag, you are basically echo'ing an <img>
tag into an <img>
tag's src
attribute which effectively looks like this:
<img src="<img src="data:image/jpeg;base64">">
In your showImage
script, there's no reason to base64 encode anything, that's only need when you are using a text-based transport like HTML. Since you are transporting over HTTP which supports binary, you can instead just send the file. There's a couple of ways, but the easiest is:
echo file_get_contents($image);
exit;
Another option that is a little more performant for larger files is:
readfile($image);
exit;
If you want to get a little fancier, you can also output the MIME for the file:
header('Content-Type: ' . mime_content_type($image));
readfile($image);
exit;
edit
I should also point out that passing filenames as parameters without sanitization and validation is a big security problem that can lead to path traversal problems. At a minimum, I'd call basename()
on the supplied parameter, but you should probably look into your specific setup to see what could or couldn't go wrong.