fix for rotated jpegs

This commit is contained in:
Zutatensuppe 2020-12-09 00:15:04 +01:00
parent 800ca0cc54
commit f017499cbf
3 changed files with 39 additions and 1 deletions

8
package-lock.json generated
View file

@ -220,6 +220,14 @@
"resolved": "https://npm.stroeermediabrands.de/etag/-/etag-1.8.1.tgz",
"integrity": "sha1-Qa4u62XvpiJorr/qg6x9eSmbCIc="
},
"exif": {
"version": "0.6.0",
"resolved": "https://registry.npmjs.org/exif/-/exif-0.6.0.tgz",
"integrity": "sha1-YKYmaAdlQst+T1cZnUrG830sX0o=",
"requires": {
"debug": "^2.2"
}
},
"express": {
"version": "4.17.1",
"resolved": "https://npm.stroeermediabrands.de/express/-/express-4.17.1.tgz",

View file

@ -2,6 +2,7 @@
"type": "module",
"dependencies": {
"body-parser": "^1.19.0",
"exif": "^0.6.0",
"express": "^4.17.1",
"image-size": "^0.9.3",
"multer": "^1.4.2",

View file

@ -1,16 +1,45 @@
import sizeOf from 'image-size'
import Util from './../common/Util.js'
import exif from 'exif'
// cut size of each puzzle tile in the
// final resized version of the puzzle image
const TILE_SIZE = 64
async function getDimensions(imagePath) {
let dimensions = sizeOf(imagePath)
try {
const orientation = await getExifOrientation(imagePath)
// when image is rotated to the left or right, switch width/height
// https://jdhao.github.io/2019/07/31/image_rotation_exif_info/
if (orientation === 6 || orientation === 8) {
return {
width: dimensions.height,
height: dimensions.width,
}
}
} catch {}
return dimensions
}
async function getExifOrientation(imagePath) {
return new Promise((resolve, reject) => {
new exif.ExifImage({ image: imagePath }, function (error, exifData) {
if (error) {
reject(error)
} else {
resolve(exifData.image.Orientation)
}
})
})
}
async function createPuzzle(targetTiles, image) {
const imagePath = image.file
const imageUrl = image.url
// load bitmap, to determine the original size of the image
const dim = sizeOf(imagePath)
const dim = await getDimensions(imagePath)
// determine puzzle information from the bitmap
const info = determinePuzzleInfo(dim.width, dim.height, targetTiles)