Skip to content

Instantly share code, notes, and snippets.

@eosvn
Forked from fazlurr/canvas-upload.php
Created May 22, 2024 14:49
Show Gist options
  • Save eosvn/2b244f6f0d7e95c3f4a351b1f2f16b5e to your computer and use it in GitHub Desktop.
Save eosvn/2b244f6f0d7e95c3f4a351b1f2f16b5e to your computer and use it in GitHub Desktop.
Function to save base64 image to png with PHP
<?php
// 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.';
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment