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:

<?php

// Base64-encoded image data
$img_string =
  'data:image/jpeg;base64,/9j/4AAQSkZJRgABAQAAAQABAAD/2wCEAAkGBxQTEhQUExMWFhUXGBcaGRgYGBcaGBgZGhgfGxoaHSggGB0lHRgaITEhJSkrLi4uGB8zODMtNygtLisBCgoKDg0OGxAQGy0lICYtLS0tLS0tLS0tKy0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLf/AABEIAKgBLAMBIgACEQEDEQH...';

// 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.