2011-05-05 189 views
1

我在从数据库中提取图像后显示图像时遇到问题。 我有2个单独的表,一个用于元数据,另一个用于保存实际的BLOB数据。该表是一个常规的BLOB,我只在每一行存储了60k个数据块。我想要渲染时重新编译图像。mysql blob图像不显示

我一直尽管收到此错误:

的图片:“http://imgStore.localhost/ImageBuilder/index/id/11”无法显示,因为它包含错误。

这里是流程如何工作。

/图片/图像/ ID/11将有图像的这里面像这样

<img src="http://imgStore.localhost/ImageBuilder/index/id/11" /> 

的图像控制器处理插入和编辑以及上市图像 而ImageBuilder只关注显示给定的图像

这里是表结构:

images ------ 
image_id INT 
image_name VARCHAR 
image_type VARCHAR 
image_size INT 
loaded_date DATETIME 

image_data -- 
image_data_id INT 
image_id  INT 
data   BLOB 

这里是我如何将文件保存到数据库中:

(注:我使用的是最新的Zend Framework)

(插入动作)------------------

$image = new ImgStore_Model_Images($form->getValues()); 
$image->setImage_size(((int) substr($form->image_file->getFileSize(), 0, -2) * 1024)); 
$image->setImage_type($form->image_file->getMimeType()); 
$image->setLoaded_date(time()); 
$image->setLoaded_by($user->get('contacts_id')); 
$mapper = new ImgStore_Model_ImagesMapper(); 
$image_id = $mapper->save($image); 

// open the uploaded file to read binary data 
$fp = fopen($form->image_file->getFileName(), "r"); 
$dataMapper = new ImgStore_Model_ImageDataMapper(); 
// loop through the file and push the contents into 
// image data entries 

while(!feof($fp)){ 
// Make the data mysql insert safe 
$binary_data = addslashes(fread($fp, 60000)); 

$data_entry = new ImgStore_Model_ImageData(); 
$data_entry->setImage_id($image_id); 
$data_entry->setImage_data($binary_data); 

    $dataMapper->save($data_entry); 
} 
fclose($fp); 

和这里是它是如何提取:

(动作)------------------

$this->_helper->_layout->disableLayout(); 

// get the image meta data 
$image_id = $this->_request->getParam('id', '0'); 

$mapper = new ImgStore_Model_ImagesMapper(); 
$info = $mapper->getInfo($image_id); 

// build the image and push it to the view 
$mapper   = new ImgStore_Model_ImageDataMapper(); 
$this->view->image = $mapper->buildImage($image_id); 
$this->view->name = $info->getImage_name(); 
$this->view->type = $info->getImage_type(); 
$this->view->size = $info->getImage_size(); 

(模型)-------- ----------

public function buildImage($image_id) 
{ 
    // get the image data 
    $sql = "SELECT image_data 
      FROM image_data 
      WHERE image_id='$image_id' 
      ORDER BY image_data_id ASC"; 
    $results = $this->_adapter->fetchAll($sql); 

    // piece together the image and return it 
    $image = NULL; 
    foreach($results as $row){ 
      $image .= $row['image_data']; 
    } 
    return $image; 
} #end buildImage function 

(视图)------------------

<?php 

header("Content-Type: " . $this->type); 
header('Content-Disposition: inline; filename="'.$this->name.'"'); 

echo $this->image; 

?> 

我试图使用小的图像足以只占用在image_data表中也有一行,所以我不认为它与image_data行的重新编译有任何关系。

任何帮助将不胜感激,我真的不知道这是什么问题。


编辑了一些用于显示目的的格式。

回答

1

我最近做了这样的事情,但使用了不同的方法进行渲染。如果请求URI指向一个实际的文件,Zend不会启动应用程序,因此我在文件控制器中创建了一个渲染操作,该操作在驱动器上创建了映像的副本。这使得扩展和管理变得更加容易,因为这些文件都位于一个中央数据库中,而且还提供了从磁盘读取性能的好处。这是我的公开行动:

public function openAction() { 
    $file = // find the file in the db 
    if(! $file) { 
     throw new Zend_Exception('File not found', 404); 
    } 

    $path = // get the filepath 
    if(! file_exists($path) || $this->_request->getParam('reload') == true) { 
     file_put_contents($path, $file->image_data); 
    } 

    $this->_redirect('document root relative path'); 
} 
0

使图像数据混淆数据库没有实际价值。 (从数据库获取数据也会比简单地从磁盘加载数据慢很多)

我建议您只将图像存储在文件系统上,并将元数据中的图像路径存储在数据库中。

+0

它不是直接查看,它是用于归档旧副本和使备份更简单。活动图像将被存储在文件系统中。 – 2011-05-05 23:03:38

+0

你也不应该在回答部分留言。 – 2011-05-06 18:58:40