2015-11-06 235 views
0

我想從我的數據庫中獲取圖像並顯示uder圖像標籤,但它顯示的是破碎的圖像。哪裏有問題?從mysql blob檢索圖像,並在img標籤中顯示

$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>';} 
?> 

回答

0

類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> 
+0

這裏show.html使用查詢字符串ID鍵正從image.php圖像 –

相關問題