php: recreate and display an image from binary data

You can do this using a data URI in the image src attribute.

The format is: data:[<MIME-type>][;charset="<encoding>"][;base64],<data>

This example is straight from the Wikipedia page on data URIs:

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

<img src="https://stackoverflow.com/questions/2070603/<?php echo data_uri("elephant.png','image/png'); ?>" alt="An elephant" />

Leave a Comment