2

I stored it images in the database using an BLOB field (I'm using SQLite). Now I want to recover this image to a HTML page and show the images there.

I can retrieve the binary data from the image from the database, but what I can do to transform this data in an image and show in the page? Currently I want to show the images inside a field in a table.

Renato Dinhani
  • 35,057
  • 55
  • 139
  • 199

5 Answers5

1

You could abuse the data: protocol, but trust me, you don't want that if you can avoid it. Normally, you create a separate php-script that serves images, so in script 1:

 <img src="/myimagescript.php?id=1234">

In myimagescript.php:

//get the data from the database somehow (mysql query et al.)
//let's assuma the data is in $data
header('Content-Type: image/jpeg');//alter for png/gif/etc.
echo $data;
Wrikken
  • 69,272
  • 8
  • 97
  • 136
1

@uscere90 is right, but an example might help (example of a PNG image):

<?php
  header("Content-type: image/png");
  echo $image_data;
?>
Teddy
  • 18,357
  • 2
  • 30
  • 42
0

You can create a simple image.php page that queries your database, then prints out a content-type relevant to the image and vomits the binary data to screen. So, in your table, you'd have <img src=image.php?id=something />, and then you'd use that id in your image.php page to do your database lookup, retrieve the binary data, and print it to screen after printing the content-type header.

image.php:

<?php

header('Content-type: image/jpeg');

//DO SQL NINJA STUFF HERE

echo mysql_result($result,0,"file_content");

?>

uscere90
  • 543
  • 1
  • 5
  • 12
0

Typically this is done by creating a wrapper script or function that retrieves the BLOB and delivers it with the appropriate content headers to be used as an <img src=''>

Doing it this way also gives you the benefit of being able to deliver or not deliver the image based on other authentication factors determined by your PHP. If, for example, a user doesn't have permission to see an image, you can instead show some default or blocking image in its place.

// File getimg.php
// Retrieve the image blob specified by $_GET['imgid'] from the database

// Assuming your blob is now in the variable $image...
header("Content-type: image/jpeg");
// Just echo out the image data
echo $image;
exit();

Now in your html:

<img src='getimg.php?imgid=12345' alt='this is your img from the database' />
Michael Berkowski
  • 267,341
  • 46
  • 444
  • 390
0

There are two options I would say:

  1. You create a script that returns the image data. The <img src="-field then calls that script.
  2. You offer the data of the images directly via a data url.

Both have it's pros and cons. For the first solution you must create a new script for the images. The second method will bloat your page if the images are large.

As there are examples for the image script method already, here is some code fragment for data URIs:

<?php
function data_uri($content, $mime) 
{  
  $base64   = base64_encode($content); 
  return ('data:' . $mime . ';base64,' . $base64);
}
?>

<img src="<?php echo data_uri($content,'image/png'); ?>" />

You need to set the mime-type according to your image, image/png for PNG images, image/jpeg for JPG files etc., see here for a list.

hakre
  • 193,403
  • 52
  • 435
  • 836