Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

BLOB data as SRC attribute of image tag

Tags:

html

php

mysql

blob

I tried to link stored image(blob data) inside MySQL DB to PHP but only thing I achieved was whole page of characters. This is what I did:

$query = "SELECT image FROM uploads WHERE id = {$id}";
$image_array = mysql_query($query, $connection);
$row = mysql_fetch_array($image_array);
$image = $row['image'];

echo $image;
// echo base64_decode($content);

This displays raw data how it is stored. I would like to link it into HTML tag or atleast display it on page, where I display a lot another stuff to so header('Content-type: image/png'); is not solution for me.

Any advice?

like image 261
Robert Avatar asked Jun 07 '13 13:06

Robert


1 Answers

To show the image correctly you just need to put one header with Content-type: image/png before echo

header("Content-type: image/png");
echo (base64_decode($row['image']));

If you want to place instead in an image tag you just need to use this code

echo '<img src="data:image/png;base64,' . $row['image'] . '" />'; 
like image 61
Fabio Avatar answered Sep 20 '22 14:09

Fabio