dragon_9000 2015-11-06 03:58
浏览 59

从mysql blob中检索图像并将其显示在img标记中

I am trying to fetch image from my database and show uder image tag but it is showing a broken image. Where is the problem?

$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>';}
?>
  • 写回答

1条回答 默认 最新

  • dongyuxin5353 2015-11-06 04:45
    关注

    Class 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>
    
    评论

报告相同问题?

悬赏问题

  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100
  • ¥15 关于#hadoop#的问题
  • ¥15 (标签-Python|关键词-socket)
  • ¥15 keil里为什么main.c定义的函数在it.c调用不了
  • ¥50 切换TabTip键盘的输入法
  • ¥15 可否在不同线程中调用封装数据库操作的类
  • ¥15 微带串馈天线阵列每个阵元宽度计算
  • ¥15 keil的map文件中Image component sizes各项意思