PHP base64_docode 保存损坏的图像
PHP base64_docode saves corrupt image
我正在从我的 android 应用向 PHP 发送 base64
编码图像。有时它存储完整图像 (4KB),有时 (3KB)(同一图像)。当我在 picasso 中使用 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!";
希望对您有所帮助:)
我正在从我的 android 应用向 PHP 发送 base64
编码图像。有时它存储完整图像 (4KB),有时 (3KB)(同一图像)。当我在 picasso 中使用 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!";
希望对您有所帮助:)