27

I have this database that contains images as strings. Those strings look something like this:

data:image/jpeg;base64,/9j/4AAQSkZJRgABAQEAYABgAAD...

I need to create a link that will display this image. Like something.com/?id=27 is an image. All the images are in jpeg format. Here's what I've tried but didn't work:

<?php
  $host = "smth";
  $user = "smth";
  $pass = "smth";
  $db_name = "smth";
  $dbh = new PDO("mysql:host=$host;dbname=$db_name", $user, $pass);
  $dbh->exec("SET NAMES utf8");
  $q = $dbh->prepare("select content from img where id = :id");
  $q->execute(array(':id'=>$_GET['id']));
  $row = $q->fetch(PDO::FETCH_BOTH);
  header("Content-type: image/jpeg");
  echo $row['content'];
?>

The data is being fetched correctly but the image is not displayed.

I need to be able to use this link like this <img src="mysite.com?id=21" /> and I do NOT want this solution: <img src="data:image/jpeg;base64,/9j/4AAQSkZJRgABA..." />

Thanks!

dda
  • 6,030
  • 2
  • 25
  • 34
Kosmas Papadatos
  • 1,277
  • 2
  • 14
  • 32
  • 2
    Why are you storing it as base64 in the first place? – Blender Apr 28 '13 at 10:54
  • 1
    Off topic, but you do realise that this will be substantially slower/needlessly resource heavy in comparison to simply storing a path to the image on disk in the DB? – John Parker Apr 28 '13 at 10:55
  • Long story, I don't have a choice anymore I just have to display them somehow! – Kosmas Papadatos Apr 28 '13 at 10:56
  • @middaparka Off topic? LOL :D – hek2mgl Apr 28 '13 at 10:56
  • @KosmasPapadatos What about to remove the `data:image/jpeg;base64,` in front? – hek2mgl Apr 28 '13 at 10:56
  • 3
    As @hek2mgl states, you'll need to strip the preceeding "data:image/jpeg;base64," and base64 decode the data before you can output it. – John Parker Apr 28 '13 at 10:57
  • 1
    @middaparka Note that you can do **really** cool things with images in a database. Like *select image where color is 75% green* (pseudo example). Of course you can do the same if you just have that information in the db and the image itself on filesystem. But remembering a DB2 extension who did so – hek2mgl Apr 28 '13 at 11:00
  • The solution to your problem is here: http://stackoverflow.com/questions/4110907/how-to-decode-a-base64-string-gif-into-image-in-php-html – Menelaos Apr 28 '13 at 11:02

6 Answers6

46

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.

Community
  • 1
  • 1
Menelaos
  • 23,508
  • 18
  • 90
  • 155
12

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);
Ashkan Arefi
  • 665
  • 4
  • 7
6

try this

//your image data

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

Try this:

echo '<img src="data:image/png;base64,' . $base64encodedString . '" />
Mahbub Alam
  • 368
  • 2
  • 6
2
/**
* @param $base64_image_content 
* @param $path 
* @return bool|string
*/
function base64_image_content($base64_image_content,$path){
  if (preg_match('/^(data:\s*image\/(\w+);base64,)/', $base64_image_content, $result)){
    $type = $result[2];
    $new_file = $path."/".date('Ymd',time())."/";
    $basePutUrl = C('UPLOAD_IMG_BASE64_URL').$new_file;

    if(!file_exists($basePutUrl)){
        //Check if there is a folder, if not, create it and grant the highest authority.
       mkdir($basePutUrl, 0700);
    }
       $ping_url = genRandomString(8).time().".{$type}";
       $ftp_image_upload_url = $new_file.$ping_url;
       $local_file_url = $basePutUrl.$ping_url;

   if (file_put_contents($local_file_url, base64_decode(str_replace($result[1], '', $base64_image_content)))){
     ftp_upload(C('REMOTE_ROOT').$ftp_image_upload_url,$local_file_url);
         return $ftp_image_upload_url;
    }else{
         return false;
    }
  }else{
     return false;
 }
}
1

If you are dealing with data stored in a PostgreSQL database bytea field you will receive a stream when fetching the PDO. To process the data any further first transform the stream to usual data like this: $stream = $row['content']; rewind($stream); $data=stream_get_contents($stream);

Steffen
  • 601
  • 1
  • 5
  • 5