Skip to content

Instantly share code, notes, and snippets.

@linux-china
Created December 7, 2012 04:34
Show Gist options
  • Save linux-china/4230788 to your computer and use it in GitHub Desktop.
Save linux-china/4230788 to your computer and use it in GitHub Desktop.
Past image from clipboard and upload to your server
<script type="text/javascript">
requirejs([],
function () {
function fillImageContent(imgTag) {
var canvas = document.createElement('canvas');
canvas.width = imgTag.width;
canvas.height = imgTag.height;
var ctx = canvas.getContext('2d');
ctx.drawImage(imgTag, 0, 0);
ctx.fill();
// Convert that back to a dataURL
var dataURL = canvas.toDataURL('image/png');
// Base64 image only.
document.getElementById("clipboard_attachmentContentBase64").value = dataURL.replace(/data:image\/png;base64,/, '');
}
document.body.onpaste = function (e) {
var items = e.clipboardData.items;
for (var i = 0; i < items.length; ++i) {
if (items[i].kind == 'file' && items[i].type.indexOf('image/') !== -1) {
var blob = items[i].getAsFile();
window.URL = window.URL || window.webkitURL;
var blobUrl = window.URL.createObjectURL(blob);
var img = document.createElement('img');
img.src = blobUrl;
var logBox = document.getElementById('imgBox');
logBox.appendChild(img);
img.onload = function () {
fillImageContent(img);
}
}
}
}
});
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment