Oracle Blob as img src in PHP page

纵然是瞬间 提交于 2019-12-21 05:09:13

问题


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.


回答1:


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); ?>" />



回答2:


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.




回答3:


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.




回答4:


You may try this :

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



回答5:


<?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);
?>


来源:https://stackoverflow.com/questions/3056287/oracle-blob-as-img-src-in-php-page

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!