Oracle Blob 在 PHP 页面中作为 img src
我有一个网站当前使用文件服务器上的图像。这些图像显示在页面上,用户可以根据需要拖放每个图像。这是使用 jQuery 完成的,图像包含在列表中。每个图像都非常标准:
<img src='//network_path/image.png' height='80px'>
但是现在我需要引用在 Oracle 数据库中存储为 BLOB 的图像(对此没有选择,因此不进行优劣讨论)。我可以毫无问题地检索 BLOB 并使用它自己显示:
$sql = "SELECT image FROM images WHERE image_id = 123";
$stid = oci_parse($conn, $sql);
oci_execute($stid);
$row = oci_fetch_array($stid, OCI_ASSOC+OCI_RETURN_NULLS);
$img = $row['IMAGE']->load();
header("Content-type: image/jpeg");
print $img;
但我需要[有效地]将该图像获取为 img 标记的 src 属性。我尝试了 imagecreatefromstring() 但这只是返回浏览器中的图像,忽略其他 html。我查看了 data uri,但 IE8 的大小限制排除了这一点。
所以现在我有点陷入困境。我的搜索不断出现使用 src 属性来加载包含图像的另一个页面。但我需要图像本身实际显示在页面上。 (注:我说的是图像,意思是一页上至少有一张图像,但最多八张图像)。
任何帮助将不胜感激。
I have a site that currently uses images on a file server. The images appear on a page where the user can drag and drop each as is needed. This is done with jQuery and the images are enclosed in a list. Each image is pretty standard:
<img src='//network_path/image.png' height='80px'>
Now however I need to reference images stored as a BLOB in an Oracle database (no choice on this, so not a merit discussion). I have no problem retrieving the BLOB and displaying on it's own using:
$sql = "SELECT image FROM images WHERE image_id = 123";
$stid = oci_parse($conn, $sql);
oci_execute($stid);
$row = oci_fetch_array($stid, OCI_ASSOC+OCI_RETURN_NULLS);
$img = $row['IMAGE']->load();
header("Content-type: image/jpeg");
print $img;
But I need to [efficiently] get that image as the src attribute of the img tag. I tried imagecreatefromstring() but that just returns the image in the browser, ignoring the other html. I looked at data uri, but the IE8 size limit rules that out.
So now I am kind of stuck. My searches keep coming up with using a src attribute that loads another page that contains the image. But I need the image itself to actually show on the page. (Note: I say image, meaning at least one image but as many as eight on a page).
Any help would be greatly appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
嗯,你可以做一些事情。您可以制作一个页面来渲染图像
image.php 页面将具有以下内容:
或者,您可以将其进行 base64 编码到 src 中(注意,并非所有浏览器都能很好地处理此问题):
Well, you can do a few things. You can either make a page that will render the image
That image.php page would have this:
Or, you could base64 encode it into the src (note, not all browsers handle this well):
正如 Byron 已经说过的那样,可接受的正确方法是将 blob 输出到独立的图像资源中,并使用
img< 嵌入该图像/代码> 标签。这是唯一的好办法。您可以使用
data:
URI 但它们几乎从来都不是一个好的选择。
As Byron already says, the accepted and right way is to output the blob in an independent image resource, and to embed that using an
img
tag. It's the only good way. You can usedata:
URIs but theyAlmost never a good option.
执行此操作的正常方法是使用
字段。它将显示在页面上,而不是作为链接。这有什么问题吗?您想将图像数据嵌入到 HTML 中吗?
为了提高效率,您可以实现某种类型的缓存,即,将图像数据写入文件并在找到它时执行
header(location...)
而不是再次查询数据库。此外,还应设置 浏览器缓存标头,以便浏览器不会下载图像(如果有本地缓存)。The normal way to do this is with a
<img src=/path/to/script?id=32>
field. It will show up on the page not as a link. What is the problem with this? Do you want to embed the image data into HTML?To make it more efficient, you can implement some type of caching ie, write the image data to a file and do a
header(location...)
if you find it instead of querying the db again. Also the browser caching headers should be set so the browser doesn't download the image if it has it cached locally.你可以尝试这个:
You may try this :