Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // convert pasted images to jpeg before posting
- document.body.addEventListener('paste', (e) => {
- if (e.target.closest('[name=post]')) {
- for (let i = 0; i < e.clipboardData.files.length; i++) {
- const file = e.clipboardData.files[i];
- if (file.type.startsWith('image')) {
- // create the canvas
- const canvas = document.createElement('canvas');
- const context = canvas.getContext('2d')
- // prepare the image in order to draw it on the canvas
- createImageBitmap(file).then((bmp) => {
- canvas.width = bmp.width;
- canvas.height = bmp.height;
- context.drawImage(bmp, 0, 0);
- // add the new canvas to what we're sending to the server
- canvas.toBlob((blob) => {
- // this doesn't play nicely with the multiple image upload js -- it'll overwrite the first image every time. ideally the addFile function it uses would be available, but that's ok
- $(document).on('ajax_before_post', (e, formData) => formData.append('file', blob, 'blob.jpg'))
- }, 'image/jpeg')
- })
- return false;
- }
- }
- }
- });
Advertisement
Add Comment
Please, Sign In to add comment