php 显示存储在 mysql blob 中的图像

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

displaying an image stored in a mysql blob

phpmysqlblob

提问by kevin

when i run the code below it displays an image that is stored in a mysql Db as a blob variable. The problem is if I echo out anything else, even something as simple as echo '--------'; before I call the image, the image will not display. only random characters display. if i echo out anything after the image the image displays but nothing does after it. Can someone tell me why this is and how i can go about displaying other items and the image together, thanks

当我运行下面的代码时,它会显示一个图像,该图像作为 blob 变量存储在 mysql Db 中。问题是我是否回显了其他任何东西,甚至是像 echo '--------'; 这样简单的东西。在我调用图像之前,图像不会显示。只显示随机字符。如果我在图像显示后回显任何内容,但在它之后没有任何反应。有人能告诉我为什么会这样以及我如何将其他项目和图像一起显示,谢谢

<?php

include("inc/library.php");

connectToDatabase();

$sql = "SELECT * FROM theBlogs WHERE ID = 1;";

$result = mysql_query($sql) or die(mysql_error());  
$row = mysql_fetch_array($result);

header("Content-type: image/jpeg");
echo $row['imageContent'];
$db->close();

?>

回答by

You can convert the image data into base64 and stick it in an <img>tag. Currently, you are trying to write text outside of the image data, and the internet browser thinks your text is part of the image and thus throws an error.

您可以将图像数据转换为 base64 并将其粘贴在<img>标签中。目前,您正在尝试在图像数据之外写入文本,而 Internet 浏览器认为您的文本是图像的一部分,因此会引发错误。

Try something like this:

尝试这样的事情:

echo '<img src="data:image/jpeg;base64,' . base64_encode( $row['imageContent'] ) . '" />';
echo 'Hello world.';

Note, this isn't the best solution because it cannot be cached and is fairly slow, especially on mobile phones. Check out the caniuse for data URIs.

请注意,这不是最佳解决方案,因为它无法缓存并且速度相当慢,尤其是在手机上。查看caniuse for data URIs

回答by Skittles

Well...the answer to why it will do what you described is because of your use of the header() function. In PHP, you cannot print anything prior to a header call as this directs the web server to prepare a content header. Usually those completely supersede all content.

嗯......为什么它会做你描述的事情的答案是因为你使用了 header() 函数。在 PHP 中,您不能在标头调用之前打印任何内容,因为这会指示 Web 服务器准备内容标头。通常那些完全取代所有内容。

Secondly, I would like to mention that storing images in a database is usually a bad idea for two reasons.

其次,我想提到将图像存储在数据库中通常是一个坏主意,原因有两个。

  1. It has a significant impact on performance and rendering.
  2. You have to write code render the blob data rather than just displaying the image itself.
  1. 它对性能和渲染有重大影响。
  2. 您必须编写代码来渲染 blob 数据,而不仅仅是显示图像本身。

The preferred method for database driven image presentation would be that you would have the images stored in a directory and their filenames stored in the database. Now, when you wish to display the images, you merely need to poll the DB for what filenames you want to display and then simply include the filename into an HTML attribute.

数据库驱动图像呈现的首选方法是将图像存储在目录中,并将它们的文件名存储在数据库中。现在,当您希望显示图像时,您只需要轮询 DB 以了解要显示的文件名,然后简单地将文件名包含到 HTML 属性中。

The execution is much faster also.

执行速度也快得多。

Also, I would like to point out that if you wish to have a script actually do your rendering, you would want that script to define your header and then echo or print the image blob after you define the header.

另外,我想指出的是,如果您希望让脚本实际进行渲染,您会希望该脚本定义您的标题,然后在您定义标题后回显或打印图像 blob。

Please note that when you create your html tag...that in the src attribute, you would then make it something more like this;

请注意,当您创建 html 标签时……在 src 属性中,您将使其更像这样;

<img src="image.php?id=<some_number>">

Now, your image.php file will spit out the image data into the tag.

现在,您的 image.php 文件会将图像数据吐出到标签中。

回答by Johann du Toit

Just a idea, maybe you can seperate the logic to display the image from the content. What I mean is that you could create a file like pic.php that accepts the id/filename (Not sure if you use filenames or id's to refer to the image) and then displays the image. Then you could simply refrence images in your HTML by for example doing something like this:

只是一个想法,也许您可​​以将显示图像的逻辑与内容分开。我的意思是你可以创建一个像 pic.php 这样的文件,它接受 id/filename(不确定你是使用文件名还是 id 来引用图像),然后显示图像。然后你可以简单地在你的 HTML 中引用图像,例如做这样的事情:

<p>my content</p>
<img src="pic.php?picid=1" />
<p>my Other Content</p>