我正在从我的安卓应用程序向PHP发送base64
编码的图像。 有时它存储完整图像(4KB),有时(3KB)(同一图像)。 当我在毕加索中使用 URL 时,4KB 大小的图像工作正常,但 3KB 大小的图像无法加载,它显示解码错误。这是我的PHP代码(有时有效)
$encodedImage = str_replace(' ','+',$_POST['encodedProfileImage']);
$data = base64_decode($encodedImage);
$file = 'Pics/'. uniqid() . '.png';
$success = file_put_contents($file, $data);
$BASE_URL = 'http://domain.com/TestApp/';
然后我在PHP中执行SQL操作以存储图像路径。是否有可能在半解码图像(已损坏)上完成下一个代码操作。
您需要删除图像数据开头显示data:image/png;base64
的部分。实际的base64数据在此之后。
使用以下功能:-
function base64_to_png($base64_string, $output_file) {
$ifp = fopen($output_file, "wb");
$data = explode(',', $base64_string);
fwrite($ifp, base64_decode($data[1]));
fclose($ifp);
return $output_file;
}
如果你想使用str_replace
功能,那么可能低于工作方式。我不确定:)
$fname = filter_input(INPUT_POST, "name");
$encodedImage = filter_input(INPUT_POST, "image");
$encodedImage = str_replace('data:image/png;base64,', '', $encodedImage);
$encodedImage = str_replace(' ', '+', $encodedImage);
$encodedImage = base64_decode($encodedImage);
file_put_contents($fname, $encodedImage);
print "Image has been saved!";
希望它能帮助你:)