Guest User

Untitled

a guest
Jan 16th, 2018
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.63 KB | None | 0 0
  1. 'detectFiles(event) {
  2. this.getOrientation(event.target.files[0], function (orientation) {
  3. this.imageOrientation = orientation;
  4. }.bind(this));
  5. console.log('the original image data', event.target.files);
  6. this.selectedFiles = event.target.files;
  7.  
  8. // SAS : Converting the selected image to Base64 for rotation.
  9. const reader = new FileReader();
  10. reader.onloadend = (e) => {
  11. this.base64Data = reader.result;
  12. const rotatedData = this.rotateBase64Image(this.base64Data);
  13.  
  14. // SAS: Calling the data uri to blob
  15. const selFile = this.dataURItoBlob(rotatedData);
  16. this.uploadFiles(selFile);
  17. }
  18.  
  19. reader.readAsDataURL(event.target.files[0]);
  20.  
  21. }
  22.  
  23. // SAS: Rotate the image.
  24. rotateBase64Image(base64ImageSrc) {
  25. const canvas = document.createElement('canvas');
  26. const img = new Image();
  27. img.onload = function () {
  28. canvas.width = img.width;
  29. canvas.height = img.height;
  30. console.log('image height and width', canvas.width , canvas.height);
  31. }
  32. img.src = base64ImageSrc;
  33. const context = canvas.getContext('2d');
  34. context.translate((img.width), (img.height));
  35. // context.rotate(180 * (Math.PI / 180));
  36. context.rotate(90);
  37. context.drawImage(img, 0, 0);
  38. console.log(canvas);
  39. console.log('the rotated image', canvas.toDataURL());
  40. return canvas.toDataURL();
  41. }
  42.  
  43. // SAS: Data URI to Blob
  44. dataURItoBlob(dataURI) {
  45. // convert base64 to raw binary data held in a string
  46. // doesn't handle URLEncoded DataURIs
  47. const byteString = atob(dataURI.split(',')[1]);
  48.  
  49. // separate out the mime component
  50. const mimeString = dataURI.split(',')[0].split(':')[1].split(';')[0]
  51.  
  52. // write the bytes of the string to an ArrayBuffer
  53. const ab = new ArrayBuffer(byteString.length);
  54.  
  55. // create a view into the buffer
  56. const ia = new Uint8Array(ab);
  57.  
  58. // set the bytes of the buffer to the correct values
  59. for (let i = 0; i < byteString.length; i++) {
  60. ia[i] = byteString.charCodeAt(i);
  61. }
  62.  
  63. // write the ArrayBuffer to a blob, and you're done
  64. const blob = new Blob([ab], {type: mimeString});
  65. console.log('the value of the blob', blob);
  66. return blob;
  67. }
  68.  
  69. uploadFiles(selFile) {
  70. // SAS: Setting the 'image uploaded flag' to be retrieved in quick post to prevent duplicate placeholders.
  71.  
  72. // const file = this.selectedFiles.item(0);
  73. const file = selFile;
  74. this.currentUpload = new Upload(file);
  75.  
  76. // This is the API call to upload the file.
  77. this.storageApiService.createBlob(file).subscribe((response) => {
  78.  
  79. console.log(response);
  80. }, (error) => {
  81. console.log(error);
  82. });
  83. }
  84. }
Add Comment
Please, Sign In to add comment