简体   繁体   中英

how do I display images from a php blob

i need to display an image from my php database. I know that the image is there and this should work could anyone tell me why it is not working? also i know that i am successfully connecting to the db so that is not the problem

here is page one index.php after i connected to the database

<?php
$query = mysql_query("SELECT * FROM data WHERE id= 1");    
while($data=mysql_fetch_array){
?>
<p> <?php echo $data['title']; ?></p>
<img src="img.php?id=1"/>    
<?php } ?>

and here is img.php

<?php
$id=$_GET['id'];
$query = mysql_query("SELECT * FROM data WHERE id= $id");
while($data= mysql_fetch_array($query)){
$image=$data['image'];
header("content-type: image/jpeg");
echo $image;
}
?>

i have been trying to find the answer to this for hours

You need to create an image data resource instead of echo $image;

$img=false;

if($img=@imagecreatefromstring($data['image'])){

   header('Accept-Ranges: bytes');
   header('Content-Type: image/jpeg');      
   imagejpeg($img);
   imagedestroy($img);

}
else{

   //error message

}

You can convert the image data into base64 and stick it in an tag.

   echo '<img src="data:image/jpeg;base64,' . base64_encode( $data['image'] ) . '" />';
  • Or other best solution is to store image as by its name only, not blob.

then call the image like that

<img src="<?php echo $data['image_name_in_database'].'gif' ;?>"  height="42" width="42">

this link may help you

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM