Views base64 encoded blob in HTML with PHP

13,372

Try this:

echo '<img src="data:image/png;base64,'.base64_encode($blob_data).'">';
Share:
13,372
Christopher
Author by

Christopher

Updated on June 04, 2022

Comments

  • Christopher
    Christopher almost 2 years

    having a bit of trouble Im trying to view a base64 encoded image which is held on my server as a blob. I want to view this inside either with an image tag or echo it as an html file which I can reference to with an image tag.

    The problem is the blob data returns wrong from my server.

        <?php
    
           $select = "SELECT `data` 
                   FROM  `sandbox`.`photos` 
                   WHERE `id` = 4
                   LIMIT 0 , 1";
    
            $result = mysql_query($select, $connection);
    
    
    
             while($row = mysql_fetch_array( $result )){
    
               $blob_data = $row[0];
    
               echo "$blob_data";
    
               }
    
        ?>
    

    The problem is that this retuns 5.533782782 * E.

    How do I view this as an image? Do I need to deencode it?

  • Christopher
    Christopher over 12 years
    thanks however I want to dynamically reference the images so to the database they go.
  • twsaef
    twsaef over 12 years
    What aspect of a 'dynamically referenced' image precludes storing it in the filesystem?
  • HorusKol
    HorusKol over 12 years
    Or you could do that - but I don't think that is 100% cross-browser