PHP保存Base64图片 Convert Base64 string to an image file

The problem is that data:image/png;base64, is included in the encoded contents. This will result in invalid image data when the base64 function decodes it. Remove that data in the function before decoding the string, like so.

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

 

转自:http://stackoverflow.com/a/15153931/4484798

本文:PHP保存Base64图片 Convert Base64 string to an image file

Leave a Reply