如何从base64数据保存jpg文件 [英] How to save the jpg file from base64 data

查看:650
本文介绍了如何从base64数据保存jpg文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有图像的基数64数据,我想将它们保存为JPG文件在本地计算机中。

I have the base 64 data for my images and I want to save them in my local machine as JPG Files.

我的基数64代码以

My base 64 code starts with

data:image/jpg;base64,/...

我已经尝试通过以下代码保存它:

I already tried to save it by the following code:

 file_put_contents('MyFile.jpg', base64_decode($imgData1));

但是我无法打开创建的图像;它说文件似乎已损坏,损坏或太大。

But I cannot open the created image; it says "the file appears to be damaged, corrupt, or is too large". Could you please let me know what I'm missing.

另外,如果您需要进一步的说明,请告诉我您需要进一步的说明。

Also if you need any more clarification, please let me know which part you need more clarification.

推荐答案

在调用 base64_decode 之前取消预定义。

Get rid of the prefex before calling base64_decode.

<?php
// get rid of everything up to and including the last comma
$imgData1 = substr($imgData1, 1+strrpos($imgData1, ','));
// write the decoded file
file_put_contents('MyFile.jpg', base64_decode($imgData1));

这篇关于如何从base64数据保存jpg文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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