加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > PHP教程 > 正文

php – 从dataURL创建/显示图像

发布时间:2020-12-13 14:10:31 所属栏目:PHP教程 来源:网络整理
导读:是否可以从POST收到的dataURL创建和显示图片文件? 就像是: ?$imgstr = $_POST["imgdata"]; //data:image/png;base64,.... etc (it's always PNG)echo base64_decode($imgstr); // idk what this really does? 我不能使用 img标签来显示它.它需要像“普通”
是否可以从POST收到的dataURL创建和显示图片文件?
就像是:
<?
$imgstr = $_POST["imgdata"]; //data:image/png;base64,.... etc (it's always PNG)
echo base64_decode($imgstr); // idk what this really does
?>

我不能使用< img>标签来显示它.它需要像“普通”图像文件一样.

如果是你的意见是
// Red dot graphic,stolen from Wikipedia
$imgstr = 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAUAAAAFCAYAAACNbyblAAAAHElEQVQI12P4//8/w38GIAXDIBKE0DHxgljNBAAO9TXL0Y4OHwAAAABJRU5ErkJggg==';

然后你就可以做你想做的事

// Grab the MIME type and the data with a regex for convenience
if (!preg_match('/data:([^;]*);base64,(.*)/',$imgstr,$matches)) {
    die("error");
}

// Decode the data
$content = base64_decode($matches[2]);

// Output the correct HTTP headers (may add more if you require them)
header('Content-Type: '.$matches[1]);
header('Content-Length: '.strlen($content));

// Output the actual image data
echo $content;
die;

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读