Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: rotate resize param on multiples of 90 #1229

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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(" □▹▹▿□□ ", "□▹▹▿□□ ", "▹▹▿□□ ", "▹▿□□ ")
);
});
});