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.


try this

//your image data

$logodata = "/9j/4AAQSkZJRgABAQEAYABgAAD........";
echo '<img src="data:image/gif;base64,' . $logodata . '" />';

Use this:

$code_base64 = $row['content'];
$code_base64 = str_replace('data:image/jpeg;base64,','',$code_base64);
$code_binary = base64_decode($code_base64);
$image= imagecreatefromstring($code_binary);
header('Content-Type: image/jpeg');
imagejpeg($image);
imagedestroy($image);

Try this:

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

Tags:

Php