我需要我的 PHP 页面来显示 mysql 数据库中的 BLOB 图像

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13225726/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 05:01:01  来源:igfitidea点击:

I need my PHP page to show my BLOB image from mysql database

phpmysqlimageblobmime

提问by DBeslan

So my last question was on how to have my techID shown from a search :

所以我的最后一个问题是关于如何从搜索中显示我的 techID:

I am trying to have my "Details" page to reference two seperate parts of my server that are linked via techID

我试图让我的“详细信息”页面引用通过 techID 链接的服务器的两个独立部分

My new question is still on this page. I have added in an echo image as well. but am having trouble using Bloband having it display my image and not binary JPEG data. I've been trying to find another instance of this but cannot find any that fix my error.

我的新问题仍在此页面上。我还添加了回声图像。但是在使用Blob和让它显示我的图像而不是二进制 JPEG 数据时遇到问题。我一直在尝试找到另一个实例,但找不到任何可以解决我的错误的实例。

//Header ('Content-type: image/jpeg')
echo "<dt><strong>Technician Image:</strong></dt><dd>" . '<img src='.$row2['image'].' width="290" height="290">' . "</dd>";

and

$query_Recordset2 = "SELECT * FROM technician WHERE techID=" . $row1["techID"] ;
$Rs2 = mysql_query($query_Recordset2) or die(mysql_error());

Are the only changes I have put in so far from my last question (obviously including the fix I was given that worked).

到目前为止,我对上一个问题所做的唯一更改是(显然包括我得到的有效修复)。

What I do not understand is where and how to put 'Content-type: image/jpeg'to have my page recognize the image being linked is it's MIME TYPE image/jpeg.

我不明白的是在哪里以及如何'Content-type: image/jpeg'让我的页面识别链接的图像是 MIME TYPE image/jpeg

What I am seeing on my page is this

我在我的页面上看到的是这个

Technician Image: ?E??j??i`=7f$D??o"???????b???Ckkc??R??^M?;n~??0&m)J??R??E)JDR??E)JDR??E)JDR??E)JDR??E)JDR??E)JDSjR??)???+??N??.R,u????i??n9,???QX~ ????{(??????:???2?12??"??aV7?6???{???LP[?W??????? R$+? ??LMc'hM?5?o?PA????|???????.8??E?????Rn ??1?[??{??3>?rY??X??;?ǖ????u???z??'?vf?N葟 ??z?Q?????k??3???O???????S???,N? ?????[{+D? ?;?'?$?$?&?iJR??)JR??)JR??)JR??)JR??)JR??)JR??)JR??)JR??)JR??)JR??)JR??)?? width="290" height="290">

技术员图片:?E??j??i`=7f$D??o"???????b???Ckkc??R??^M?;n~??0&m)J?? R??E)JDR??E)JDR??E)JDR??E)JDR??E)JDR??E)JDSjR??)???+??N??.R,u??? ?i??n9,???QX~ ??{(??????:???2?12??"??aV7?6???{???LP[?W? ?????? R$+? ??LMc'hM?5?o?PA????|???????.8??E?????Rn ??1?[? ?{??3>?rY??X??; ?ǖ????u???z??'?vf?N葟 ??z?Q???k??3??? O???????S? ??,N? ??????[{+D? ?;?'?$?$?&?iJR??)JR??)JR??)JR? ?)JR??)JR??)JR??)JR??)JR??)JR??)JR??)JR??)?? width="290" height="290">

Obviously I have deleted a middle chunk so it's not massive. there is a little "Broken image" box that appears infront and when I right click and choose "Open image in new window" the URL it puts in is simply Content-type:or I get a forbidden access page with the url http:// localhost/Sim5Server/Pages/%EF%BF%BD%EF%BF%BD%EF%BF%BD%EF%BF%BD%EF%BF%BD%10JFIF%EF%BF%BD%01%02%EF%BF%BD%EF%BF%BDd%EF%BF%BDd%EF%BF%BD%EF%BF%BD%EF%BF%BD%EF%BF%BD%EF%BF%BDC%EF%BF%BD

显然我删除了一个中间块,所以它不是很大。有一个小的“损坏的图像”框出现在前面,当我右键单击并选择“在新窗口中打开图像”时,它输入的 URL 很简单,Content-type:或者我得到了一个带有 url 的禁止访问页面http:// localhost/Sim5Server/Pages/%EF%BF%BD%EF%BF%BD%EF%BF%BD%EF%BF%BD%EF%BF%BD%10JFIF%EF%BF%BD%01%02%EF%BF%BD%EF%BF%BDd%EF%BF%BDd%EF%BF%BD%EF%BF%BD%EF%BF%BD%EF%BF%BD%EF%BF%BDC%EF%BF%BD

I have put a space in that url since it is not a link for the internet.

我在该网址中放置了一个空格,因为它不是互联网的链接。

I have only used normal BLOB type as I just need it as a small less than 64Kb image

我只使用了普通的 BLOB 类型,因为我只需要它作为一个小于 64Kb 的小图像

回答by newfurniturey

In your current case, you have two upfront options.

在您当前的情况下,您有两个前期选择。

The first, and the one I don't recommend if you have numerous images like this, is to use inline base64 encoding. This is done with:

第一个,如果您有大量这样的图像,我不建议使用内联 base64 编码。这是通过以下方式完成的:

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

A copy/paste version, using your existing code:

复制/粘贴版本,使用您现有的代码:

echo '<dt><strong>Technician Image:</strong></dt><dd>'
     . '<img src="data:image/jpeg;base64,' . base64_encode($row2['image']) . '" width="290" height="290">'
     . '</dd>';

The second method is to create an "image" PHP file that takes the ID of the image in the database as a query-string parameter and outputs the image. So, your HTML would look something like:

第二种方法是创建一个“图像”PHP 文件,该文件将数据库中图像的 ID 作为查询字符串参数并输出图像。所以,你的 HTML 看起来像:

<img src="image.php?id=<?php echo $image_id; ?>" />

And your PHP page would look something similar to:

您的 PHP 页面将类似于:

<?php
$id = (isset($_GET['id']) && is_numeric($_GET['id'])) ? intval($_GET['id']) : 0;
$image = getImageFromDatabase($id); // your code to fetch the image

header('Content-Type: image/jpeg');
echo $image;
?>

回答by Musa

The only way you can output an image from the same page as the document is with a data uri.

您可以从与文档相同的页面输出图像的唯一方法是使用数据 uri

echo "<dt><strong>Technician Image:</strong></dt><dd>" . 
     '<img src="data:image/jpeg;base64,'.
      base64_encode($row2['image']).
      '" width="290" height="290">' . "</dd>";

回答by user2624107

The right code should be as below. You have to use stream_get_contents() to change resource id into string.

正确的代码应该如下。您必须使用 stream_get_contents() 将资源 ID 更改为字符串。

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

回答by Sikander

I think best solution is to store path of the image in the database instead of storing the whole image as BLOB. And then for showing the image on webpage set the src property of the img tag to the path stored in database.

我认为最好的解决方案是将图像的路径存储在数据库中,而不是将整个图像存储为 BLOB。然后为了在网页上显示图像,将 img 标签的 src 属性设置为存储在数据库中的路径。