简体   繁体   中英

Php base64 string to image

I get base64 string from my android appliation using which the php script saves it to the directory everything works fine for png. except i need the script to work for all image types At android side i have httpservlet extended class and using which i send a get request with a variable containing base64 string. So all we have is a base64 string nothing else at server side Can you help me with that?? This is my php code

<?php

 $imgstr = $_REQUEST['string'];


      // Decode the data
$data = base64_decode($imgstr);

$im = imagecreatefromstring($data);
if ($im !== false) {
header('Content-Type: image/png');
imagepng($im);

$success = file_put_contents("img/abc". uniqid().".png", $data);
imagedestroy($im);
if ($success) {
    echo 'yes yes yes';
}
} else {
echo 'An error occurred.';

} ?>

Ok so what i did was that i just calculated the mime type before encoding to base64 and sent that as a variable too

This is how i did it

 public static String getMimeType(String url) {
    String type = null;
    String extension = MimeTypeMap.getFileExtensionFromUrl(url);
    if (extension != null) {
        MimeTypeMap mime = MimeTypeMap.getSingleton();
        type = mime.getMimeTypeFromExtension(extension);
    }
    return type;
}   

   mimetype = getMimeType(picturePath);
   String temp[] = mimetype.split("/");
   as.add(new BasicNameValuePair("mime", temp[1]));

After this as was sent to server along with image.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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