PHP base64 decode image not working 2022
PHP

PHP base64 decode image not working 2022 : Solution Code

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