将 Base64 图像转换为文件 return 无效 image/data

Converting Base64 image to file return invalid image/data

过去两天我一直在尝试使用不同的方法将 base64 字符串转换为图像文件,但每次输出文件都没有显示图像(它确实显示大小和一些编码数据)。

备注: 1. 浏览器将文件解释为文档而不是图像 mime 但我很确定这不是问题,因为我尝试在本地下载文件并且结果相同。 2.我尝试对比了在线工具codebeautify.org/base64-to-image-converter的输出结果,好像图像文件中的数据和我的数据不一样。 3.目录和文件有775权限,apache有chown

此处为 Base64 字符串:https://dpaste.de/gP7D/raw

方法一:

list($type, $profile_image) = explode(';', $profile_image);
                list(,$extension) = explode('/',$type);
                list(,$profile_image)      = explode(',', $profile_image);
                if($extension == 'jpeg'){$extension = "jpg";}
                $filePath = '../uploads/profile_images/'.$uname.'.'.$extension; // using uname instead of unique id to not expose the uqniue key/session
                $profile_image = base64_decode($profile_image);
                file_put_contents($filePath, $profile_image);

方法二:

list($type, $profile_image) = explode(';', $profile_image);
                list(,$extension) = explode('/',$type);
                list(,$profile_image)      = explode(',', $profile_image);
                $filePath = '../uploads/profile_images/'.$uname.'.'.$extension;
                $ifp = fopen($filePath, 'wb');
                fwrite($ifp, base64_decode($profile_image));
                fclose($ifp);

我做错了什么?

更新: 显然,Apache/php.ini 的最大 max_input_vars 为 5000,而我的 base64 字符串要高得多。此 post 可以标记为已解决。

您似乎必须删除 base64 编码字符串的开头,这不是 base64 编码数据的一部分。 从 base64 字符串中删除 "data:image/jpeg;base64"

我做了一些简单的测试并得到了图像...

Apache/Php.ini 文件的最大 max_input_vars 为 5000,而我的 base64 字符串要高得多。