Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

I have a base64 encoded string that I would like to convert into an image in PHP / HTML.

Here is what I have:

$data = "R0lGODdhAAGAAKIAAP38+/3h3cjN5P3HwgAAAP8AoP8AGv8EIywAAAAAAAGAAAAD....."; echo base64_decode($data); 

// OR

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

None of those work. Any suggestions?

Much appreciated! Catalin

like image 647
integral007 Avatar asked Nov 05 '10 23:11

integral007


2 Answers

In the first case you should add this before echoing the decoded image data:

header("Content-type: image/gif"); 

In the second case, use this instead:

echo '<img src="data:image/gif;base64,' . $data . '" />'; 
like image 175
Emil H Avatar answered Oct 28 '22 09:10

Emil H


Display Image using base64_encode function

$ret = fopen($fullurl, 'r', true, $context); $contents = stream_get_contents($ret); $base64 = 'data:image/PNG;base64,' . base64_encode($contents); echo "<img src=$base64 />" ; 
like image 34
Milind Morey Avatar answered Oct 28 '22 07:10

Milind Morey