i am attempting to display an image from mysql blob (i know its not best practice) in my PHP page which i will then bring into my iOS app.
I'm having trouble setting the page header which i believe needs to be set as an image.
So, this shows the image but I do not believe the page header is correct:
//header("Content-type: image/png");
echo '';
However, this does not show my image:
header("Content-type: image/png");
echo '';
What am I missing? I believe the image should show when the header is set to image/png but i'm getting an error that it cannot be displayed because of errors.
Thank you!
解决方案header("Content-type: image/png");
This tells your browser that you're about to pass it raw binary data that is a PNG file. So anything output after that would have to be a binary PNG. You can't then place HTML and expect that to work
echo '';
This works because you're base64 encoding your image (translates binary into text), outputting that to the browser and then telling your browser to interpret it as base64.
If you want to output the raw binary data you have to rearrange the order. So here's your HTML
Now you'll note the src points to a PHP file. That's because we're going to have that PHP file return an image. Here's what image.php would look like
//Your query here
$row = mysqli_fetch_assoc($result);
header("Content-type: image/png");
echo $row['image1'];
This works because the browser will call the PHP file, expecting an image. The header tells the browser that this is a PNG file and you can now dump your binary PNG data.