Base64decoder - PHP

Asked

Viewed 41 times

0

I have this code below, where decodes a picture string and displays in the browser, but I can’t save the decoded image with a random name and save to disk.

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

?>

I have tried using Fopen function without success. Any hints?

Grateful.

1 answer

0


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

if your file has a date:image/png;Base64, remember a

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

If you can’t 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; 

}
?>
  • Marcos, I’ll just have the string to be converted to image, I still wouldn’t have the path/file.png. In this case, would I have to decode and save this image first? This is my difficulty, because I can convert and show the image on the screen, but I can’t.

  • you want to save the image to the server ?

  • Yes, after converting to image, wanted to save the image with a random name and write to disk.

  • right just cahamar this function that I passed you and where you put the file name put uniqid().". png"

  • this will generate an image with Randonico id

  • I changed the answer copies the function the way it is

  • Marcos, thank you for your help/attention. It is returning error in this list fwrite( $ifp, base64_decode( $data ); syntax error, Unexpected ';' . I’ve already checked everything and apparently the syntax is correct.

  • is missing a ) before ; change to fwrite( $ifp, base64_decode( $date ));

  • 1

    Good worked out. Thank you.

Show 4 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.