|
| 1 | +import { Injectable } from '@angular/core'; |
| 2 | +import { Platform } from 'ionic-angular'; |
| 3 | +import { ImagePicker } from '@ionic-native/image-picker'; |
| 4 | +import { UploadFS } from 'meteor/jalik:ufs'; |
| 5 | + |
| 6 | +@Injectable() |
| 7 | +export class PictureService { |
| 8 | + constructor(private platform: Platform, |
| 9 | + private imagePicker: ImagePicker) { |
| 10 | + } |
| 11 | + |
| 12 | + select(): Promise<File> { |
| 13 | + if (!this.platform.is('cordova') || !this.platform.is('mobile')) { |
| 14 | + return new Promise((resolve, reject) => { |
| 15 | + try { |
| 16 | + UploadFS.selectFile((file: File) => { |
| 17 | + resolve(file); |
| 18 | + }); |
| 19 | + } |
| 20 | + catch (e) { |
| 21 | + reject(e); |
| 22 | + } |
| 23 | + }); |
| 24 | + } |
| 25 | + |
| 26 | + return this.imagePicker.getPictures({maximumImagesCount: 1}).then((URL: string) => { |
| 27 | + return this.convertURLtoBlob(URL); |
| 28 | + }); |
| 29 | + } |
| 30 | + |
| 31 | + convertURLtoBlob(url: string, options = {}): Promise<File> { |
| 32 | + return new Promise((resolve, reject) => { |
| 33 | + const image = document.createElement('img'); |
| 34 | + |
| 35 | + image.onload = () => { |
| 36 | + try { |
| 37 | + const dataURI = this.convertImageToDataURI(image, options); |
| 38 | + const blob = this.convertDataURIToBlob(dataURI); |
| 39 | + const pathname = (new URL(url)).pathname; |
| 40 | + const filename = pathname.substring(pathname.lastIndexOf('/') + 1); |
| 41 | + const file = new File([blob], filename); |
| 42 | + |
| 43 | + resolve(file); |
| 44 | + } |
| 45 | + catch (e) { |
| 46 | + reject(e); |
| 47 | + } |
| 48 | + }; |
| 49 | + |
| 50 | + image.src = url; |
| 51 | + }); |
| 52 | + } |
| 53 | + |
| 54 | + convertImageToDataURI(image: HTMLImageElement, {MAX_WIDTH = 400, MAX_HEIGHT = 400} = {}): string { |
| 55 | + // Create an empty canvas element |
| 56 | + const canvas = document.createElement('canvas'); |
| 57 | + |
| 58 | + var width = image.width, height = image.height; |
| 59 | + |
| 60 | + if (width > height) { |
| 61 | + if (width > MAX_WIDTH) { |
| 62 | + height *= MAX_WIDTH / width; |
| 63 | + width = MAX_WIDTH; |
| 64 | + } |
| 65 | + } else { |
| 66 | + if (height > MAX_HEIGHT) { |
| 67 | + width *= MAX_HEIGHT / height; |
| 68 | + height = MAX_HEIGHT; |
| 69 | + } |
| 70 | + } |
| 71 | + |
| 72 | + canvas.width = width; |
| 73 | + canvas.height = height; |
| 74 | + |
| 75 | + // Copy the image contents to the canvas |
| 76 | + const context = canvas.getContext('2d'); |
| 77 | + context.drawImage(image, 0, 0, width, height); |
| 78 | + |
| 79 | + // Get the data-URL formatted image |
| 80 | + // Firefox supports PNG and JPEG. You could check image.src to |
| 81 | + // guess the original format, but be aware the using 'image/jpg' |
| 82 | + // will re-encode the image. |
| 83 | + const dataURL = canvas.toDataURL('image/png'); |
| 84 | + |
| 85 | + return dataURL.replace(/^data:image\/(png|jpg);base64,/, ''); |
| 86 | + } |
| 87 | + |
| 88 | + convertDataURIToBlob(dataURI): Blob { |
| 89 | + const binary = atob(dataURI); |
| 90 | + |
| 91 | + // Write the bytes of the string to a typed array |
| 92 | + const charCodes = Object.keys(binary) |
| 93 | + .map<number>(Number) |
| 94 | + .map<number>(binary.charCodeAt.bind(binary)); |
| 95 | + |
| 96 | + // Build blob with typed array |
| 97 | + return new Blob([new Uint8Array(charCodes)], {type: 'image/jpeg'}); |
| 98 | + } |
| 99 | +} |
0 commit comments