简体   繁体   English

base64解码php android

[英]base64 decoding php android

I'm working on an app that send a photo to a server and saves from Android. 我正在开发一个将照片发送到服务器并从Android保存的应用程序。 The image is encoded in base64. 该图像以base64编码。 When it is decoded it shows up all black. 解码后会显示全黑。

Anyone know what's going on? 有人知道发生了什么吗? Here is the PHP code I'm using. 这是我正在使用的PHP代码。

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

I guess you're uploading data from canvas? 我猜您是从画布上传数据? You need to correct the PNG data first and save it as a PNG file 您需要先更正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);

If you want to convert it to a JPEG, this function does the job and then delets the PNG: 如果要将其转换为JPEG,此函数将执行此工作,然后删除PNG:

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

Tip: if you're uploading canvas data, put the data into a <TEXTAREA> before posting it - I used an <INPUT> and it truncated the image data until I changed to <TEXTAREA>. 提示:如果要上传画布数据,则在发布数据之前将其放入<TEXTAREA>中-我使用了<INPUT>,它会截断图像数据,直到更改为<TEXTAREA>。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM