9.4 C
Munich
Friday, March 24, 2023

PHP base64 decode image not working 2022 : Solution Code

Must read

PHP base64 decode image not working 2022 : In this article, let’s checkout with the solution for the image which is not working due to base64 decoding.

Important Note : Please check with image path correctly.

Solution for PHP base64 decode image not working 2022

Solution php code image to base64

$imagepathvar = 'folderpath/imagefile.png';
$type = pathinfo($path, PATHINFO_EXTENSION);
$data = file_get_contents($imagepathvar);
$base64 = 'data:image/' . $type . ';base64,' . base64_encode($data);

Method or function for conversion base64 to jpeg

function base64_to_jpeg($base64_stringvar, $output_filevar) {
    // first step is open the output file for writing
    $iflp = fopen($output_filevar, 'wb'); 

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

    // now next step os to add validation here with ensuring count( $data ) > 1
    fwrite($iflp, base64_decode($data[ 1 ]));

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

    return $output_file; 
}

Other Related PHP Solution

- Advertisement -spot_img

More articles

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisement -spot_img

Latest article