正确的标题php mysql blob显示图像 [英] Proper header php mysql blob display image

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

问题描述

我试图在我的PHP页面中显示来自mysql blob的图像(我知道它不是最佳实践),然后我会将其带入我的iOS应用程序。

i am attempting to display an image from mysql blob (i know its not best practice) in my PHP page which i will then bring into my iOS app.

我在设置我认为需要设置为图像的页眉时遇到问题。

I'm having trouble setting the page header which i believe needs to be set as an image.

所以,这显示了图像,但我不相信页眉是正确的:

So, this shows the image but I do not believe the page header is correct:

    //header("Content-type: image/png");
echo '<img src="data:image/png;base64,' . base64_encode( $row['image1'] ) . '" />';

但是,这不会显示我的图片:

However, this does not show my image:

    header("Content-type: image/png");
echo '<img src="data:image/png;base64,' . base64_encode( $row['image1'] ) . '" />';

我缺少什么?我相信图像应该显示何时标题设置为image / png但是我收到错误,因为错误而无法显示。

What am I missing? I believe the image should show when the header is set to image/png but i'm getting an error that it cannot be displayed because of errors.

谢谢!

推荐答案

header("Content-type: image/png");

这告诉您的浏览器您将要传递的原始二进制数据是PNG文件。因此,之后的任何输出都必须是二进制PNG。你不能放置HTML并期望它能够工作

This tells your browser that you're about to pass it raw binary data that is a PNG file. So anything output after that would have to be a binary PNG. You can't then place HTML and expect that to work

echo '<img src="data:image/png;base64,' . base64_encode( $row['image1'] ) . '" />';

这是因为您对图像进行了base64编码(将二进制文件转换为文本),并将其输出到浏览器然后告诉浏览器将其解释为base64。

This works because you're base64 encoding your image (translates binary into text), outputting that to the browser and then telling your browser to interpret it as base64.

如果要输出原始二进制数据,则必须重新排列顺序。所以这是你的HTML

If you want to output the raw binary data you have to rearrange the order. So here's your HTML

<img src="image.php" />

现在你会注意到 src 点到PHP文件。那是因为我们要让那个PHP文件返回一个图像。这是 image.php 看起来像

Now you'll note the src points to a PHP file. That's because we're going to have that PHP file return an image. Here's what image.php would look like

//Your query here
$row = mysqli_fetch_assoc($result);
header("Content-type: image/png");
echo $row['image1'];

这是有效的,因为浏览器将调用PHP文件,期待图像。 标题告诉浏览器这是一个PNG文件,您现在可以转储二进制PNG数据。

This works because the browser will call the PHP file, expecting an image. The header tells the browser that this is a PNG file and you can now dump your binary PNG data.

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

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