简体   繁体   English

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

[英]how to display an image stored in a database?

This is echo output of image I am storing an image as blob in a database as shown in the code below这是图像的回显输出我将图像作为 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));

An insert for an employee details员工详细信息的插入

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

This is to check if there is any error这是为了检查是否有任何错误

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

    }
    ?>

I am outputting am image as below though no image displayed虽然没有显示图像,但我正在输出如下图像

        $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

try this code, make sure your image extension is always "jpeg" or you need to make it dynamic.试试这个代码,确保你的图片扩展名总是“jpeg”,或者你需要让它动态。

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

Don't save image file as base64, you just save where image dir place.不要将图像文件保存为 base64,您只需保存图像目录所在的位置。 So when you call image just call the url to image dir place.因此,当您调用 image 时,只需将 url 调用到 image dir 位置即可。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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