Skip to content

Instantly share code, notes, and snippets.

@s-hiroshi
Created November 25, 2012 09:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save s-hiroshi/4142915 to your computer and use it in GitHub Desktop.
Save s-hiroshi/4142915 to your computer and use it in GitHub Desktop.
JavaScript > snippets > Canvasデーターの保存
jQuery(function($) {
// body部パラメーター
var data = {};
// Canvasのデータをbase64でエンコードした文字列を取得
var canvasData = $('canvas').get(0).toDataURL();
// 不要な情報を取り除く
canvasData = canvasData.replace(/^data:image\/png;base64,/, '');
data.image = canvasData;
$.ajax({
url: 'example.php',
type: 'POST',
success: function() {
// 成功時の処理
},
error(jqXHR, textStatus, errorThrown) {
// 失敗時の処理
},
data: data,
dataType: 'json'
});
});
<?php
$imageData = $_POST['image'];
$filename = 'example.png';
$fp = fopen(, 'w');
fwrite($fp,base64_decode($imageData));
fclose($fp);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment