我在从数据库中提取图像后显示图像时遇到问题。 我有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行的重新编译有任何关系。
任何帮助将不胜感激,我真的不知道这是什么问题。
编辑了一些用于显示目的的格式。
它不是直接查看,它是用于归档旧副本和使备份更简单。活动图像将被存储在文件系统中。 – 2011-05-05 23:03:38
你也不应该在回答部分留言。 – 2011-05-06 18:58:40