如何显示存储在数据库中的图像?

时间:2019-07-30 03:27:09

标签: php mysql

This is echo output of image我将图像作为blob存储在数据库中,如下面的代码所示

        <?php

            // extract form values
        if(isset($_POST['submit']))
        {
        $empnum = $_POST['emp_num'];
        $lastname = $_POST['emp_lname'];
        $firstname  = $_POST['emp_fname'];
        $initial = $_POST['emp_initial'];
        $job = $_POST['job'];
        $username = $_POST['emp_usr'];
        $password = $_POST['emp_pass'];
        $emp_bdate = $_POST['emp_bdate'];

        $check = getimagesize($_FILES["image"]["tmp_name"]);
        if($check !== false){
            $image = $_FILES['image']['tmp_name'];
            $imgContent = addslashes(file_get_contents($image));

插入员工详细信息

// build query
            $qry = "INSERT INTO employee VALUES(" .
                "'$empnum','$lastname','$firstname'," .
                "'$initial','$job'," .
                "'$username',PASSWORD('$password'),' $emp_bdate',' $imgContent')";
            }
            // execute query
            $added = mysqli_query($dbconn,$qry);

这是为了检查是否有错误

    // report results
        if(trim($added) != "")  
echo  "Record added successfully." . "<br>";
        else
        {
            echo "ERROR: Record could not be added<br>" . 
                 mysqli_error($dbconn);
        }

        // close connection
        mysqli_close($dbconn);

    }
    ?>

我没有显示图像,但正在输出以下图像

        $imageData =base64_encode($line['image']); 
    echo "<img src='data:image/jpeg;base64,$imageData' height='200' width='250' alt=''>'"




  [1]: https://i.stack.imgur.com/8HsOH.png

2 个答案:

答案 0 :(得分:0)

尝试使用此代码,确保您的图片扩展名始终为“ jpeg”,或者需要使其动态化。

<?php
  $imageData = base64_encode($line['image']);
  echo "<img src='data:image/jpeg;base64,$imageData' height='200' width='250'>";
?>

答案 1 :(得分:-1)

不要将图像文件另存为base64,您只需将图像目录保存在其中。因此,当您调用图片时,只需将网址调用到图片目录位置即可。