You can use FileReader + Canvas to read a local file and then trim it without sending it to the server.
Here is a demo showing how to do this.
<form><input type="file" id=f></form> <canvas id=c width="600" height="600"></canvas> <script> var f = document.getElementById('f'); var canvas = document.getElementById('c'); var context = canvas.getContext('2d'); f.onchange = function() { var file = this.files[0]; var reader = new FileReader(); reader.onload = function(evt) { var img = new Image(); img.onload = function() { context.drawImage(this, 0, 0, 100, 100, 0, 0, 100, 100); var pngUrl = canvas.toDataURL(); </script>
What else you need to do is use the jquery jcrop plugin so that the user can select the cropping area, because in this demo I just hardcoded the crop of the top left 100x100 pixels.
It looks like you will want to use jcrops onSelect to get the beginning + width + height of the cropping area and feed these values ββinto context.drawImage
hope you can handle the rest, good luck
source share