get image from base64 string

As was noted the “+” should not be missed, the rest is straight forward. Use $_REQUEST if you are not sure is it post or get.

// requires php5
define('UPLOAD_DIR', 'images/');
$img = $_REQUEST['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.';

Leave a Comment