![](/img/trans.png)
[英]How to fetch images from mysql database directly and display them in json format using php.How to do this?
[英]How can I load and display images directly from a mysql blob field using php without saving them as files?
我将图像存储在mysql中作为mediablobs,我想直接从数据库加载(显示)它们,而不必先将它们保存为文件。
我需要将多个图像加载到一个表(拇指)中,因此header命令不会这样做。 这就是我做的:
$imagePath = 'files' . DS . 'recipe_' . $recipe['Recipe']['id'] . '.tmp';
file_put_contents($imagePath ,$recipe['Recipe']['image']);
echo $html->image(DS . $imagePath);
您创建一个名为getimage.php的脚本。 表中的拇指应该是这样的:
<img src='<path>/getimage.php?id=<image_id>' />
scipt getimage.php执行数据库查找,然后输出如下内容:
...
header('Content-Type: image/jpeg');
header('Content-Length: ' . strlen($recipe['Recipe']['image']) );
echo $recipe['Recipe']['image'];
...
另一种选择是在httpd.conf或.htaccess文件中使用apache重定向,该文件将所有图像请求( .gif | .jpg | * .png | etc等)重定向到您的getImage.php脚本。 然后getImage.php将从数据库中提取,发送适当的头并输出二进制数据。
这个优于Flavius Stef的答案是,你的标签可以有一个常规的源(/path/to/image.jpg),而不是一个明显的检索系统。 它还会同时清理您的apache访问日志。
我搞定了....
这就是我放在表格中的内容:
'<img src= "/recipes/download/" . $recipe['Recipe']['id']) . '"/>'
这是下载动作:
function download($id) {
Configure::write('debug', 0);
$file = $this->Recipe->findById($id);
header('Content-type: image/png' );
header('Content-length: ' . strlen($file['Recipe']['thumb'])); // some people reported problems with this line (see the comments), commenting out this line helped in those cases
header('Content-Disposition: attachment; filename="'.$file['Recipe']['thumb'].'"');
echo $file['Recipe']['thumb'];
exit();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.