Coder Perfect

Is it possible to convert a Base64 string to an image file? [duplicate]

Problem

I’m attempting to create an image file from a base64 image string. My Base64 string is as follows:

http://pastebin.com/ENkTrGNG

To convert it to an image file, use the following code:

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’m receiving an incorrect picture error; what’s going on?

Asked by Badal

Solution #1

The issue is that the encoded contents include data:image/png;base64. When the base64 function decodes the image, it will produce erroneous data. Before decoding the string, remove that data from the function.

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

Answered by Austin Brunkhorst

Solution #2

Here’s a simple method I’m employing:

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

Because file get contents can read data from any URI, including a data:/ URI, this works fine.

Answered by Henry Trần

Solution #3

At the beginning of the picture data, remove the part that reads data:image/png;base64. After that, the actual base64 data is displayed.

You’ll be alright if you trim everything up to and including base64 (before using base64 decode() on the data).

Answered by aaaaaa123456789

Solution #4

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

Answered by Shimon Doodkin

Solution #5

Although it is an old thread, if you wish to upload an image with the same extension-

    $image = $request->image;
    $imageInfo = explode(";base64,", $image);
    $imgExt = str_replace('data:image/', '', $imageInfo[0]);      
    $image = str_replace(' ', '+', $imageInfo[1]);
    $imageName = "post-".time().".".$imgExt;
    Storage::disk('public_feeds')->put($imageName, base64_decode($image));

You can create ‘public_feeds’ in laravel’s filesystem.php-

   'public_feeds' => [
        'driver' => 'local',
        'root'   => public_path() . '/uploads/feeds',
    ],

Answered by shubham sachan

Post is based on https://stackoverflow.com/questions/15153776/convert-base64-string-to-an-image-file