Skip to content

Commit

Permalink
Merge pull request #132 from stlk/feature/keep-rotation
Browse files Browse the repository at this point in the history
Maintant orientation specified in EXIF data
  • Loading branch information
zachleat authored Feb 23, 2022
2 parents 545b2de + 8b70972 commit 5ecae3e
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 0 deletions.
11 changes: 11 additions & 0 deletions img.js
Original file line number Diff line number Diff line change
Expand Up @@ -388,6 +388,14 @@ class Image {
let results = [];
let outputFormats = Image.getFormatsArray(this.options.formats, metadata.format || this.options.overrideInputFormat);

// Orientation 5 to 8 means dimensions are fliped
if (metadata.orientation >= 5) {
let height = metadata.height;
let width = metadata.width;
metadata.width = height;
metadata.height = width;
}

for(let outputFormat of outputFormats) {
if(!outputFormat || outputFormat === "auto") {
throw new Error("When using statsSync or statsByDimensionsSync, `formats: [null | auto]` to use the native image format is not supported.");
Expand Down Expand Up @@ -458,7 +466,10 @@ class Image {
if(metadata.format !== "svg" || !this.options.svgAllowUpscale) {
resizeOptions.withoutEnlargement = true;
}
sharpInstance.rotate();
sharpInstance.resize(resizeOptions);
} else if (stat.width === metadata.width && metadata.format !== "svg") {
sharpInstance.rotate();
}

if(!this.options.dryRun) {
Expand Down
Binary file added test/orientation.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
12 changes: 12 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,18 @@ test("Use exact same width as original", async t => {
t.is(stats.jpeg[0].width, 1280);
});

test("Maintains orientation", async t => {
let stats = await eleventyImage("./test/orientation.jpg", {
widths: [151],
formats: ["jpeg"],
outputDir: "./test/img/"
});

t.is(stats.jpeg.length, 1);
t.is(stats.jpeg[0].width, 76);
t.is(stats.jpeg[0].height, 151);
});

test("Try to use a width larger than original (statsSync)", t => {
let stats = eleventyImage.statsSync("./test/bio-2017.jpg", {
widths: [1500],
Expand Down

0 comments on commit 5ecae3e

Please sign in to comment.