summary refs log tree commit diff
path: root/webclient/components/utilities/utilities-service.js
blob: 3df2f044587589b58ea4d73426f5b917eb271d03 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
/*
 Copyright 2014 matrix.org
 
 Licensed under the Apache License, Version 2.0 (the "License");
 you may not use this file except in compliance with the License.
 You may obtain a copy of the License at
 
 http://www.apache.org/licenses/LICENSE-2.0
 
 Unless required by applicable law or agreed to in writing, software
 distributed under the License is distributed on an "AS IS" BASIS,
 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 See the License for the specific language governing permissions and
 limitations under the License.
 */

'use strict';

/*
 * This service contains multipurpose helper functions.
 */
angular.module('mUtilities', [])
.service('mUtilities', ['$q', function ($q) {
    /*
     * Get the size of an image
     * @param {File|Blob} imageFile the file containing the image
     * @returns {promise} A promise that will be resolved by an object with 2 members:
     *   width & height
     */
    this.getImageSize = function(imageFile) {
        var deferred = $q.defer();
        
        // Load the file into an html element
        var img = document.createElement("img");
        
        var reader = new FileReader();  
        reader.onload = function(e) {   
            img.src = e.target.result;
            
            // Once ready, returns its size
            img.onload = function() {
                deferred.resolve({
                    width: img.width,
                    height: img.height
                });
            };
            img.onerror = function(e) {
                deferred.reject(e);
            };
        };
        reader.onerror = function(e) {
            deferred.reject(e);
        };
        reader.readAsDataURL(imageFile);
        
        return deferred.promise;
    };

    /*
     * Resize the image to fit in a square of the side maxSize. 
     * The aspect ratio is kept. The returned image data uses JPEG compression.
     * Source: http://hacks.mozilla.org/2011/01/how-to-develop-a-html5-image-uploader/
     * @param {File} imageFile the file containing the image 
     * @param {Integer} maxSize the max side size 
     * @returns {promise} A promise that will be resolved by a Blob object containing
     *   the resized image data
     */
    this.resizeImage = function(imageFile, maxSize) {
        var self = this;
        var deferred = $q.defer();

        var canvas = document.createElement("canvas");

        var img = document.createElement("img");
        var reader = new FileReader();  
        reader.onload = function(e) {

            img.src = e.target.result;
            
            // Once ready, returns its size
            img.onload = function() {
                var ctx = canvas.getContext("2d");
                ctx.drawImage(img, 0, 0);

                var MAX_WIDTH = maxSize;
                var MAX_HEIGHT = maxSize;
                var width = img.width;
                var height = img.height;

                if (width > height) {
                    if (width > MAX_WIDTH) {
                        height *= MAX_WIDTH / width;
                        width = MAX_WIDTH;
                    }
                } else {
                    if (height > MAX_HEIGHT) {
                        width *= MAX_HEIGHT / height;
                        height = MAX_HEIGHT;
                    }
                }
                canvas.width = width;
                canvas.height = height;
                var ctx = canvas.getContext("2d");
                ctx.drawImage(img, 0, 0, width, height);

                // Extract image data in the same format as the original one.
                // The 0.7 compression value will work with formats that supports it like JPEG.
                var dataUrl = canvas.toDataURL(imageFile.type, 0.7); 
                deferred.resolve(self.dataURItoBlob(dataUrl));
            };
            img.onerror = function(e) {
                deferred.reject(e);
            };
        };
        reader.onerror = function(e) {
            deferred.reject(e);
        };
        reader.readAsDataURL(imageFile);

        return deferred.promise;
    };

    /*
     * Convert a dataURI string to a blob 
     * Source: http://stackoverflow.com/a/17682951
     * @param {String} dataURI the dataURI can be a base64 encoded string or an URL encoded string.
     * @returns {Blob} the blob
     */
    this.dataURItoBlob = function(dataURI) {
        // convert base64 to raw binary data held in a string
        // doesn't handle URLEncoded DataURIs
        var byteString;
        if (dataURI.split(',')[0].indexOf('base64') >= 0)
            byteString = atob(dataURI.split(',')[1]);
        else
            byteString = unescape(dataURI.split(',')[1]);
        // separate out the mime component
        var mimeString = dataURI.split(',')[0].split(':')[1].split(';')[0];

        // write the bytes of the string to an ArrayBuffer
        var ab = new ArrayBuffer(byteString.length);
        var ia = new Uint8Array(ab);
        for (var i = 0; i < byteString.length; i++) {
            ia[i] = byteString.charCodeAt(i);
        }

        // write the ArrayBuffer to a blob, and you're done
        return new Blob([ab],{type: mimeString});
    };

}]);