How can i download base64 image in php?

I want to offer a PNG image to download only for some users, and the image should not be located anywhere, so the users should not see it's path.

That's why I inserted it to the download.php as a base64 image. The problem is, however the image is offered for download, but it shows 0 B size when downloaded.

    $base64strImg = 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAxoAAARjCAIAAABnqMWCAAAACXBIWXM...';
  header('Content-Description: File Transfer');
            header('Content-Type: application/octet-stream');
            header('Content-Disposition: attachment; filename=myimage.png');
            header('Content-Transfer-Encoding: binary');
            header('Expires: 0');
            header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
            header('Pragma: public');
            ob_clean();
            flush();
            readfile(base64_decode($base64strImg));
            exit;

Not sure where is the problem, if it can't handle big image or why it can't be downloaded.

I also tried this way:

 header('Content-Disposition: attachment;filename="test.png"');
            header('Content-Type: application/force-download');
            echo base64_decode($base64strImg);

Now the image has correct size, but still, can't be opened after download. The image viewer says unsupported format.

And third option - without decoding, it also has a correct size, but still can't be opened.

  header('Content-Description: File Transfer');
        header('Content-Type: image/png');
        header('Content-Disposition: attachment; filename=test.png');
        header('Content-Transfer-Encoding: binary');
        header('Expires: 0');
        header('Cache-Control: must-revalidate');
        header('Pragma: public');
        header('Content-Length: ' . strlen($base64strImg));
        ob_clean();
        flush();
        echo $base64strImg;
        exit;

This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters

// requires php5
define('UPLOAD_DIR', 'images/');
$img = $_POST['img'];
$img = str_replace('data:image/png;base64,', '', $img);
$img = str_replace(' ', '+', $img);
$data = base64_decode($img);
$file = UPLOAD_DIR . uniqid() . '.png';
$success = file_put_contents($file, $data);
print $success ? $file : 'Unable to save the file.';
?>

  • PHP – Save base64 Encoded string – Convert base64 to Image
    • Step 1 Base64 POST Image
    • Step 2 Base64 images String Replace
    • Step 3 base64_decode function
    • Step 4 file_put_contents
    • Step 5 Save base64 images
    • Related posts

In this Post We Will Explain About is PHP – Save base64 Encoded string – Convert base64 to Image With Example and Demo.Welcome on Pakainfo.com – Examples, The best For Learn web development Tutorials,Demo with Example! Hi Dear Friends here u can know to Convert base64 to image file in PHP Example

In this post we will show you Best way to implement Save Base64 Encoded image to file using PHP, hear for Save a PNG image from a base64 data string with PHPwith Download .we will give you demo,Source Code and examples for implement Step By Step Good Luck!.

When we are handling any PHP API for web-apps, they mostly all the images path provides the image in Base64 encoded source code, so we need to simple we server store or save base64 encoded image to images folder to server as a image or file.

Step 1 : Base64 POST Image

If we are getting the simple image source_data with image type following latest code as below:

$source_data = $_POST['image'];
$source_data = 'data:image/png;base64,AFFAFFFEEe8LKS346fj42Pj4...';

Step 2 : Base64 images String Replace

We need to simple parse base64 function part from this image source_data following latest code below:

$source_data = str_replace('source_data:image/png;base64,', '', $source_data);
$source_data = str_replace(' ', '+', $source_data);

Step 3 : base64_decode function

And then, decode the image source_data using base64_decode function following latest code below:

$source_data = base64_decode($source_data);

Step 4 : file_put_contents

And then, we can put this image source_data to your desired file using file_put_contents function like below:

$data_file = 'admin/images/'. uniqid() . '.png';
$file_success = file_put_contents($data_file, $source_data);

Step 5 : Save base64 images

If we want to simple image rotate the image and save base64 images

$source_data = base64_decode($source_data); //simple base64 any format decoded image source_data
$source_img = imagecreatefromstring($source_data);
$rotated_img = imagerotate($source_img, 90, 0); //simple images rotate with angle 90 degree here
$data_file = 'admin/images/'. uniqid() . '.png';
$data_savefile = imagejpeg($rotated_img, $data_file, 10);
imagedestroy($source_img);

Example

I hope you have Got What is Save a Base64 Encoded Canvas image to a png file using PHP And how it works.I would Like to have FeadBack From My Blog(Pakainfo.com) readers.Your Valuable FeadBack,Any Question,or any Comments abaout This Article(Pakainfo.com) Are Most Always Welcome.

I am Jaydeep Gondaliya , a software engineer, the founder and the person running Pakainfo. I’m a full-stack developer, entrepreneur and owner of Pakainfo.com. I live in India and I love to write tutorials and tips that can help to other artisan, a Passionate Blogger, who love to share the informative content on PHP, JavaScript, jQuery, Laravel, CodeIgniter, VueJS, AngularJS and Bootstrap from the early stage.

How to convert base64 string into image in php?

Using following code to convert it into an image file: function base64_to_jpeg( $base64_string, $output_file ) { $ifp = fopen( $output_file, "wb" ); fwrite( $ifp, base64_decode( $base64_string) ); fclose( $ifp ); return( $output_file ); } $image = base64_to_jpeg( $my_base64_string, 'tmp. jpg' );

How do I save a base64 file?

How to convert Base64 to file. Paste your string in the “Base64” field. Press the “Decode Base64 to File” button. Click on the filename link to download the file.

How do I save an image in base64 node?

To save a base64-encoded image to disk with Node. js, we can use the fs. writeFile method.

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.