I'm working on an app that send a photo to a server and saves from Android. The image is encoded in base64. When it is decoded it shows up all black.
Anyone know what's going on? Here is the PHP code I'm using.
$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
$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:
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>.