Tero icon

[+] Save Base64 Image to PNG with PHP

Tero | PRO | 12/14/19 07:59:10 AM UTC | 0 ⭐ | 443 👁️ | Never ⏰ | []
PHP |

470 B

|

None

|

0 👍

/

0 👎

<?php
    // requires php5
    // https://gist.githubusercontent.com/fazlurr/9802071/raw/894b282b43050cdcee77f7a73413d52390209f51/canvas-upload.php
 
    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.';

Comments