canvasã«æããçµµï¼ç»åï¼ããµã¼ãã«ä¿å
canvasã«æããããã¼ã¿ãåå¾ããã«ã¯ãcanvas.toDataURL() ã使ãã¾ãã
toDataURL()ã§å¾ãããã®ã¯ãbase64ã¨ã³ã³ã¼ããããç»åæ
å ±ãªã®ã§ã
ããããµã¼ãã«éã£ã¦ãbase64ãã³ã¼ããã¦ä¿åã¨ããæµãã§ãã
toDataURL()ãã¦ããã®ãã¼ã¿ãPOSTããé¨åã®javascript (ajaxé¨åãé¢åãªã®ã§ãprototype.jsã使ç¨)
function saveImage() { var imgdata = $('canvas_id').toDataURL(); // ããã©ã«ãã ã¨png, å¼æ°ã§jpegã¨ããå¯è½ imgdata = imgdata.replace('data:image/png;base64,', ''); // é ã®ãããªãé¨åãè½ã¨ã new Ajax.Request(<ç»åä¿åCGIã®URL>, { parameters: 'img=' + imgdata, // ç»åãã¼ã¿ãéä¿¡ onComplete: function(res) { // callback å¥ã«ãªãã¦ããããã if (res.responseText != 'ok') alert('error'); } }); }
ãµã¼ãå´ï¼ç»åä¿åCGIï¼
#!/usr/bin/perl use strict; use warnings; use CGI; use MIME::Base64; my $q = CGI->new; my $img = $q->param('img'); # base64ã¨ã³ã³ã¼ããããç»åãã¼ã¿ãåå¾ my $res_msg = 'error'; if ($img) { open(my $fh, '>', 'image.png') or die $!; # ä¿åãããã¡ã¤ã«åã§open print $fh MIME::Base64::decode($img); # base64ãã³ã¼ã close($fh); $res_msg = 'ok'; } print "Content-type:text/plain\n\n"; print $res_msg; exit;
ããã§ãimage.png ã«canvasã®ç»åãä¿åããã¾ãã
origin-clean flag
canvasã«ã¯ã»ãã¥ãªãã£ã®é¢ãããorigin-cleanãã©ã°ã¨ãããã®ãåå¨ãã¦ãããããã§ãã
ãã®ãã©ã°ã false ã«ãªã£ã¦ããå ´åãtoDataURL()ã¯ãsecurity errorãã¨ãªã£ã¦ã失æãã¾ãã
ããã«é¢ããåæã¯ã以ä¸ã§ãã
http://www.whatwg.org/specs/web-apps/current-work/multipage/the-canvas-element.html#security-with-canvas-elements
canvasã¯ãæåã¯origin-cleanãã©ã°ãtrueã«ã»ããããã¦ãã¾ããã
ããã¤ãç¹å®ã®ã¢ã¯ã·ã§ã³ãããã¨origin-cleanãã©ã°ãfalseã«ãªãã¾ãã
ä¾ãã°ã以前の記事 ã§ãã£ãç»åã®èªã¿è¾¼ã¿(drawImage)ã¨ãã§ã
ãã®ç»åã canvasã表示ãã¦ãããã¼ã¸ã¨åããã¡ã¤ã³ã®ç»åãªã toDataURL()ã§ãã¾ããã
å¥ãã¡ã¤ã³ã®ç»åãcanvasã«æç»ãã¦ãã¾ãã¨ãorigin-cleanãã©ã°ãfalseã«ãªã£ã¦ãtoDataURL()ã§ããªããªãã¾ãã
ãªã®ã§ãå¤é¨ã®ç»åãèªã¿è¾¼ã¾ãã¦ããçµµããããã¦ããµã¼ãã§ä¿åã¨ããããå ´åã¯ã
ä¸åº¦ãµã¼ãå´ã§ç»åããã¦ã³ãã¼ããã¦ããããcanvasã«æç»ããå¿
è¦ãããããã§ãã