How to convert BLOB Image to be displayed in Web page

Viewed 1401

I am trying to convert the BLOB data type I inserted to display to my webpage

I tried using the basecode64 but it doesn't work

<td> <img src='data:image/jpg;base64,".base64_encode($photo->image)."'/></td> ;

adding to database

echo $imagename=$_FILES["myimage"]["name"]; 

//Get the content of the image and then add slashes to it 
$imagetmp=addslashes (file_get_contents($_FILES['myimage']['tmp_name']));

$photo_image->auditID = $auditID;
$photo_image->critID = $critID;
$photo_image->image = $img;
$photo_image->image_name = $imagename;

$dao->add($photo_image);

retrieving from the database

public  function retrieveName($photo_image) {
        $sql = 'select * from audit_trans_photo where auditID=:auditID and critID=:critID';
        $result = array();

        $connMgr = new ConnectionManager();
        $conn = $connMgr->getConnection();

        $stmt = $conn->prepare($sql);
        $stmt->setFetchMode(PDO::FETCH_ASSOC);
        $stmt->bindParam(':auditID', $photo_image->auditID, PDO::PARAM_INT);
        $stmt->bindParam(':critID', $photo_image->critID, PDO::PARAM_INT);

        $stmt->execute();

        if($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
            $result = new Audit_Trans_Photo($row['auditID'], $row['critID'], $row['image'], $row['image_name']);
        }

        return $result;
    }

edit-view.php

 $dao3 = new Audit_Trans_PhotoDAO();
 $photo_image = new Audit_Trans_Photo();
 $photo_image->auditID = $auditID;
 $photo_image->critID = $n_transaction->critID;

 $photo  = $dao3->retrieveName($photo_image);
 $image_p = $photo->image;

 echo <td> <img src='data:image/jpg;base64,".base64_encode($photo->image)."'/></td> ;

It just shows a broken image.

3 Answers

Try to return image as an array. I think you do, So Try the following,

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

Because of you are using addslashes in this statement,

$imagetmp = addslashes (file_get_contents($_FILES['myimage']['tmp_name']));

You have to add stripslashes when displaying the image like this,

echo '<img src="data:image/jpeg;base64,'.base64_encode(stripslashes($photo->image)).'" />';

And also I don't see you are inserting $imagetmp anywhere. When I checked your base 64 string it shows a broken image. So I assume that your image doesn't upload correctly.

try this

echo '<img src="data:image/jpeg;base64,'.base64_encode($photo->image) .'" />';
$db = mysqli_connect("localhost","root","password","yourdbname");
$sql = "SELECT * FROM products WHERE id = $id";
$sqlData = mysqli_query($db, $sql);
$result=mysqli_fetch_array($sqlData);
echo '<img src="data:image/jpeg;base64,'.base64_encode( $result['image'] ).'"/>';
Related