Displaying a Base64 images from a database via PHP

The solution to your problem is here:

How to decode a base64 string (gif) into image in PHP / HTML

Quoting that source but modifying:

In the case you strip out the first case and choose to decode the string, you should add this before echoing the decoded image data:

header("Content-type: image/gif");
$data = "/9j/4AAQSkZJRgABAQEAYABgAAD........";
echo base64_decode($data);

In the second case, use this instead:

echo '<img src="data:image/gif;base64,' . $data . '" />';

The second case is bad because the browser does not perform caching if the same image is shown on multiple pages.

Leave a Comment