解码图像从base64字符串


Decode image from base64 string

我正在发送一个图像到php服务器从base64字符串。当检索它从服务器端它显示图像的大小,但它不显示我已经发布到服务的图像。我使用post方法发送字符串。

My android code://to encode the image.

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代码://解码图像

$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; 
}

谁能帮我检索原始图像?

如果你想在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);

希望它有帮助,祝你好运!。