如何从mysql blob显示图像 [英] How to display an Image from a mysql blob

查看:113
本文介绍了如何从mysql blob显示图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试显示来自MySQL Blob字段的图像.我尝试了几种不同的方法,但似乎都没有效果.

I am trying to display an image from a MySQL blob field. I have tried a few different things and none of them seem to work.

我尝试过:

  • header("Content-type: $type"); img src = $blobData;

header("Content-type: $type"); echo($blobData);

推荐答案

您可能会考虑的另一种选择(假设您使用的是Apache):

Another option you might consider (assuming you are on Apache):

为所有图像扩展名(png,jpg,gif)创建带有mod_rewrite.htaccess文件.

Create an .htaccess file with a mod_rewrite for all image extensions (png, jpg, gif).

已将其重定向到查找数据库中请求的图像的php脚本.如果存在,它将回显标头和BLOG.如果不存在,则返回标准404.

Have it redirect to a php script that looks up the image requested in the DB. If it is there, it echos out the header and BLOG. If it isn't there, it returns a standard 404.

这样,您可以:

<img src="adorablepuppy.jpg" />

然后重定向到ala:

RewriteEngine on
RewriteRule \.(gif|jpg|png)$ imagelookup.php

此脚本对图像进行查询,(显然)假定所请求的图像具有与URL中的文件名匹配的唯一键:

This script does a query for the image, which (obviously) assumes that the requested image has a unique key that matches the filename in the URL:

 $url = $_SERVER['REQUEST_URI'];
 $url_parts = explode("/", $url);
 $image_name = array_pop($url_parts);

现在,您只有图像文件名.进行查询(我将由您自己决定,以及所有验证方法,并检查该地址处的真实文件等).

Now you have just the image filename. Do the query (which I shall leave up to you, along with any validation methods and checks for real files at the address, etc.).

如果得出结果:

 header('Content-type: image/jpeg');
 header('Content-Disposition: inline; filename="adorablepuppy.jpg"');
 print($image_blog);

否则:

 header("HTTP/1.0 404 Not Found");

仅供参考:我不知道这在性能方面是否会很糟糕.但这会让您做我想做的事情,即使用简单的image元素将图像输出为服务器上的平面图像文件.我倾向于同意BLOB并不是最好的选择,但这确实避免了任何跨浏览器的问题.

FYI: I have no idea if this would be bad in terms of performance. But it would allow you to do what I think you want, which is output the image as though it were a flat image file on the server using a simple image element. I'm inclined to agree that BLOBs are not the best way to go, but this does avoid any cross-browser issues.

这篇关于如何从mysql blob显示图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆