[an error occurred while processing this directive]
[an error occurred while processing this directive]
<script>
img = new Image();
img.src="orig.png";
img.onload = function(){
var cvs = document.createElement("canvas");
cvs.width = img.naturalWidth;
cvs.height = img.naturalHeight;
var ctx = cvs.getContext("2d");
ctx.drawImage(img,0, 0);
var b64img = cvs.toDataURL();
// ここで b64img でなんかする
};
<script>
まず image 要素を作成します。そこで画像をロードさせます。
画像のロードが終わったら、onload に代入している関数に処理が続きます。
...
var b64img = cvs.toDataURL();
$.ajax({
type: 'POST',
url: './post.cgi',
data: {
'data': b64img,
},
dataType: 'text',
success: function(data) {
console.debug("image sent");
},
fail: function (d){
console.debug(d);
}
});
これで、posg.cgi に対して、"data" という名前で base64 エンコードされたデータが送信されました。
#!/usr/bin/env python
import cgi,base64
imgname = "hoge.png"
f = cgi.FieldStorage()
txt = f.getfirst('data', '')
A=b64=txt.split(",")
if len(A) == 2:
b64=txt.split(",")[1]
b = base64.b64decode(b64)
fd=open(imgname, "w")
fd.write(b)
fd.close()
print """Content-type: text/html
Pragma: no-cache
<html>
<body>
<img src="%s" />
</body>
</html>
"""%(imgname)