Home » Php » php: recreate and display an image from binary data

php: recreate and display an image from binary data

Posted by: admin April 23, 2020 Leave a comment


Is it possible to recreate images from binary data (process them if needed) and display them, all in the same script? Something like

// get and display image 1:
$imagedata1 = file_get_contents('assets/test.png');
$imagedata1 = process_using_gd_or_something($imagedata1);

echo "<img src={$imagedata1} >"; // <-- IS THIS (OR EQUIVALENT) POSSIBLE?

// get and display image 2:

I want to avoid storing the images to to disk after processing and getting them from there, or using an external script…

How to&Answers:

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:

function data_uri($file, $mime) 
  $contents = file_get_contents($file);
  $base64   = base64_encode($contents); 
  return ('data:' . $mime . ';base64,' . $base64);

<img src="<?php echo data_uri('elephant.png','image/png'); ?>" alt="An elephant" />


This is actually possible using inline images (called data URIs).

Your image tag would look something like this:

<img src="data:image/gif;base64,R0lGODlhEAAOALMAAOazToeHh0tLS/7LZv/0jvb29t/f3//Ub/
width="16" height="14" alt="embedded folder icon">

Why they are mostly not a good idea:

  • Page load will be slowed down because the image needs to be fetched before the full HTML structure can be loaded and thus, rendered. Even more so if you are performing additional operations on the image. Your site will very likely feel much slower than if it were an external image.

  • Inline images need to be base64 encoded, adding 33% to their size.

If you are talking about a reasonable high-traffic, public site, I would recommend you store your image externally, and cache them. If it’s just for a small project, inline images may work for you.


Other possibility for you is to create a script producing the image data to the output and direct the link to it.


$imagedata1 = file_get_contents('assets/test.png');
$imagedata1 = process_using_gd_or_something($imagedata1);

header('Content-type: image/png');
echo $imagedata1;


echo "<img src='image.php?some_params'>";

Sorry, I missed the notice of not wanting an external script, but this solution is more efficient than encoding the image to base64.


Try this…


<img alt="105x105" class="img-responsive" src="data:image/jpg;charset=utf8;base64,<?php echo $img ?>"/>


In case you just want the image, without any html around it you can use the following:

$filename = 'assets/test.png';
$original_image = file_get_contents($filename);
$processed_image = process_the_image_somehow($original_image);

header('Content-type: '.mime_content_type($filename));
header('Content-Length: '.strlen($processed_image));
echo $processed_image;

You must not forget the Content-Length header, otherwise it won’t work.
You may also want to replace mime_content_type() as it is deprecated according to the docs.