用PHP显示图像 [英] Display image in php

查看:77
本文介绍了用PHP显示图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何从mysql数据库中检索图像以html格式显示.

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.

代码:

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显示为文本,如您所想象的那样,很难解释.

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

推荐答案

您必须使用PHP设置 Content-Type 标头.如果是JPEG,则为 image/jpeg .您可以使用以下方法进行设置:

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.

但是在您从同一文档输出图像数据和文本数据的文件中,您不能将其标头类型设置为多种类型.

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.

一个选项是

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

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
 

这篇关于用PHP显示图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆