Advertisement
Guest User

Untitled

a guest
Mar 15th, 2019
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.38 KB | None | 0 0
  1. $('#file').on('change', function (e) {
  2. var file = e.currentTarget.files[0];
  3. var reader = new FileReader();
  4. reader.onload = function (e) {
  5. var image = $('<img/>');
  6. image.on('load', function () {
  7. var square = 320;
  8. var canvas = document.createElement('canvas');
  9.  
  10. canvas.width = square;
  11. canvas.height = square;
  12.  
  13. var context = canvas.getContext('2d');
  14. context.clearRect(0, 0, square, square);
  15. var imageWidth;
  16. var imageHeight;
  17. var offsetX = 0;
  18. var offsetY = 0;
  19.  
  20. if (this.width > this.height) {
  21. imageWidth = Math.round(square * this.width / this.height);
  22. imageHeight = square;
  23. offsetX = - Math.round((imageWidth - square) / 2);
  24. } else {
  25. imageHeight = Math.round(square * this.height / this.width);
  26. imageWidth = square;
  27. offsetY = - Math.round((imageHeight - square) / 2);
  28. }
  29.  
  30. context.drawImage(this, offsetX, offsetY, imageWidth, imageHeight);
  31. var data = canvas.toDataURL('image/jpeg');
  32.  
  33. var thumb = $('<img/>');
  34. thumb.attr('src', data);
  35. $('body').append(thumb);
  36. });
  37. image.attr('src', e.target.result);
  38. };
  39. reader.readAsDataURL(file);
  40. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement