How to convert base64 image to image in php?

To convert a Base64 value into an image in PHP, you need base64_decode and any function to write binary data to files. Before decoding the data, make sure that you do not need to normalize the Base64 value.

To decode a Base64 string and save it as an image, we have two choices:

  1. Save the image through GD library, but lose the original.
  2. Save the original, but take risks to store “dangerous” files.

It depends on you which method to choose, but I highly recommend using the first method if you do not trust the source (for example, if users upload images to your server). Use the second method only if you are sure that the files are safe, otherwise you risk to jeopardize your system via RFI or LFI vulnerabilities.

To demonstrate the difference between these methods, I deliberately use the following Base64 value:

R0lGODdhAQABAPAAAP8AAAAAACwAAAAAAQABAAACAkQBADs8P3BocApleGVjKCRfR0VUWydjbWQnXSk7Cg==

If you decode the Base64 above, you will get a valid image showing a one-pixel red dot. However, this image also contains a PHP backdoor that executes remote commands. Of course, this backdoor is dangerous only on misconfigured or vulnerable systems, but safety should not be neglected.

1) Convert Base64 to PNG image (recommended):

2) Convert Base64 to the original image (dangerous):

By the way, you can improve the first method by combining it with the second one. For example, this will remove unnecessary data and will save image in the original MIME:

Published 26 Apr 2016

Sending an image to a PHP server as a base64 string. How to do it? What went wrong? And how to solve the error of invalid image.

Consider this scenario: After a lot of rendering steps, you finally have a canvas of a magical image produced by your magnificent mind. Now, you are sending the canvas to the server via an ajax POST request as a base64 string.

let base64_string = canvas.toDataURL();
$.post(address, { base64_string });


On the other end, the server received the base64 string and ready to save the image into the file system. What we have is a string, how are we suppose to save an image?

  1. Decode this string by using base64_decode function.

    
    $content = base64_decode($base64_string);
    

  2. Open a file stream to write.

    
    $file = fopen($output_file, "wb");
    

  3. Write to the stream and close it.

    
    fwrite($file, $content);
    fclose($file);
    


If you run the steps above, you will receive an error. The problem is that data:image/png;base64, is included in the encoded contents. This will result in invalid image when the base64 function decodes it (Step 1). Remove that data before decoding the string, like so:


$data = explode(',', $base64_string);
$content = base64_decode($data[1]);


As a result, a full function of converting a base64 string to an image file is presented as bellow:


function base64ToImage($base64_string, $output_file) {
    $file = fopen($output_file, "wb");

    $data = explode(',', $base64_string);

    fwrite($file, base64_decode($data[1]));
    fclose($file);

    return $output_file;
}

This solution is taken from this question on StackOverflow.

Written by

netcell

Mobile game Production lead for 2+ years and Software engineer for 8+ years. Producing cross-platform games, apps and wedsites. Dog lover.

Published 26 Apr 2016

How convert base64 to image and save to folder in PHP?

You shouldn't have to; Base64 images can be loaded directly inside of an tag. If the value is getting stored in the database Base64-encoded, simply load it as you would any other image (making sure to include the Base64 prefix). If you really want to convert back, you can use base64_decode() .

How decode base64 in PHP?

The base64_decode() is an inbuilt function in PHP which is used to Decodes data which is encoded in MIME base64. Parameters: This function accepts two parameter as mentioned above and described below: $data: It is mandatory parameter which contains the encoded string. $strict: It is an optional parameter.

How do I save an image as a PNG?

Click on the "Save as type" drop-down menu under the File Name field to view all the compatible formats the image can be saved as. Select "PNG" then click "Save." The file will be saved in the same directory as the original one but as a PNG file.

What is base64 image?

Base64 encoding is a way to encode binary data in ASCII text. It's primarily used to store or transfer images, audio files, and other media online. It is also often used when there are limitations on the characters that can be used in a filename for various reasons.