Skip to content

Commit

Permalink
Only rotate for 11ty#132 if metadata.orientation needs it
Browse files Browse the repository at this point in the history
  • Loading branch information
zachleat committed Feb 24, 2022
1 parent d2ea2d0 commit 434a264
Showing 1 changed file with 13 additions and 4 deletions.
17 changes: 13 additions & 4 deletions img.js
Original file line number Diff line number Diff line change
Expand Up @@ -382,14 +382,19 @@ class Image {
return stats;
}

// https://jdhao.github.io/2019/07/31/image_rotation_exif_info/
// Orientation 5 to 8 means image is rotated (width/height are flipped)
needsRotation(orientation) {
return orientation >= 5;
}

// metadata so far: width, height, format
// src is used to calculate the output file names
getFullStats(metadata) {
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) {
if (this.needsRotation(metadata.orientation)) {
let height = metadata.height;
let width = metadata.width;
metadata.width = height;
Expand Down Expand Up @@ -466,10 +471,14 @@ class Image {
if(metadata.format !== "svg" || !this.options.svgAllowUpscale) {
resizeOptions.withoutEnlargement = true;
}
sharpInstance.rotate();
if(this.needsRotation(metadata.orientation)) {
sharpInstance.rotate();
}
sharpInstance.resize(resizeOptions);
} else if (stat.width === metadata.width && metadata.format !== "svg") {
sharpInstance.rotate();
if(this.needsRotation(metadata.orientation)) {
sharpInstance.rotate();
}
}

if(!this.options.dryRun) {
Expand Down

0 comments on commit 434a264

Please sign in to comment.