Tero

[+] Save Base64 Image to PNG with PHP

Dec 14th, 2019
494
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 0.47 KB | None | 0 0
  1. <?php
  2.     // requires php5
  3.     // https://gist.githubusercontent.com/fazlurr/9802071/raw/894b282b43050cdcee77f7a73413d52390209f51/canvas-upload.php
  4.  
  5.     define('UPLOAD_DIR', 'images/');
  6.     $img = $_POST['img'];
  7.     $img = str_replace('data:image/png;base64,', '', $img);
  8.     $img = str_replace(' ', '+', $img);
  9.     $data = base64_decode($img);
  10.     $file = UPLOAD_DIR . uniqid() . '.png';
  11.     $success = file_put_contents($file, $data);
  12.     print $success ? $file : 'Unable to save the file.';
Advertisement
Add Comment
Please, Sign In to add comment