【问题标题】:retrieve image from mysql blob and display it in img tag从 mysql blob 中检索图像并将其显示在 img 标签中
【发布时间】:2016-02-07 03:43:23
【问题描述】:

我正在尝试从我的数据库中获取图像并显示 uder 图像标签,但它显示的是损坏的图像。问题出在哪里?

$query="SELECT  imagetype,Attachments from Events where E_id='$id' ";
 $result = mysqli_query($link,$query);
 // $image = mysql_result($result,0);

 // echo "<img src =".$image."/>";
if ($row = mysqli_fetch_array($result)){
    // echo $row['Attachments'];
  header('content-type: image/jpeg');
  // exit();
  echo '<img src="data:image/jpeg;base64,'.base64_encode($row['Attachments']).'" alt="photo"><br>';}
?>

【问题讨论】:

    标签: php database image blob


    【解决方案1】:

    类 image.php

     <?php
            class image extends mysqli {
             public $a = array();
    
                public function __construct($host, $user,$password,$db_name) {
                    parent::mysqli($host, $user,$password,$db_name);
                }
                public function save() {
                    $num = count($this->a);
                    $i = 0;
                    foreach ($this->a as $c) {
                        $ext = explode(".", $c);
                        $this->query("INSERT INTO Events set name='{$c}',Attachments='" . $this->real_escape_string(file_get_contents($c)) . "',imagetype='" . $ext[count($ext) - 1] . "'");
                        $i++;
                    } 
                    return $i == $num;
                }
    
                public function select($id) {
                    $rs = array();
                    $sql = "SELECT * FROM Events WHERE 1";
                    if ($id != NULL) {
                        $sql.=' AND E_id=' . $id;
                    }
                    $query = $this->query($sql);
                    if ($query->num_rows) {
                        while ($line = $query->fetch_object()) {
                            $rs[] = $line;
                        }
                    }
                    return $rs;
                }
    
    
            }
            $image_url=array('');//contain array of image url if you would like to save 
            $obj=new image($host, $user,$password,$db_name);
            $obj->a=$image_url;
            $obj->save();
            $row=$obj->select(isset($_GET['id'])?$_GET['id']:1);
            header("Content-type: image/{$row[0]->imagetype}");
            echo $row[0]->image;
    

    show.html

     <html>
        <head></head>
        <body>
            <img src="image.php?id=<?php echo $_GET['id']?>" width="175" height="200" />
        </body>
    </html>
    

    【讨论】:

    • 这里 show.html 使用带有 id 键的查询字符串从 image.php 获取图像
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-01-13
    • 1970-01-01
    • 2013-01-13
    • 1970-01-01
    • 1970-01-01
    • 2012-05-02
    相关资源
    最近更新 更多