From 253f0e62ab7f9a46ae2f5e50d5acabd0d6ba06d2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Johan=20Satge=CC=81?= Date: Thu, 29 Nov 2018 19:18:02 +0100 Subject: [PATCH 1/2] Fix destination buffer dimensions when rotation is 5 or 7 --- src/transform.js | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/src/transform.js b/src/transform.js index 26f1c51..94a3d25 100644 --- a/src/transform.js +++ b/src/transform.js @@ -35,14 +35,17 @@ m.do = function(buffer, orientation, quality, module_callback) { if (transformations[orientation].rotate > 0) { new_buffer = _rotate(new_buffer, jpeg.width, jpeg.height, transformations[orientation].rotate) } + + const ratioWillChange = (transformations[orientation].rotate / 90) % 2 === 1 + const destWidth = ratioWillChange ? jpeg.height : jpeg.width + const destHeight = ratioWillChange ? jpeg.width : jpeg.height + if (transformations[orientation].flip) { - new_buffer = _flip(new_buffer, jpeg.width, jpeg.height) + new_buffer = _flip(new_buffer, destWidth, destHeight) } - const width = orientation < 5 ? jpeg.width : jpeg.height - const height = orientation < 5 ? jpeg.height : jpeg.width - const new_jpeg = jpegjs.encode({data: new_buffer, width: width, height: height}, quality) - module_callback(null, new_jpeg.data, width, height) + const new_jpeg = jpegjs.encode({data: new_buffer, width: destWidth, height: destHeight}, quality) + module_callback(null, new_jpeg.data, destWidth, destHeight) } /** From a0acaf2c8083d47f2a2605a64230c5bebc8b6851 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Johan=20Satge=CC=81?= Date: Thu, 29 Nov 2018 19:18:47 +0100 Subject: [PATCH 2/2] Write test images in test/samples/transformed for visual checking when running "npm test" --- .gitignore | 1 + test/samples/transformed/.gitkeep | 0 test/test.js | 3 +++ 3 files changed, 4 insertions(+) create mode 100644 .gitignore create mode 100644 test/samples/transformed/.gitkeep diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..714535d --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +test/samples/transformed/*.jpg diff --git a/test/samples/transformed/.gitkeep b/test/samples/transformed/.gitkeep new file mode 100644 index 0000000..e69de29 diff --git a/test/test.js b/test/test.js index 34e4577..4ac70f6 100644 --- a/test/test.js +++ b/test/test.js @@ -132,6 +132,9 @@ function itShouldTransform(path_or_buffer, label) { if (!compareEXIF(orig_exif, dest_exif)) { throw new Error('EXIF do not match') } + if (typeof path_or_buffer === 'string') { + fs.writeFileSync(path_or_buffer.replace('samples/', 'samples/transformed/'), buffer) + } done() }) })