Advertisement
Guest User

Untitled

a guest
Jun 20th, 2019
118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.96 KB | None | 0 0
  1. onImageChange($event){
  2. const files = $event.target.files || $event.dataTransfer.files;
  3. if (files[0] !== undefined) {
  4. // check for 5M limit
  5. let maxFileSize = 5242880;
  6. let maxImageSize = 150;
  7. if (files[0].size > 5242880) {
  8. // error
  9. Event.$emit('error_message', this.trans('Max size of image file is 5M') );
  10. } else {
  11. var self = this;
  12.  
  13. this.profileImage = files[0].name;
  14.  
  15. const fileReader = new FileReader ();
  16. fileReader.addEventListener('load', () => {
  17. // this.profileImageUrl = fileReader.result;
  18. var image = new Image();
  19. image.onload = function(){
  20. var canvas = document.createElement("canvas");
  21. EXIF.getData(image, function() {
  22. this.orientation = this.exifdata.Orientation;
  23. })
  24. var ctx = canvas.getContext("2d");
  25.  
  26. if(image.height > maxImageSize) {
  27. image.width *= maxImageSize / image.height;
  28. image.height = maxImageSize;
  29. }
  30.  
  31. if (4 < this.orientation && this.orientation < 9) {
  32. canvas.width = image.height;
  33. canvas.height = image.width;
  34. } else {
  35. canvas.width = image.width;
  36. canvas.height = image.height;
  37. }
  38.  
  39. switch (this.orientation) {
  40. case 2: ctx.transform(-1, 0, 0, 1, image.width, 0); break;
  41. case 3: ctx.transform(-1, 0, 0, -1, image.width, image.height); break;
  42. case 4: ctx.transform(1, 0, 0, -1, 0, image.height); break;
  43. case 5: ctx.transform(0, 1, 1, 0, 0, 0); break;
  44. case 6: ctx.transform(0, 1, -1, 0, image.height, 0); break;
  45. case 7: ctx.transform(0, -1, -1, 0, image.height, image.width); break;
  46. case 8: ctx.transform(0, -1, 1, 0, 0, image.width); break;
  47. default: break;
  48. }
  49.  
  50. ctx.clearRect(0, 0, canvas.width, canvas.height);
  51.  
  52. // ctx.drawImage(image, 0, 0, canvas.width, canvas.height);
  53. ctx.drawImage(image, 0, 0, image.width, image.height);
  54.  
  55. self.person.volunteer.profile_photo = canvas.toDataURL("image/png");
  56. };
  57. image.src = fileReader.result;
  58. this.profileImageFile = files[0] // this is an image file that can be sent to server...
  59. });
  60. fileReader.readAsDataURL(files[0]);
  61. }
  62. }
  63. },
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement