]>
cat aescling's git repositories - mastodon.git/blob - app/javascript/flavours/glitch/util/resize_image.js
1 import EXIF
from 'exif-js';
3 const MAX_IMAGE_PIXELS
= 1638400; // 1280x1280px
5 const getImageUrl
= inputFile
=> new Promise((resolve
, reject
) => {
6 if (window
.URL
&& URL
.createObjectURL
) {
8 resolve(URL
.createObjectURL(inputFile
));
15 const reader
= new FileReader();
16 reader
.onerror
= (...args
) => reject(...args
);
17 reader
.onload
= ({ target
}) => resolve(target
.result
);
19 reader
.readAsDataURL(inputFile
);
22 const loadImage
= inputFile
=> new Promise((resolve
, reject
) => {
23 getImageUrl(inputFile
).then(url
=> {
24 const img
= new Image();
26 img
.onerror
= (...args
) => reject(...args
);
27 img
.onload
= () => resolve(img
);
33 const getOrientation
= (img
, type
= 'image/png') => new Promise(resolve
=> {
34 if (type
!== 'image/jpeg') {
39 EXIF
.getData(img
, () => {
40 const orientation
= EXIF
.getTag(img
, 'Orientation');
45 const processImage
= (img
, { width
, height
, orientation
, type
= 'image/png' }) => new Promise(resolve
=> {
46 const canvas
= document
.createElement('canvas');
48 if (4 < orientation
&& orientation
< 9) {
49 canvas
.width
= height
;
50 canvas
.height
= width
;
53 canvas
.height
= height
;
56 const context
= canvas
.getContext('2d');
58 switch (orientation
) {
59 case 2: context
.transform(-1, 0, 0, 1, width
, 0); break;
60 case 3: context
.transform(-1, 0, 0, -1, width
, height
); break;
61 case 4: context
.transform(1, 0, 0, -1, 0, height
); break;
62 case 5: context
.transform(0, 1, 1, 0, 0, 0); break;
63 case 6: context
.transform(0, 1, -1, 0, height
, 0); break;
64 case 7: context
.transform(0, -1, -1, 0, height
, width
); break;
65 case 8: context
.transform(0, -1, 1, 0, 0, width
); break;
68 context
.drawImage(img
, 0, 0, width
, height
);
70 canvas
.toBlob(resolve
, type
);
73 const resizeImage
= (img
, type
= 'image/png') => new Promise((resolve
, reject
) => {
74 const { width
, height
} = img
;
76 const newWidth
= Math
.round(Math
.sqrt(MAX_IMAGE_PIXELS
* (width
/ height
)));
77 const newHeight
= Math
.round(Math
.sqrt(MAX_IMAGE_PIXELS
* (height
/ width
)));
79 getOrientation(img
, type
)
80 .then(orientation
=> processImage(img
, {
90 export default inputFile
=> new Promise((resolve
, reject
) => {
91 if (!inputFile
.type
.match(/image.*/) || inputFile
.type
=== 'image/gif') {
96 loadImage(inputFile
).then(img
=> {
97 if (img
.width
* img
.height
< MAX_IMAGE_PIXELS
) {
102 resizeImage(img
, inputFile
.type
)
104 .catch(() => resolve(inputFile
));
This page took 0.09047 seconds and 4 git commands to generate.