将Base64字符串转换为图像文件?

时间:2022-11-13 12:21:57

I am trying to convert my base64 image string to an image file. This is my Base64 string:

我正在尝试将我的base64图像字符串转换为一个图像文件。这是我的Base64字符串:

http://pastebin.com/ENkTrGNG

http://pastebin.com/ENkTrGNG

Using following code to convert it into an image file:

使用以下代码将其转换为图像文件:

function base64_to_jpeg( $base64_string, $output_file ) {
    $ifp = fopen( $output_file, "wb" ); 
    fwrite( $ifp, base64_decode( $base64_string) ); 
    fclose( $ifp ); 
    return( $output_file ); 
}

$image = base64_to_jpeg( $my_base64_string, 'tmp.jpg' );

But I am getting an error of invalid image, whats wrong here?

但是我得到一个无效图像的错误,这里有什么问题吗?

5 个解决方案

#1


202  

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.

问题是数据:图像/png;base64,包含在编码的内容中。当base64函数对其进行解码时,这将导致图像数据无效。在解码字符串之前,删除函数中的数据。

function base64_to_jpeg($base64_string, $output_file) {
    // open the output file for writing
    $ifp = fopen( $output_file, 'wb' ); 

    // split the string on commas
    // $data[ 0 ] == "data:image/png;base64"
    // $data[ 1 ] == <actual base64 string>
    $data = explode( ',', $base64_string );

    // we could add validation here with ensuring count( $data ) > 1
    fwrite( $ifp, base64_decode( $data[ 1 ] ) );

    // clean up the file resource
    fclose( $ifp ); 

    return $output_file; 
}

#2


38  

You need to remove the part that says data:image/png;base64, at the beginning of the image data. The actual base64 data comes after that.

您需要删除显示数据的部分:image/png;base64,在图像数据的开头。接下来是实际的base64数据。

Just strip everything up to and including base64, (before calling base64_decode() on the data) and you'll be fine.

只要将所有内容都去掉,包括base64(在数据上调用base64_decode()之前),就可以了。

#3


7  

maybe like this

也许像这样

function save_base64_image($base64_image_string, $output_file_without_extension, $path_with_end_slash="" ) {
    //usage:  if( substr( $img_src, 0, 5 ) === "data:" ) {  $filename=save_base64_image($base64_image_string, $output_file_without_extentnion, getcwd() . "/application/assets/pins/$user_id/"); }      
    //
    //data is like:    data:image/png;base64,asdfasdfasdf
    $splited = explode(',', substr( $base64_image_string , 5 ) , 2);
    $mime=$splited[0];
    $data=$splited[1];

    $mime_split_without_base64=explode(';', $mime,2);
    $mime_split=explode('/', $mime_split_without_base64[0],2);
    if(count($mime_split)==2)
    {
        $extension=$mime_split[1];
        if($extension=='jpeg')$extension='jpg';
        //if($extension=='javascript')$extension='js';
        //if($extension=='text')$extension='txt';
        $output_file_with_extension=$output_file_without_extension.'.'.$extension;
    }
    file_put_contents( $path_with_end_slash . $output_file_with_extension, base64_decode($data) );
    return $output_file_with_extension;
}

#4


1  

if($_SERVER['REQUEST_METHOD']=='POST'){
$image_no="5";//or Anything You Need
$image = $_POST['image'];
$path = "uploads/$image_no.png";

file_put_contents($path,base64_decode($image));
echo "Successfully Uploaded";
}

#5


0  

easy way i'm using:

简单的方法我使用:

file_put_contents($output_file, file_get_contents($base64_string));

#1


202  

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.

问题是数据:图像/png;base64,包含在编码的内容中。当base64函数对其进行解码时,这将导致图像数据无效。在解码字符串之前,删除函数中的数据。

function base64_to_jpeg($base64_string, $output_file) {
    // open the output file for writing
    $ifp = fopen( $output_file, 'wb' ); 

    // split the string on commas
    // $data[ 0 ] == "data:image/png;base64"
    // $data[ 1 ] == <actual base64 string>
    $data = explode( ',', $base64_string );

    // we could add validation here with ensuring count( $data ) > 1
    fwrite( $ifp, base64_decode( $data[ 1 ] ) );

    // clean up the file resource
    fclose( $ifp ); 

    return $output_file; 
}

#2


38  

You need to remove the part that says data:image/png;base64, at the beginning of the image data. The actual base64 data comes after that.

您需要删除显示数据的部分:image/png;base64,在图像数据的开头。接下来是实际的base64数据。

Just strip everything up to and including base64, (before calling base64_decode() on the data) and you'll be fine.

只要将所有内容都去掉,包括base64(在数据上调用base64_decode()之前),就可以了。

#3


7  

maybe like this

也许像这样

function save_base64_image($base64_image_string, $output_file_without_extension, $path_with_end_slash="" ) {
    //usage:  if( substr( $img_src, 0, 5 ) === "data:" ) {  $filename=save_base64_image($base64_image_string, $output_file_without_extentnion, getcwd() . "/application/assets/pins/$user_id/"); }      
    //
    //data is like:    data:image/png;base64,asdfasdfasdf
    $splited = explode(',', substr( $base64_image_string , 5 ) , 2);
    $mime=$splited[0];
    $data=$splited[1];

    $mime_split_without_base64=explode(';', $mime,2);
    $mime_split=explode('/', $mime_split_without_base64[0],2);
    if(count($mime_split)==2)
    {
        $extension=$mime_split[1];
        if($extension=='jpeg')$extension='jpg';
        //if($extension=='javascript')$extension='js';
        //if($extension=='text')$extension='txt';
        $output_file_with_extension=$output_file_without_extension.'.'.$extension;
    }
    file_put_contents( $path_with_end_slash . $output_file_with_extension, base64_decode($data) );
    return $output_file_with_extension;
}

#4


1  

if($_SERVER['REQUEST_METHOD']=='POST'){
$image_no="5";//or Anything You Need
$image = $_POST['image'];
$path = "uploads/$image_no.png";

file_put_contents($path,base64_decode($image));
echo "Successfully Uploaded";
}

#5


0  

easy way i'm using:

简单的方法我使用:

file_put_contents($output_file, file_get_contents($base64_string));