Oracle Blob 在 PHP 页面中作为 img src

发布于 2024-09-06 02:29:23 字数 780 浏览 5 评论 0原文

我有一个网站当前使用文件服务器上的图像。这些图像显示在页面上,用户可以根据需要拖放每个图像。这是使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

悲歌长辞 2024-09-13 02:29:23

嗯,你可以做一些事情。您可以制作一个页面来渲染图像

<img src="image.php?id=123" />

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;
}

或者,您可以将其进行 base64 编码到 src 中(注意,并非所有浏览器都能很好地处理此问题):

<img src="data:image/jpeg;base64,<?php echo base64_encode($img); ?>" />

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:

$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):

<img src="data:image/jpeg;base64,<?php echo base64_encode($img); ?>" />
坦然微笑 2024-09-13 02:29:23

但我需要[有效地]获取该图像作为 img 标签的 src 属性

正如 Byron 已经说过的那样,可接受的正确方法是将 blob 输出到独立的图像资源中,并使用 img< 嵌入该图像/代码> 标签。这是唯一的好办法。您可以使用 data: URI 但它们

  1. 使您的 HTML 代码变得臃肿
  2. 不会 不能在 IE 中工作8 且在 IE 8 中限制为 32 KB、
  3. 数据量扩大33%,并
  4. 取消浏览器缓存图片资源的可能性。

几乎从来都不是一个好的选择。

But I need to [efficiently] get that image as the src attribute of the img tag

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: URIs but they

  1. fatten your HTML code
  2. don't work in IE < 8 and are limited to 32 KB in IE 8,
  3. expand the data volume by 33%, and
  4. take away the brower's possibility to cache the image resource.

Almost never a good option.

小巷里的女流氓 2024-09-13 02:29:23

执行此操作的正常方法是使用 字段。它将显示在页面上,而不是作为链接。这有什么问题吗?您想将图像数据嵌入到 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.

风铃鹿 2024-09-13 02:29:23

你可以尝试这个:

$img = $row['IMAGE']->load();
print('<img src="data:image/png;base64,'.base64_encode($img).'" />');

You may try this :

$img = $row['IMAGE']->load();
print('<img src="data:image/png;base64,'.base64_encode($img).'" />');
把时间冻结 2024-09-13 02:29:23
<?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);
?>
<?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);
?>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文