PHP - get base64 img string decode and save as jpg (resulting empty image )

To decode a base64 encoded image string and save it as a JPG file in PHP, you can use the following code:


// Base64-encoded image data
$img_string =

// Decode the base64 string and save as a file
$img = base64_decode($img_string);
file_put_contents('image.jpg', $img);

// Check if the image was saved
if (file_exists('image.jpg')) {
  echo "Image saved successfully.";
} else {
  echo "Failed to save image.";

Where $img_string is the base64 encoded image string.

Watch a course Learn object oriented PHP

Please note that if the resulting image is empty, it's likely that the input string is not a properly formatted base64 encoded image. Also, check the permission of the folder where you are saving the image and make sure that the folder has the write permission.