Created
April 20, 2015 05:20
-
-
Save baku89/a3a1251b45e72597754f to your computer and use it in GitHub Desktop.
Capturing canvas as png sequence
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# canvas will be exported to "./export/test/test000089.png" | |
canvas = document.cleateElement('canvas') | |
# draw something on canvas.. | |
# post | |
$.ajax | |
type: "POST" | |
url: './save.php' | |
data: | |
name: 'test' | |
directory: './export' | |
number: 89 | |
image: canvas.toDataURL('image/png') | |
success: (res) -> | |
json = $.parseJSON(json) | |
# ... | |
error: (XMLHttpRequest, textStatus, errorThrown) -> | |
# ... |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<? | |
try { | |
// decode base64 & create image | |
$canvas = $_POST["image"]; | |
$canvas = preg_replace("/data:image\/png;base64,/i","",$canvas); | |
$buffer = base64_decode($canvas); | |
$image = imagecreatefromstring($buffer); | |
// get path | |
$filename = $_POST["name"] ."_". str_pad($_POST["number"], 6, "0", STR_PAD_LEFT) . ".png"; | |
$dir = $_POST["directory"] . "/" . $_POST["name"]; | |
// make export directory | |
if (!file_exists($dir)) { | |
mkdir($dir, 0777, true); | |
} | |
// save | |
imagepng($image, $dir . "/" . $filename); | |
imagedestroy( $image ); | |
} catch (Exception $e) { | |
?>{"status": "failed", "message": <?= $e->getMessage() ?>}<? | |
exit(); | |
} | |
?> | |
{"status": "success"} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment