如何在 PHP/HTML 中将 base64 字符串 (gif) 解码为图像 [英] How to decode a base64 string (gif) into image in PHP / HTML

查看:40
本文介绍了如何在 PHP/HTML 中将 base64 字符串 (gif) 解码为图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 base64 编码的字符串,我想将其转换为 PHP/HTML 中的图像.

I have a base64 encoded string that I would like to convert into an image in PHP / HTML.

这是我所拥有的:

$data = "R0lGODdhAAGAAKIAAP38+/3h3cjN5P3HwgAAAP8AoP8AGv8EIywAAAAAAAGAAAAD.....";
echo base64_decode($data);

//或

echo '<img src="data:image/gif;base64,' . base64_decode($data) . '" />';

这些都不起作用.有什么建议吗?

None of those work. Any suggestions?

非常感谢!卡塔林

推荐答案

在第一种情况下,您应该在回显解码的图像数据之前添加它:

In the first case you should add this before echoing the decoded image data:

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

在第二种情况下,改用这个:

In the second case, use this instead:

echo '<img src="data:image/gif;base64,' . $data . '" />';

这篇关于如何在 PHP/HTML 中将 base64 字符串 (gif) 解码为图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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