Proper header php mysql blob display image

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.

  • How to use NSURLSession to determine if resource has changed?
  • NSURLConnection Authorization Header not Working
  • Specifying HTTP referer in embedded UIWebView
  • UIWebView capturing the response headers
  • How to upload the Users profile pic and how to fetch that profile pic from another users device?
  • How to get response headers when using Alamofire in Swift?
  • So, this shows the image but I do not believe the page header is correct:

        //header("Content-type: image/png");
    echo '<img src="data:image/png;base64,' . base64_encode( $row['image1'] ) . '" />';

    However, this does not show my image:

        header("Content-type: image/png");
    echo '<img src="data:image/png;base64,' . base64_encode( $row['image1'] ) . '" />';

    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!

    Solutions Collect From Internet About “Proper header php mysql blob display image”

    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 '<img src="data:image/png;base64,' . base64_encode( $row['image1'] ) . '" />';

    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

    <img src="image.php" />

    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.