在php中显示图像(Display image in php)

编程入门 行业动态 更新时间:2024-10-26 22:17:29
在php中显示图像(Display image in php)

如何通过从mysql数据库中检索图像来显示html中的图像。

我有以下代码,但只显示文本代替图像。 如何显示图像而不是图像文本。

码:

while ($row = mysql_fetch_assoc($result)) { //echo "<div>"; //echo "<div class=\"slide-image\">"; print $row['Image']; //echo "</div>"; echo "<div class=\"slide-text\">"; echo $row['Head']; echo $row['Description']; //echo "</div>"; //echo "</div>"; }

它将生成的JPEG显示为文本,您可能会想到这很难解释。

how to display image in html by retrieving it from mysql database.

i have the following code but only text is being displayed in place of image. How to display the image instead of the text of image.

Code:

while ($row = mysql_fetch_assoc($result)) { //echo "<div>"; //echo "<div class=\"slide-image\">"; print $row['Image']; //echo "</div>"; echo "<div class=\"slide-text\">"; echo $row['Head']; echo $row['Description']; //echo "</div>"; //echo "</div>"; }

It displays the resulting JPEG as text, which as you might imagine is very difficult to interpret.

最满意答案

您必须使用PHP设置Content-Type标头。 如果它是JPEG,它将是image/jpeg 。 你可以用:

header('Content-Type: image/jpeg');

务必在输出任何数据之前设置该标题。

更新

但是在您的文件中,当您从同一文档输出图像数据和文本数据时,您无法将其标题类型设置为多种类型。

一种选择是

while ($row = mysql_fetch_assoc($result)) { echo "<div>"; echo "<div class=\"slide-image\">"; echo "<img src='image_render.php?id=".$row['some_key_for_record']."'"; //print $row['Image']; we are moving it to an external page echo "</div>"; echo "<div class=\"slide-text\">"; echo $row['Head']; echo $row['Description']; echo "</div>"; echo "</div>"; }

并在image_render.php中

//code to pull data from db according to $_GET['id']; header('Content-Type: image/jpeg'); print $row['Image']; //no text data to be output from here

You have to set the Content-Type header with PHP. If it is JPEG it will be image/jpeg. You can set it with:

header('Content-Type: image/jpeg');

Be sure to set that header before outputting any data.

update

But in your file as you are outputting image data and text data from same document you cant set its header type as multiple types.

one option would be

while ($row = mysql_fetch_assoc($result)) { echo "<div>"; echo "<div class=\"slide-image\">"; echo "<img src='image_render.php?id=".$row['some_key_for_record']."'"; //print $row['Image']; we are moving it to an external page echo "</div>"; echo "<div class=\"slide-text\">"; echo $row['Head']; echo $row['Description']; echo "</div>"; echo "</div>"; }

and in image_render.php

//code to pull data from db according to $_GET['id']; header('Content-Type: image/jpeg'); print $row['Image']; //no text data to be output from here

更多推荐

本文发布于:2023-07-08 19:24:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1080007.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:图像   php   image   Display

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!