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/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) } /** 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() }) })