base64 decoding php android


base64 decoding php android

我正在开发一个应用程序,可以将照片发送到服务器并从Android保存。图像以base64编码。当它被解码时,它显示为全黑。

有人知道发生了什么事吗?下面是我使用的PHP代码。

$newFileName = uniqid();
$newFileName = $newFileName.$fileType;                  
$file = file_put_contents($path.'../../uploads/'.$user.'/'.$newFileName.'', base64_decode($file));

我猜你是从canvas上传数据的?您需要首先更正PNG数据,并将其保存为PNG文件

$img_data = str_replace('data:image/png;base64,', '', $img_data);
$img_data = str_replace(' ', '+', $img_data);
$decoded_image = base64_decode($img_data);
$PNGfile = $_SERVER['DOCUMENT_ROOT']."/".$upload_path."/temp.png";
file_put_contents( $PNGfile ,$decoded_image);
png2jpg($PNGfile, $large_image_location, 80);

如果你想把它转换成JPEG,这个函数会完成这项工作,然后删除PNG:

function png2jpg($originalFile, $outputFile, $quality) {
    $image = imagecreatefrompng($originalFile);
    imagejpeg($image, $outputFile, $quality);
   // imagedestroy($image);
}

提示:如果您要上传画布数据,请将数据放入