PHP从数据库错误中检索图像

时间:2017-02-02 12:58:43

标签: php database mysqli

我试图从数据库中回忆一个图像。我已经确定图像正确到达数据库,但是当我向下拉图像时,我得到了Blob文本。如何下拉图像以显示图像?

<?php
    //libs
    include 'library/config.php';
    include 'library/opendb.php';
    //query
    $query = "SELECT * FROM `upload` WHERE `postID`=".$elem["postID"].";";
    $result = mysqli_query($conn, $query);
    if (mysqli_num_rows($result) > 0) { // if results exist
        while ($row = mysqli_fetch_assoc($result)) { // assign db cont to variable
            $name = $row["name"];
            $size = $row["size"];
            $type = $row["type"];
            $content = $row["content"];
            // header info
            header("Content-length: $size");
            header("Content-type: $type");
            header("Content-Disposition: attachment; filename=$name");
            //display image
            echo "img", $content;
         }
         include 'library/closedb.php';
     ?>

2 个答案:

答案 0 :(得分:1)

使用

   $content = base64_encode($_row['content']);

显示jpeg图像。

http://php.net/manual/en/function.base64-encode.php

答案 1 :(得分:0)

$code_base64 = $row['content'];
$code_base64 = str_replace('data:image/jpeg;base64,','',$code_base64);
$code_binary = base64_decode($code_base64);
$image= imagecreatefromstring($code_binary);
header('Content-Type: image/jpeg');
imagejpeg($image);
imagedestroy($image);