0

I've stored the image in the base64 format in the database (stored formate: data:image/jpeg;base64,/9j/4AAQSkZJR ) now I want back image from database using PHP, I used all the method but doesn't work.

my current method is

enter image description here

Syscall
  • 19,327
  • 10
  • 37
  • 52
akshay dighe
  • 13
  • 1
  • 5

2 Answers2

0

what you need to do is read the base64 from db and use it to create an image file, here is a function to create jpeg image from base64

function base64_to_jpeg($base64, $jpeg) {

    $fp = fopen($jpeg, 'wb'); 
    $data = explode(',', $base64);
    fwrite($fp, base64_decode($data[1]));
    fclose( $fp ); 

    return $jpeg; 
}

and the function will return the jpeg file object

Shobi
  • 10,374
  • 6
  • 46
  • 82
0

May be this solution works for you:

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.

Jasbir
  • 374
  • 5
  • 15