从 dataURL 创建/显示图像 [英] Create/display image from dataURL

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

问题描述

是否可以从 POST 接收的 dataURL 创建和显示图片文件?比如:

is it possible to create and display a picture file from dataURL received by POST? Something like:

<?
$imgstr = $_POST["imgdata"]; //data:image/png;base64,.... etc (it's always PNG)
echo base64_decode($imgstr); // idk what this really does
?>

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

I can't use <img> tag to display it. It needs to act like a "normal" image file.

推荐答案

如果例如你的输入是

// Red dot graphic, stolen from Wikipedia
$imgstr = 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAUAAAAFCAYAAACNbyblAAAAHElEQVQI12P4//8/w38GIAXDIBKE0DHxgljNBAAO9TXL0Y4OHwAAAABJRU5ErkJggg==';

然后你就可以随心所欲了

Then you can do what you want with

// 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;

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

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