Skip to content

Commit

Permalink
Fix: rotate resize param on multiples of 90 (#1229)
Browse files Browse the repository at this point in the history
  • Loading branch information
sjoerd108 authored May 11, 2023
1 parent 2948127 commit bc22100
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 2 deletions.
8 changes: 6 additions & 2 deletions packages/plugin-rotate/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -231,8 +231,12 @@ export default () => ({
return throwError.call(this, "mode must be a boolean or a string", cb);
}

if (Math.abs(deg % 90) === 0) {
// apply matrixRotate if the angle is a multiple of 90 degrees (eg: 180 or -90)
// use matrixRotate if the angle is a multiple of 90 degrees (eg: 180 or -90) and resize is allowed or not needed.
const matrixRotateAllowed =
deg % 90 === 0 &&
(mode || this.bitmap.width === this.bitmap.height || deg % 180 === 0);

if (matrixRotateAllowed) {
matrixRotate.call(this, deg);
} else {
advancedRotate.call(this, deg, mode, cb);
Expand Down
41 changes: 41 additions & 0 deletions packages/plugin-rotate/test/rotation.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -567,3 +567,44 @@ describe("Rotate a non-square image", () => {
);
});
});

describe("Rotate a non-square image without resizing", () => {
let imgSrc = null;
before((done) => {
jimp
.read(mkJGD("□□□□□□□□", "▹▹▹▹▹▹▹▹", "▿▿▿▿▿▿▿▿", "□□□□□□□□"))
.then((imgJimp) => {
imgSrc = imgJimp;
done();
})
.catch(done);
});

it("90 degrees", () => {
expectToBeJGD(
imgSrc.clone().rotate(90, false).getJGDSync(),
mkJGD(" □▹▿□ ", " □▹▿□ ", " □▹▿□ ", " □▹▿□ ")
);
});

it("180 degrees", () => {
expectToBeJGD(
imgSrc.clone().rotate(180, false).getJGDSync(),
mkJGD("□□□□□□□□", "▿▿▿▿▿▿▿▿", "▹▹▹▹▹▹▹▹", "□□□□□□□□")
);
});

it("270 degrees", () => {
expectToBeJGD(
imgSrc.clone().rotate(270, false).getJGDSync(),
mkJGD(" □▿▹□ ", " □▿▹□ ", " □▿▹□ ", " □▿▹□ ")
);
});

it("45 degrees", () => {
expectToBeJGD(
imgSrc.clone().rotate(45, false).getJGDSync(),
mkJGD(" □▹▹▿□□ ", "□▹▹▿□□ ", "▹▹▿□□ ", "▹▿□□ ")
);
});
});

0 comments on commit bc22100

Please sign in to comment.