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!

8
  • 2
    Why are you storing it as base64 in the first place?
    – Blender
    Apr 28, 2013 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? Apr 28, 2013 at 10:55
  • Long story, I don't have a choice anymore I just have to display them somehow! Apr 28, 2013 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. Apr 28, 2013 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, 2013 at 11:00

6 Answers 6

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.

5
  • 2
    don't work. It made an HTML code, but we need a jpeg streaming, call from <img source link to show images. Apr 28, 2013 at 11:14
  • thanks! what I actually did was change the last line of my code to this: echo base64_decode(substr($row['content'],23)); Apr 28, 2013 at 11:18
  • Sorry you feel like, please check dl.dropboxusercontent.com/u/6465647/image_decode.php.txt on your apache. It works fine for me :)
    – Menelaos
    Apr 28, 2013 at 11:20
  • 1
    @Kosmas Papadatos Kali sinexeia! Glad it helped!
    – Menelaos
    Apr 28, 2013 at 11:21
  • Will not browser automatically decode the image? Are not we decoding it before sending to the browser by using decode method in php?
    – muasif80
    Mar 13, 2015 at 1:47
11

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);
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 . '" />
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;
 }
}
0
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);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.