|
|
@ -1,3 +1,5 @@ |
|
|
|
|
|
|
|
import EXIF from 'exif-js'; |
|
|
|
|
|
|
|
|
|
|
|
const MAX_IMAGE_DIMENSION = 1280; |
|
|
|
const MAX_IMAGE_DIMENSION = 1280; |
|
|
|
|
|
|
|
|
|
|
|
const getImageUrl = inputFile => new Promise((resolve, reject) => { |
|
|
|
const getImageUrl = inputFile => new Promise((resolve, reject) => { |
|
|
@ -28,23 +30,62 @@ const loadImage = inputFile => new Promise((resolve, reject) => { |
|
|
|
}).catch(reject); |
|
|
|
}).catch(reject); |
|
|
|
}); |
|
|
|
}); |
|
|
|
|
|
|
|
|
|
|
|
export default inputFile => new Promise((resolve, reject) => { |
|
|
|
const getOrientation = (img, type = 'image/png') => new Promise(resolve => { |
|
|
|
if (!inputFile.type.match(/image.*/) || inputFile.type === 'image/gif') { |
|
|
|
if (type !== 'image/jpeg') { |
|
|
|
resolve(inputFile); |
|
|
|
resolve(1); |
|
|
|
return; |
|
|
|
return; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
loadImage(inputFile).then(img => { |
|
|
|
EXIF.getData(img, () => { |
|
|
|
|
|
|
|
const orientation = EXIF.getTag(img, 'Orientation'); |
|
|
|
|
|
|
|
resolve(orientation); |
|
|
|
|
|
|
|
}); |
|
|
|
|
|
|
|
}); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const processImage = (img, { width, height, orientation, type = 'image/png' }) => new Promise(resolve => { |
|
|
|
const canvas = document.createElement('canvas'); |
|
|
|
const canvas = document.createElement('canvas'); |
|
|
|
const { width, height } = img; |
|
|
|
[canvas.width, canvas.height] = orientation < 5 ? [width, height] : [height, width]; |
|
|
|
|
|
|
|
|
|
|
|
let newWidth, newHeight; |
|
|
|
const context = canvas.getContext('2d'); |
|
|
|
|
|
|
|
|
|
|
|
if (width < MAX_IMAGE_DIMENSION && height < MAX_IMAGE_DIMENSION) { |
|
|
|
switch (orientation) { |
|
|
|
resolve(inputFile); |
|
|
|
case 2: |
|
|
|
return; |
|
|
|
context.translate(width, 0); |
|
|
|
|
|
|
|
break; |
|
|
|
|
|
|
|
case 3: |
|
|
|
|
|
|
|
context.translate(width, height); |
|
|
|
|
|
|
|
break; |
|
|
|
|
|
|
|
case 4: |
|
|
|
|
|
|
|
context.translate(0, height); |
|
|
|
|
|
|
|
break; |
|
|
|
|
|
|
|
case 5: |
|
|
|
|
|
|
|
context.rotate(0.5 * Math.PI); |
|
|
|
|
|
|
|
context.translate(1, -1); |
|
|
|
|
|
|
|
break; |
|
|
|
|
|
|
|
case 6: |
|
|
|
|
|
|
|
context.rotate(0.5 * Math.PI); |
|
|
|
|
|
|
|
context.translate(0, -height); |
|
|
|
|
|
|
|
break; |
|
|
|
|
|
|
|
case 7: |
|
|
|
|
|
|
|
context.rotate(0.5, Math.PI); |
|
|
|
|
|
|
|
context.translate(width, -height); |
|
|
|
|
|
|
|
break; |
|
|
|
|
|
|
|
case 8: |
|
|
|
|
|
|
|
context.rotate(-0.5, Math.PI); |
|
|
|
|
|
|
|
context.translate(-width, 0); |
|
|
|
|
|
|
|
break; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
context.drawImage(img, 0, 0, width, height); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
canvas.toBlob(resolve, type); |
|
|
|
|
|
|
|
}); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const resizeImage = (img, type = 'image/png') => new Promise((resolve, reject) => { |
|
|
|
|
|
|
|
const { width, height } = img; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
let newWidth, newHeight; |
|
|
|
|
|
|
|
|
|
|
|
if (width > height) { |
|
|
|
if (width > height) { |
|
|
|
newHeight = height * MAX_IMAGE_DIMENSION / width; |
|
|
|
newHeight = height * MAX_IMAGE_DIMENSION / width; |
|
|
|
newWidth = MAX_IMAGE_DIMENSION; |
|
|
|
newWidth = MAX_IMAGE_DIMENSION; |
|
|
@ -56,11 +97,31 @@ export default inputFile => new Promise((resolve, reject) => { |
|
|
|
newHeight = MAX_IMAGE_DIMENSION; |
|
|
|
newHeight = MAX_IMAGE_DIMENSION; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
canvas.width = newWidth; |
|
|
|
getOrientation(img, type) |
|
|
|
canvas.height = newHeight; |
|
|
|
.then(orientation => processImage(img, { |
|
|
|
|
|
|
|
width: newWidth, |
|
|
|
|
|
|
|
height: newHeight, |
|
|
|
|
|
|
|
orientation, |
|
|
|
|
|
|
|
type, |
|
|
|
|
|
|
|
})) |
|
|
|
|
|
|
|
.then(resolve) |
|
|
|
|
|
|
|
.catch(reject); |
|
|
|
|
|
|
|
}); |
|
|
|
|
|
|
|
|
|
|
|
canvas.getContext('2d').drawImage(img, 0, 0, newWidth, newHeight); |
|
|
|
export default inputFile => new Promise((resolve, reject) => { |
|
|
|
|
|
|
|
if (!inputFile.type.match(/image.*/) || inputFile.type === 'image/gif') { |
|
|
|
|
|
|
|
resolve(inputFile); |
|
|
|
|
|
|
|
return; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
loadImage(inputFile).then(img => { |
|
|
|
|
|
|
|
if (img.width < MAX_IMAGE_DIMENSION && img.height < MAX_IMAGE_DIMENSION) { |
|
|
|
|
|
|
|
resolve(inputFile); |
|
|
|
|
|
|
|
return; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
canvas.toBlob(resolve, inputFile.type); |
|
|
|
resizeImage(img, inputFile.type) |
|
|
|
|
|
|
|
.then(resolve) |
|
|
|
|
|
|
|
.catch(() => resolve(inputFile)); |
|
|
|
}).catch(reject); |
|
|
|
}).catch(reject); |
|
|
|
}); |
|
|
|
}); |
|
|
|