Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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

This comment has been minimized.

Copy link

@Willshaw Willshaw commented Feb 10, 2014

Great little snippet, thanks

@niekberenschot

This comment has been minimized.

Copy link

@niekberenschot niekberenschot commented Jul 15, 2014

Thanks!

@dkrasov

This comment has been minimized.

Copy link

@dkrasov 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

This comment has been minimized.

Copy link

@arifsh4rk arifsh4rk commented Aug 31, 2015

thanks!, you solve my problem

@upardhi

This comment has been minimized.

Copy link

@upardhi upardhi commented Mar 23, 2016

Great snippet thanks 👍

@pravin-navle

This comment has been minimized.

Copy link

@pravin-navle pravin-navle commented Jun 20, 2018

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

@kinereth

This comment has been minimized.

Copy link

@kinereth 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
You can’t perform that action at this time.