如何转换要在网页中显示的BLOB图像

时间:2019-06-26 09:14:17

标签: php mysql image blob

我正在尝试将插入显示的BLOB数据类型转换为我的网页

我尝试使用basecode64,但无法正常工作

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

添加到数据库

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);

从数据库中检索

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> ;

它只是显示了损坏的图像。

3 个答案:

答案 0 :(得分:1)

尝试将图像返回为array。我认为您可以,因此请尝试以下操作,

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

由于您在此语句中使用addslashes

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

在显示这样的图像时,您必须添加stripslashes

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

而且我也看不到您要在任何地方插入$imagetmp。当我检查您的base 64 string时,它显示了损坏的图像。因此,我认为您的图片上传不正确。

答案 1 :(得分:0)

尝试

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

答案 2 :(得分:0)

$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'] ).'"/>';