user3789039
user3789039

Reputation: 93

display an image stored in database as blob type.in PHP?

Save the image in the database as blob type. But i am not able to display these image below i will give the code for dislaying the image.

It will gives an output like some codes with special symbols... How to displays the image properly. I am new to php.``

<?php
while($row = mysql_fetch_assoc($result)){
    $type=$row['type']; 
    header("content-type : $type");
    echo $row['src'];
}
?>

Upvotes: 0

Views: 1072

Answers (3)

Christian
Christian

Reputation: 28165

Don't use base64_encode(), it doesn't play well with most browsers. Instead, do something like so:

File main.php (could be plain html):

<img src="image.php?id=5"/>

File image.php:

// replce this with your db-loading code
$image = read_from_db((int)$_GET['id']);

// important: this tells the client what kind of image you have
header('Content-Type: ' . $image->mime);

// give the browser an indication of the size of the image
header('Content-Length: ' . strlen($image->data));

// TODO Put other useful headers here

// send the actual image data to the browser
echo $image->data;

Upvotes: 0

Parth Kumar
Parth Kumar

Reputation: 179

You need to first read the image file and then you can echo the image. I have pasted a sample code below.

<?php
function base64_encode_image ($filename=string,$filetype=string) {
    if ($filename) {
    $imgbinary = fread(fopen($filename, "r"), filesize($filename));
    return 'data:image/' . $filetype . ';base64,' . base64_encode($imgbinary);
    }
}
?>
<img src="<?php echo base64_encode_image('img.png','png'); ?>"/>

Upvotes: 0

user1130272
user1130272

Reputation:

You need base64_encode

 echo '<img src="data:image/jpeg;base64,'.base64_encode( $row['src'] ).'"/>';

And just an advice, its easier to store the image on the server and saving the image name in the DB than make a match

Upvotes: 1

Related Questions