Skip to content

Instantly share code, notes, and snippets.

@xjamundx
Created February 26, 2011 16:13
Show Gist options
  • Star 17 You must be signed in to star a gist
  • Fork 9 You must be signed in to fork a gist
  • Save xjamundx/845345 to your computer and use it in GitHub Desktop.
Save xjamundx/845345 to your computer and use it in GitHub Desktop.
php canvas base64 png decoder
<?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.';
?>
@Willshaw
Copy link

Great little snippet, thanks

@niekberenschot
Copy link

Thanks!

@dkrasov
Copy link

dkrasov commented Jul 29, 2015

I have just a black rectangle (file has been created, it's always 1.5 KB size). What I'm doing wrong?

@arifsh4rk
Copy link

thanks!, you solve my problem

@upardhi
Copy link

upardhi commented Mar 23, 2016

Great snippet thanks 👍

@pravin-navle
Copy link

@dkrasov make to run the command sudo chmod 777 "your_upload_directory"

@kinereth
Copy link

kinereth commented Jan 8, 2019

Great snippet really helpfull to me Thank you

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment