From f017499cbfee3fe68ee09bd19de5b83afadd45b7 Mon Sep 17 00:00:00 2001 From: Zutatensuppe Date: Wed, 9 Dec 2020 00:15:04 +0100 Subject: [PATCH] fix for rotated jpegs --- package-lock.json | 8 ++++++++ package.json | 1 + server/Puzzle.js | 31 ++++++++++++++++++++++++++++++- 3 files changed, 39 insertions(+), 1 deletion(-) diff --git a/package-lock.json b/package-lock.json index 03ba5bc..30de56e 100644 --- a/package-lock.json +++ b/package-lock.json @@ -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", diff --git a/package.json b/package.json index 42efc53..a0710ee 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/server/Puzzle.js b/server/Puzzle.js index ad89014..fad6144 100644 --- a/server/Puzzle.js +++ b/server/Puzzle.js @@ -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)