Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save alvinchesaro/7d52703772c7f9a39708ad9de2b9879e to your computer and use it in GitHub Desktop.
Save alvinchesaro/7d52703772c7f9a39708ad9de2b9879e to your computer and use it in GitHub Desktop.
DropzoneJS and Croppie integration
<div class="dropzone" id="myDropzone"></div>
Dropzone.options.myDropzone = {
url: '/post',
transformFile: function(file, done) {
var myDropZone = this;
// Create the image editor overlay
var editor = document.createElement('div');
editor.style.position = 'fixed';
editor.style.left = 0;
editor.style.right = 0;
editor.style.top = 0;
editor.style.bottom = 0;
editor.style.zIndex = 9999;
editor.style.backgroundColor = '#000';
document.body.appendChild(editor);
// Create the confirm button
var confirm = document.createElement('button');
confirm.style.position = 'absolute';
confirm.style.left = '10px';
confirm.style.top = '10px';
confirm.style.zIndex = 9999;
confirm.textContent = 'Confirm';
confirm.addEventListener('click', function() {
// Get the output file data from Croppie
croppie.result({
type:'blob',
size: {
width: 256,
height: 256
}
}).then(function(blob) {
// Update the image thumbnail with the new image data
myDropZone.createThumbnail(
blob,
myDropZone.options.thumbnailWidth,
myDropZone.options.thumbnailHeight,
myDropZone.options.thumbnailMethod,
false,
function(dataURL) {
// Update the Dropzone file thumbnail
myDropZone.emit('thumbnail', file, dataURL);
// Return modified file to dropzone
done(blob);
}
);
});
// Remove the editor from view
editor.parentNode.removeChild(editor);
});
editor.appendChild(confirm);
// Create the croppie editor
var croppie = new Croppie(editor, {
enableResize: true
});
// Load the image to Croppie
croppie.bind({
url: URL.createObjectURL(file)
});
}
};
<script src="https://unpkg.com/dropzone"></script>
<script src="https://unpkg.com/croppie"></script>
html {
font-family: -apple-system,BlinkMacSystemFont,Segoe UI,Roboto,Helvetica,Arial,sans-serif;
font-size: 1em;
}
body {
padding: 2em;
}
.dropzone {
min-height: 0
}
<link href="https://unpkg.com/dropzone/dist/dropzone.css" rel="stylesheet" />
<link href="https://unpkg.com/croppie/croppie.css" rel="stylesheet" />
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment