简体   繁体   English

从base64字符串解码图像

[英]Decode image from base64 string

I am sending an image to php server in the from of base64 string. 我正在从base64字符串中将图像发送到php服务器。 When retrieving it from server side it shows size of the image,but it dont display the image I have posted to the service. 从服务器端检索它时,它会显示图像的大小,但不会显示我发布到服务中的图像。 Am using post method to send the string. 我正在使用post方法发送字符串。

My android code://to encode the image. 我的android code://编码图像。

Bitmap bm=BitmapFactory.decodeFile(picturePath);
ByteArrayOutputStream baos = new ByteArrayOutputStream();  
bm.compress(Bitmap.CompressFormat.JPEG, 100, baos); //bm is the bitmap object   
byte[] b = baos.toByteArray(); 
String encodedImage = Base64.encodeToString(b, Base64.DEFAULT);

Php code://to decode the image PHP代码://解码图像

$string=$_POST ['i_image'];
$filename="newimage.jpg";
$imagepath=base64_to_jpeg($string,$filename);

function base64_to_jpeg($base64_string, $output_file) {
    $ifp = fopen($output_file, "wb"); 

    $data = explode(',', $base64_string);

    fwrite($ifp, base64_decode($data[1])); 
    fclose($ifp); 

    return $output_file; 
}

Can anyone help me to retrieve the original image? 谁能帮我找回原图?

Thanks in advance. 提前致谢。

if you want to decode your image in PHP i think this will help you 如果您想用PHP解码图像,我认为这将对您有所帮助

$uploadDir = 'path/to/download/dir/';
$binary = base64_decode($data_encoded_base64);
header('Content-Type: bitmap; charset=utf-8');
$fileName=md5(uniqid()).'.png';
$file = fopen($uploadDir . $fileName, 'wb');
fwrite($file, $binary);
fclose($file);

hope it helps, good luck!. 希望对您有帮助,祝您好运!

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

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