|
9 | 9 | * http://www.opensource.org/licenses/MIT |
10 | 10 | */ |
11 | 11 |
|
12 | | -/*global window, describe, it, expect */ |
| 12 | +/*global window, describe, it, expect, Blob */ |
13 | 13 |
|
14 | 14 | (function (expect, loadImage) { |
15 | 15 | 'use strict'; |
16 | 16 |
|
17 | | - // 80x60px GIF image (color black, base64 data): |
18 | | - var b64Data = 'R0lGODdhUAA8AIABAAAAAP///ywAAAAAUAA8AAACS4SPqcvtD6' + |
| 17 | + var canCreateBlob = !!window.dataURLtoBlob, |
| 18 | + // 80x60px GIF image (color black, base64 data): |
| 19 | + b64DataGIF = 'R0lGODdhUAA8AIABAAAAAP///ywAAAAAUAA8AAACS4SPqcvtD6' + |
19 | 20 | 'OctNqLs968+w+G4kiW5omm6sq27gvH8kzX9o3n+s73/g8MCofE' + |
20 | 21 | 'ovGITCqXzKbzCY1Kp9Sq9YrNarfcrvcLDovH5PKsAAA7', |
21 | | - imageUrl = 'data:image/gif;base64,' + b64Data, |
22 | | - blob = window.dataURLtoBlob && window.dataURLtoBlob(imageUrl); |
| 22 | + imageUrlGIF = 'data:image/gif;base64,' + b64DataGIF, |
| 23 | + blobGIF = canCreateBlob && window.dataURLtoBlob(imageUrlGIF), |
| 24 | + // 1x2px JPEG (color white, with the Exif orientation flag set to 6): |
| 25 | + b64DataJPEG = '/9j/4AAQSkZJRgABAQEAYABgAAD/4QAiRXhpZgAASUkqAAgAAA' + |
| 26 | + 'ABABIBAwABAAAABgASAAAAAAD/2wBDAAEBAQEBAQEBAQEBAQEB' + |
| 27 | + 'AQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQ' + |
| 28 | + 'EBAQEBAQEBAQEBAQH/2wBDAQEBAQEBAQEBAQEBAQEBAQEBAQEB' + |
| 29 | + 'AQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQ' + |
| 30 | + 'EBAQEBAQH/wAARCAABAAIDASIAAhEBAxEB/8QAHwAAAQUBAQEB' + |
| 31 | + 'AQEAAAAAAAAAAAECAwQFBgcICQoL/8QAtRAAAgEDAwIEAwUFBA' + |
| 32 | + 'QAAAF9AQIDAAQRBRIhMUEGE1FhByJxFDKBkaEII0KxwRVS0fAk' + |
| 33 | + 'M2JyggkKFhcYGRolJicoKSo0NTY3ODk6Q0RFRkdISUpTVFVWV1' + |
| 34 | + 'hZWmNkZWZnaGlqc3R1dnd4eXqDhIWGh4iJipKTlJWWl5iZmqKj' + |
| 35 | + 'pKWmp6ipqrKztLW2t7i5usLDxMXGx8jJytLT1NXW19jZ2uHi4+' + |
| 36 | + 'Tl5ufo6erx8vP09fb3+Pn6/8QAHwEAAwEBAQEBAQEBAQAAAAAA' + |
| 37 | + 'AAECAwQFBgcICQoL/8QAtREAAgECBAQDBAcFBAQAAQJ3AAECAx' + |
| 38 | + 'EEBSExBhJBUQdhcRMiMoEIFEKRobHBCSMzUvAVYnLRChYkNOEl' + |
| 39 | + '8RcYGRomJygpKjU2Nzg5OkNERUZHSElKU1RVVldYWVpjZGVmZ2' + |
| 40 | + 'hpanN0dXZ3eHl6goOEhYaHiImKkpOUlZaXmJmaoqOkpaanqKmq' + |
| 41 | + 'srO0tba3uLm6wsPExcbHyMnK0tPU1dbX2Nna4uPk5ebn6Onq8v' + |
| 42 | + 'P09fb3+Pn6/9oADAMBAAIRAxEAPwD+/iiiigD/2Q==', |
| 43 | + imageUrlJPEG = 'data:image/jpeg;base64,' + b64DataJPEG, |
| 44 | + blobJPEG = canCreateBlob && window.dataURLtoBlob(imageUrlJPEG); |
23 | 45 |
|
24 | 46 | describe('Loading', function () { |
25 | 47 |
|
26 | 48 | it('Return the img element or FileReader object to allow aborting the image load', function () { |
27 | | - var img = loadImage(blob, function () {}); |
| 49 | + var img = loadImage(blobGIF, function () { |
| 50 | + return; |
| 51 | + }); |
28 | 52 | expect(img).to.be.an(Object); |
29 | 53 | expect(img.onload).to.be.a('function'); |
30 | 54 | expect(img.onerror).to.be.a('function'); |
31 | 55 | }); |
32 | 56 |
|
33 | 57 | it('Load image url', function (done) { |
34 | | - expect(loadImage(imageUrl, function (img) { |
| 58 | + expect(loadImage(imageUrlGIF, function (img) { |
35 | 59 | done(); |
36 | 60 | expect(img.width).to.be(80); |
37 | 61 | expect(img.height).to.be(60); |
38 | 62 | })).to.be.ok(); |
39 | 63 | }); |
40 | 64 |
|
41 | 65 | it('Load image blob', function (done) { |
42 | | - expect(loadImage(blob, function (img) { |
| 66 | + expect(loadImage(blobGIF, function (img) { |
43 | 67 | done(); |
44 | 68 | expect(img.width).to.be(80); |
45 | 69 | expect(img.height).to.be(60); |
|
55 | 79 | }); |
56 | 80 |
|
57 | 81 | it('Keep object URL if options.noRevoke is true', function (done) { |
58 | | - expect(loadImage(blob, function (img) { |
| 82 | + expect(loadImage(blobGIF, function (img) { |
59 | 83 | loadImage(img.src, function (img2) { |
60 | 84 | done(); |
61 | 85 | expect(img.width).to.be(img2.width); |
|
65 | 89 | }); |
66 | 90 |
|
67 | 91 | it('Discard object URL if options.noRevoke is undefined or false', function (done) { |
68 | | - expect(loadImage(blob, function (img) { |
| 92 | + expect(loadImage(blobGIF, function (img) { |
69 | 93 | loadImage(img.src, function (img2) { |
70 | 94 | done(); |
71 | 95 | expect(img2).to.be.a(window.Event); |
|
79 | 103 | describe('Scaling', function () { |
80 | 104 |
|
81 | 105 | it('Scale to options.maxWidth', function (done) { |
82 | | - expect(loadImage(blob, function (img) { |
| 106 | + expect(loadImage(blobGIF, function (img) { |
83 | 107 | done(); |
84 | 108 | expect(img.width).to.be(40); |
85 | 109 | expect(img.height).to.be(30); |
86 | 110 | }, {maxWidth: 40})).to.be.ok(); |
87 | 111 | }); |
88 | 112 |
|
89 | 113 | it('Scale to options.maxHeight', function (done) { |
90 | | - expect(loadImage(blob, function (img) { |
| 114 | + expect(loadImage(blobGIF, function (img) { |
91 | 115 | done(); |
92 | 116 | expect(img.width).to.be(20); |
93 | 117 | expect(img.height).to.be(15); |
94 | 118 | }, {maxHeight: 15})).to.be.ok(); |
95 | 119 | }); |
96 | 120 |
|
97 | 121 | it('Scale to options.minWidth', function (done) { |
98 | | - expect(loadImage(blob, function (img) { |
| 122 | + expect(loadImage(blobGIF, function (img) { |
99 | 123 | done(); |
100 | 124 | expect(img.width).to.be(160); |
101 | 125 | expect(img.height).to.be(120); |
102 | 126 | }, {minWidth: 160})).to.be.ok(); |
103 | 127 | }); |
104 | 128 |
|
105 | 129 | it('Scale to options.minHeight', function (done) { |
106 | | - expect(loadImage(blob, function (img) { |
| 130 | + expect(loadImage(blobGIF, function (img) { |
107 | 131 | done(); |
108 | 132 | expect(img.width).to.be(320); |
109 | 133 | expect(img.height).to.be(240); |
110 | 134 | }, {minHeight: 240})).to.be.ok(); |
111 | 135 | }); |
112 | 136 |
|
113 | 137 | it('Scale to options.minWidth but respect options.maxWidth', function (done) { |
114 | | - expect(loadImage(blob, function (img) { |
| 138 | + expect(loadImage(blobGIF, function (img) { |
115 | 139 | done(); |
116 | 140 | expect(img.width).to.be(160); |
117 | 141 | expect(img.height).to.be(120); |
118 | 142 | }, {minWidth: 240, maxWidth: 160})).to.be.ok(); |
119 | 143 | }); |
120 | 144 |
|
121 | 145 | it('Scale to options.minHeight but respect options.maxHeight', function (done) { |
122 | | - expect(loadImage(blob, function (img) { |
| 146 | + expect(loadImage(blobGIF, function (img) { |
123 | 147 | done(); |
124 | 148 | expect(img.width).to.be(160); |
125 | 149 | expect(img.height).to.be(120); |
126 | 150 | }, {minHeight: 180, maxHeight: 120})).to.be.ok(); |
127 | 151 | }); |
128 | 152 |
|
129 | 153 | it('Scale to options.minWidth but respect options.maxHeight', function (done) { |
130 | | - expect(loadImage(blob, function (img) { |
| 154 | + expect(loadImage(blobGIF, function (img) { |
131 | 155 | done(); |
132 | 156 | expect(img.width).to.be(160); |
133 | 157 | expect(img.height).to.be(120); |
134 | 158 | }, {minWidth: 240, maxHeight: 120})).to.be.ok(); |
135 | 159 | }); |
136 | 160 |
|
137 | 161 | it('Scale to options.minHeight but respect options.maxWidth', function (done) { |
138 | | - expect(loadImage(blob, function (img) { |
| 162 | + expect(loadImage(blobGIF, function (img) { |
139 | 163 | done(); |
140 | 164 | expect(img.width).to.be(160); |
141 | 165 | expect(img.height).to.be(120); |
142 | 166 | }, {minHeight: 180, maxWidth: 160})).to.be.ok(); |
143 | 167 | }); |
144 | 168 |
|
145 | 169 | it('Do not scale to max settings without min settings', function (done) { |
146 | | - expect(loadImage(blob, function (img) { |
| 170 | + expect(loadImage(blobGIF, function (img) { |
147 | 171 | done(); |
148 | 172 | expect(img.width).to.be(80); |
149 | 173 | expect(img.height).to.be(60); |
150 | 174 | }, {maxWidth: 160, maxHeight: 120})).to.be.ok(); |
151 | 175 | }); |
152 | 176 |
|
153 | 177 | it('Do not scale to min settings without max settings', function (done) { |
154 | | - expect(loadImage(blob, function (img) { |
| 178 | + expect(loadImage(blobGIF, function (img) { |
155 | 179 | done(); |
156 | 180 | expect(img.width).to.be(80); |
157 | 181 | expect(img.height).to.be(60); |
|
164 | 188 | describe('Cropping', function () { |
165 | 189 |
|
166 | 190 | it('Crop to same values for options.maxWidth and options.maxHeight', function (done) { |
167 | | - expect(loadImage(blob, function (img) { |
| 191 | + expect(loadImage(blobGIF, function (img) { |
168 | 192 | done(); |
169 | 193 | expect(img.width).to.be(40); |
170 | 194 | expect(img.height).to.be(40); |
171 | 195 | }, {maxWidth: 40, maxHeight: 40, crop: true})).to.be.ok(); |
172 | 196 | }); |
173 | 197 |
|
174 | 198 | it('Crop to different values for options.maxWidth and options.maxHeight', function (done) { |
175 | | - expect(loadImage(blob, function (img) { |
| 199 | + expect(loadImage(blobGIF, function (img) { |
176 | 200 | done(); |
177 | 201 | expect(img.width).to.be(40); |
178 | 202 | expect(img.height).to.be(60); |
|
184 | 208 | describe('Orientation', function () { |
185 | 209 |
|
186 | 210 | it('Should keep the orientation', function (done) { |
187 | | - expect(loadImage(blob, function (img) { |
| 211 | + expect(loadImage(blobGIF, function (img) { |
188 | 212 | done(); |
189 | 213 | expect(img.width).to.be(80); |
190 | 214 | expect(img.height).to.be(60); |
191 | 215 | }, {orientation: 1})).to.be.ok(); |
192 | 216 | }); |
193 | 217 |
|
194 | 218 | it('Should rotate right', function (done) { |
195 | | - expect(loadImage(blob, function (img) { |
| 219 | + expect(loadImage(blobGIF, function (img) { |
196 | 220 | done(); |
197 | 221 | expect(img.width).to.be(60); |
198 | 222 | expect(img.height).to.be(80); |
199 | 223 | }, {orientation: 6})).to.be.ok(); |
200 | 224 | }); |
201 | 225 |
|
202 | 226 | it('Should rotate left', function (done) { |
203 | | - expect(loadImage(blob, function (img) { |
| 227 | + expect(loadImage(blobGIF, function (img) { |
204 | 228 | done(); |
205 | 229 | expect(img.width).to.be(60); |
206 | 230 | expect(img.height).to.be(80); |
207 | 231 | }, {orientation: 8})).to.be.ok(); |
208 | 232 | }); |
209 | 233 |
|
210 | 234 | it('Should adjust constraints to new coordinates', function (done) { |
211 | | - expect(loadImage(blob, function (img) { |
| 235 | + expect(loadImage(blobGIF, function (img) { |
212 | 236 | done(); |
213 | 237 | expect(img.width).to.be(60); |
214 | 238 | expect(img.height).to.be(80); |
|
220 | 244 | describe('Canvas', function () { |
221 | 245 |
|
222 | 246 | it('Return img element to callback if options.canvas is not true', function (done) { |
223 | | - expect(loadImage(blob, function (img) { |
| 247 | + expect(loadImage(blobGIF, function (img) { |
224 | 248 | done(); |
225 | 249 | expect(img.getContext).to.not.be.ok(); |
226 | 250 | expect(img.nodeName.toLowerCase()).to.be('img'); |
227 | 251 | })).to.be.ok(); |
228 | 252 | }); |
229 | 253 |
|
230 | 254 | it('Return canvas element to callback if options.canvas is true', function (done) { |
231 | | - expect(loadImage(blob, function (img) { |
| 255 | + expect(loadImage(blobGIF, function (img) { |
232 | 256 | done(); |
233 | 257 | expect(img.getContext).to.be.ok(); |
234 | 258 | expect(img.nodeName.toLowerCase()).to.be('canvas'); |
235 | 259 | }, {canvas: true})).to.be.ok(); |
236 | 260 | }); |
237 | 261 |
|
238 | 262 | it('Return scaled canvas element to callback', function (done) { |
239 | | - expect(loadImage(blob, function (img) { |
| 263 | + expect(loadImage(blobGIF, function (img) { |
240 | 264 | done(); |
241 | 265 | expect(img.getContext).to.be.ok(); |
242 | 266 | expect(img.nodeName.toLowerCase()).to.be('canvas'); |
|
246 | 270 | }); |
247 | 271 |
|
248 | 272 | it('Accept a canvas element as parameter for loadImage.scale', function (done) { |
249 | | - expect(loadImage(blob, function (img) { |
| 273 | + expect(loadImage(blobGIF, function (img) { |
250 | 274 | done(); |
251 | 275 | img = loadImage.scale(img, { |
252 | 276 | maxWidth: 40 |
|
260 | 284 |
|
261 | 285 | }); |
262 | 286 |
|
| 287 | + describe('Metadata', function () { |
| 288 | + |
| 289 | + it('Should parse Exif information', function (done) { |
| 290 | + loadImage.parseMetaData(blobJPEG, function (data) { |
| 291 | + done(); |
| 292 | + expect(data.exif).to.be.ok(); |
| 293 | + expect(data.exif.get('Orientation')).to.be(6); |
| 294 | + }); |
| 295 | + }); |
| 296 | + |
| 297 | + it('Should parse the complete image head', function (done) { |
| 298 | + loadImage.parseMetaData(blobJPEG, function (data) { |
| 299 | + expect(data.imageHead).to.be.ok(); |
| 300 | + loadImage.parseMetaData( |
| 301 | + new Blob([data.imageHead], {type: 'image/jpeg'}), |
| 302 | + function (data) { |
| 303 | + done(); |
| 304 | + expect(data.exif).to.be.ok(); |
| 305 | + expect(data.exif.get('Orientation')).to.be(6); |
| 306 | + } |
| 307 | + ); |
| 308 | + }); |
| 309 | + }); |
| 310 | + |
| 311 | + }); |
| 312 | + |
263 | 313 | }( |
264 | 314 | this.expect, |
265 | 315 | this.loadImage |
|
0 commit comments