0

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'];
}
?>

3 Answers 3

1

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

Sign up to request clarification or add additional context in comments.

6 Comments

Ok. As a new leaner i just try this method also
Now it will not displays the special charecters by adding base64_encode. BUT NO IMAGE IS DISPLAYED IN THE BROWSER .
you copied the code what i gave? and second what does the type variable look like? i mean what header content does it store?
Sorry for the wastage of time . It works fine , That is a mistake from my side , I will not change the "$row" variable. Thanks.
No problem, happy to help, good luck with coding in the future
|
0

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'); ?>"/>

Comments

0

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;

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.