Base64decoder - PHP

0

I have this code below, where it decodes a string into an image and displays it in the browser, but I can not save the decoded image with a random name and save it to disk.

$string  = 'iVBORw0KGgoAAAANSUhEUgAAAIAAAACACAYAAADDPmHLAAAgAElEQVR4nNR9d1gU1/f3mdnOLlvobelIEVEEsWPDgokaC7bYYos ........'
$decoded = base64_decode($string);
header("Content-Type: image/png");
echo $decoded;

?>

I have tried to use the Fopen function without success. Any tips?

Thank you.

    
asked by anonymous 22.02.2018 / 15:43

1 answer

0

Good try this function, where $ output_file is the name of the path / file.png

If your file has a date: image / png; base64, remember to do one

explode(",",$data) e usar o  $data[1]

If you can not use the function the way it is here

<?php
    function base64_to_png($base64_strin) {
    $output_file = uniqid().".png";
    $ifp = fopen( $output_file, 'wb' ); 
    $data = $base64_string;
    fwrite( $ifp, base64_decode( $data ));
    fclose( $ifp ); 
    return $output_file; 

}
?>
    
22.02.2018 / 15:53