如何从base64编码的jpeg创建GD图像​​?

时间:2022-10-24 14:16:09

I have an upload Api that as a response object delivers (along with other stuff inside a Json object) a base64 encoded jpeg image.

我有一个上传Api作为响应对象(与Json对象内的其他东西一起)传递base64编码的jpeg图像。

I create the encoded image as so:

我创建编码图像如下:

$im; // gd image resource
ob_start();
imagejpeg($im);
$data = base64_encode(ob_get_clean());

The data then is put inside a form field using javascript and submitted.

然后使用javascript将数据放入表单字段并提交。

How can I create a GD resource from that again so that I actually can save that image as a file?

如何再次创建GD资源,以便我实际上可以将该图像保存为文件?

Everything in PHP.

PHP中的一切。

3 个解决方案

#1


30  

You can use imagecreatefromstring() function:

你可以使用imagecreatefromstring()函数:

$data = base64_decode($_POST['image_as_base64']);

$formImage = imagecreatefromstring($data);

"String" does not mean a "real" string. In that case it means bytes/blob data.

“字符串”并不意味着“真正的”字符串。在这种情况下,它意味着字节/ blob数据。

#2


14  

How can I create a GD resource from that again so that I actually can save that image as a file?

如何再次创建GD资源,以便我实际上可以将该图像保存为文件?

Are you sure you need to do that extra step? How about:

你确定你需要做那个额外的步骤吗?怎么样:

file_put_contents('MyFile.jpg', base64_decode($_POST['MyFormField']));

#3


0  

I did by this way:

我是这样做的:

// input is in format: data:image/png;base64,...
$str="data:image/png;base64,"; 
$data=str_replace($str,"",$_POST['image']); 
$data = base64_decode($data);
file_put_contents('tmp/'.mktime().'.png', $data);

#1


30  

You can use imagecreatefromstring() function:

你可以使用imagecreatefromstring()函数:

$data = base64_decode($_POST['image_as_base64']);

$formImage = imagecreatefromstring($data);

"String" does not mean a "real" string. In that case it means bytes/blob data.

“字符串”并不意味着“真正的”字符串。在这种情况下,它意味着字节/ blob数据。

#2


14  

How can I create a GD resource from that again so that I actually can save that image as a file?

如何再次创建GD资源,以便我实际上可以将该图像保存为文件?

Are you sure you need to do that extra step? How about:

你确定你需要做那个额外的步骤吗?怎么样:

file_put_contents('MyFile.jpg', base64_decode($_POST['MyFormField']));

#3


0  

I did by this way:

我是这样做的:

// input is in format: data:image/png;base64,...
$str="data:image/png;base64,"; 
$data=str_replace($str,"",$_POST['image']); 
$data = base64_decode($data);
file_put_contents('tmp/'.mktime().'.png', $data);