显示存储在 mysql blob 中的图像

发布于 2024-10-30 02:03:23 字数 498 浏览 4 评论 0原文

当我运行下面的代码时,它显示一个图像,该图像作为 blob 变量存储在 mysql 数据库中。问题是,如果我回显其他任何内容,即使是像 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();

?>

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

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

?>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

与君绝 2024-11-06 02:03:23

您可以将图像数据转换为 Base64 并将其粘贴到 标记中。目前,您正在尝试在图像数据之外写入文本,并且互联网浏览器认为您的文本是图像的一部分,从而引发错误。

尝试这样的操作:

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

注意,这不是最好的解决方案,因为它无法缓存并且速度相当慢,尤其是在移动电话上。查看caniuse 数据 URI

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.

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.

空心空情空意 2024-11-06 02:03:23

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

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

  1. 它对性能和渲染有重大影响。
  2. 您必须编写代码来呈现 blob 数据,而不仅仅是显示图像本身。

数据库驱动图像呈现的首选方法是将图像存储在目录中并将其文件名存储在数据库中。现在,当您希望显示图像时,您只需轮询数据库以获取要显示的文件名,然后只需将文件名包含到 HTML 属性中即可。

执行速度也快得多。

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

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

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

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

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.

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.

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.

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.

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

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

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

嘦怹 2024-11-06 02:03:23

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

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

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:

<p>my content</p>
<img src="pic.php?picid=1" />
<p>my Other Content</p>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文