Skip to content

Instantly share code, notes, and snippets.

@xjamundx
Created February 26, 2011 16:13
Show Gist options
  • Select an option

  • Save xjamundx/845345 to your computer and use it in GitHub Desktop.

Select an option

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
Copy Markdown

Great little snippet, thanks

@niekberenschot

Copy link
Copy Markdown

Thanks!

@dkrasov

dkrasov commented Jul 29, 2015

Copy link
Copy Markdown

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
Copy Markdown

thanks!, you solve my problem

@upardhi

upardhi commented Mar 23, 2016

Copy link
Copy Markdown

Great snippet thanks 👍

@pravin-navle

Copy link
Copy Markdown

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

@kinereth

kinereth commented Jan 8, 2019

Copy link
Copy Markdown

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