Oracle Blob 作为 PHP 页面中的 img src
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3056287/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Oracle Blob as img src in PHP page
提问by menkes
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:
我有一个当前使用文件服务器上的图像的站点。图像显示在用户可以根据需要拖放的页面上。这是使用 jQuery 完成的,图像包含在列表中。每个图像都非常标准:
<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:
但是,现在我需要引用在 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;
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.
但我需要[有效]将该图像作为 img 标签的 src 属性。我试过 imagecreatefromstring() 但这只是在浏览器中返回图像,忽略其他 html。我查看了数据 uri,但 IE8 大小限制排除了这一点。
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).
所以现在我有点卡住了。我的搜索不断提出使用 src 属性加载另一个包含图像的页面。但我需要图像本身实际显示在页面上。(注意:我说的是图像,意思是一页上至少有一张图像,但多达八张)。
Any help would be greatly appreciated.
任何帮助将不胜感激。
回答by ircmaxell
Well, you can do a few things. You can either make a page that will render the image
好吧,你可以做一些事情。您可以制作一个页面来呈现图像
<img src="image.php?id=123" />
That image.php page would have this:
该 image.php 页面将具有以下内容:
$sql = "SELECT image FROM images WHERE image_id = " . (int) $_GET['id'];
$stid = oci_parse($conn, $sql);
oci_execute($stid);
$row = oci_fetch_array($stid, OCI_ASSOC+OCI_RETURN_NULLS);
if (!$row) {
header('Status: 404 Not Found');
} else {
$img = $row['IMAGE']->load();
header("Content-type: image/jpeg");
print $img;
}
Or, you could base64 encode it into the src (note, not all browsers handle this well):
或者,您可以将其 base64 编码到 src 中(注意,并非所有浏览器都能很好地处理此问题):
<img src="data:image/jpeg;base64,<?php echo base64_encode($img); ?>" />
回答by Pekka
But I need to [efficiently] get that image as the src attribute of the img tag
但我需要[有效]将该图像作为 img 标签的 src 属性
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 use data:
URIsbut they
正如拜伦所说,公认的正确方法是将 blob 输出到独立的图像资源中,并使用img
标签嵌入。这是唯一的好办法。您可以使用data:
URI,但它们
- fatten your HTML code
- don't work in IE < 8 and are limited to 32 KB in IE 8,
- expand the data volume by 33%, and
- take away the brower's possibility to cache the image resource.
- 增加你的 HTML 代码
- 不适用于 IE < 8 并且在 IE 8 中限制为 32 KB,
- 将数据量扩大 33%,以及
- 取消浏览器缓存图像资源的可能性。
Almost never a good option.
几乎从来都不是一个好的选择。
回答by Byron Whitlock
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?
执行此操作的正常方法是使用<img src=/path/to/script?id=32>
字段。它不会作为链接显示在页面上。这有什么问题?要将图像数据嵌入到 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 headersshould be set so the browser doesn't download the image if it has it cached locally.
为了提高效率,您可以实现某种类型的缓存,即,将图像数据写入文件并在header(location...)
找到它时执行 a而不是再次查询数据库。此外,还应设置浏览器缓存标头,以便浏览器在本地缓存图像时不会下载该图像。
回答by Serty Oan
You may try this :
你可以试试这个:
$img = $row['IMAGE']->load();
print('<img src="data:image/png;base64,'.base64_encode($img).'" />');
回答by Karts
<?php
if(isset($_POST['']))//get the id
$roll_no=$_POST[''];
$conn = oci_connect("", "", "");//DB connection
$query = 'SELECT image FROM TABLE where id=:id';
$stmt = oci_parse ($conn, $query);
oci_bind_by_name($stmt, ':id', $id);
oci_execute($stmt);
$arr = oci_fetch_array($stmt, OCI_ASSOC);
$result = $arr['image']->load();
header("Content-type: image/JPEG");
echo $result;
oci_close($conn);
?>