-
-
Notifications
You must be signed in to change notification settings - Fork 772
Rotation adds black border #721
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
Comments
I faced the same problem. Here is the jsfiddle to reproduce the issue. |
LOL the for loop!!! 😂 |
@tonysamperi |
@tooleks |
After some digging, I found this issue is introduced by #708 Workaround: implement your own |
Is the above merged PR supposed to resolve the black border? I see the PR says "in scope of EXIF rotation", but I'm testing on 0.12.4 and it still shows up. If I only want to do EXIF rotation (90, 180 or 270deg) should I be calling a different rotation function than Not quite sure how I can resolve this without forking the repo and using above suggested |
That could just be another function that the rotate plugin adds. I'd be open to a PR if someone wants to make it |
I had included it ( |
It's a great project but I cant to introduces in my project because I don't have support for ES Modules:c |
No. Thanks to #875, EXIF autorotation stops using
Jimp reads EXIF metadata on file open and autorotates pictures accordingly. |
For the one that doesn't want to add plugins or whatever ES module management Thanks me later "patch-jimp-rotate.js" module.exports = (deg, image) => {
// Restore simple rotate of jimp v0.5.6
// https://github.com/oliver-moran/jimp/issues/821
if (deg % 90 !== 0) {
return image;
}
let steps = Math.round(deg / 90) % 4;
steps += steps < 0 ? 4 : 0;
if (steps === 0) {
return image;
}
const srcBuffer = image.bitmap.data;
const len = srcBuffer.length;
const dstBuffer = Buffer.allocUnsafe(len);
let tmp;
if (steps === 2) {
// Upside-down
for (let srcOffset = 0; srcOffset < len; srcOffset += 4) {
tmp = srcBuffer.readUInt32BE(srcOffset, true);
dstBuffer.writeUInt32BE(tmp, len - srcOffset - 4, true);
}
} else {
// Clockwise or counter-clockwise rotation by 90 degree
rotate90degrees(image.bitmap, dstBuffer, steps === 1);
tmp = image.bitmap.width;
image.bitmap.width = image.bitmap.height;
image.bitmap.height = tmp;
}
image.bitmap.data = dstBuffer;
return image;
function rotate90degrees(bitmap, dstBuffer, clockwise) {
const dstOffsetStep = clockwise ? -4 : 4;
let dstOffset = clockwise ? dstBuffer.length - 4 : 0;
let tmp;
let x;
let y;
let srcOffset;
for (x = 0; x < bitmap.width; x++) {
for (y = bitmap.height - 1; y >= 0; y--) {
srcOffset = (bitmap.width * y + x) << 2;
tmp = bitmap.data.readUInt32BE(srcOffset, true);
dstBuffer.writeUInt32BE(tmp, dstOffset, true);
dstOffset += dstOffsetStep;
}
}
}
}; "in your code" //image.rotate(90);
image = require('./patch-jimp-rotate')(90, image); //this can be more sexy, you can declare it above @cladveloper (FYI) |
@mathias22osterhagen22 I am going through the same situation, but in my case it is when I crop the image with .crop (), did you find a solution for this problem? please let me know, thanks |
@Eriickson yes sure. I passed on this library instead: https://www.npmjs.com/package/sharp |
@jimp/plugin-rotate/dist/index.js:29-30 have "+1" on their resizing height/width that are causing this issue for the rotate
|
Thanks to @mathias22osterhagen22, works well on the degree which is multiple of 90. Here is the TypeScript code import type Jimp from 'jimp';
/**
* Util to handle Jimp rotate bug
* https://github.com/oliver-moran/jimp/issues/721#issuecomment-733804760
*/
export const simpleRotate = (deg: number /* TODO: Restrict only 0,90,180,270 */, image: Jimp) => {
// Restore simple rotate of jimp v0.5.6
// https://github.com/oliver-moran/jimp/issues/821
if (deg % 90 !== 0) {
return image;
}
let steps = (deg / 90) % 4;
if (steps < 0) steps += 4;
if (steps === 0) {
return image;
}
const srcBuffer = image.bitmap.data;
const len = srcBuffer.length;
const dstBuffer = Buffer.allocUnsafe(len);
let tmp;
if (steps === 2) {
// Upside-down
for (let srcOffset = 0; srcOffset < len; srcOffset += 4) {
tmp = srcBuffer.readUInt32BE(srcOffset);
dstBuffer.writeUInt32BE(tmp, len - srcOffset - 4);
}
} else {
// Clockwise or counter-clockwise rotation by 90 degree
rotate90degrees(image.bitmap, dstBuffer, steps === 1);
tmp = image.bitmap.width;
image.bitmap.width = image.bitmap.height;
image.bitmap.height = tmp;
}
image.bitmap.data = dstBuffer;
return image;
// eslint-disable-next-line @typescript-eslint/no-explicit-any
function rotate90degrees(bitmap: any, dstBuffer: Buffer, clockwise: boolean) {
const dstOffsetStep = clockwise ? 4 : -4;
let dstOffset = clockwise ? 0 : dstBuffer.length - 4;
let tmp;
let x;
let y;
let srcOffset;
for (x = 0; x < bitmap.width; x++) {
for (y = bitmap.height - 1; y >= 0; y--) {
srcOffset = (bitmap.width * y + x) << 2;
tmp = bitmap.data.readUInt32BE(srcOffset, true) as number;
dstBuffer.writeUInt32BE(tmp, dstOffset);
dstOffset += dstOffsetStep;
}
}
}
}; |
Is there any fix for this. I'm getting this when I rotate -90 |
Hey everyone, i had the same problem, i'dnt know if this are the correct solution but i solved it replacing the "modular if" with the original image sizes: REPLACE THIS FOR THIS in the file: node_modules@jimp\plugin-rotate\dist\index.js I hope this can help you |
Someone should really fork this lib and start merging all the PRs... |
hey @tonysamperi unfortunately it's just me manning the ship of here! other than a brief stint around 4 years ago I haven't been a big user of jimp myself so it fell by the wayside as I went through my career. I'm happy to facilitate contribution. If you have changes you want to make I'm all for getting them in (this is to anyone). Jimp is a community led effect and the community is me 😓 @aquilatrindade would you like to submit a pr to fix those lines? |
hi @hipstersmoothie actually as I said I have a few libraries of my own to maintain already! |
Good call 😉 |
Expected Behavior
When the image is rotated, the image is identical to the original
Current Behavior
A black border (1px) is added
Failure Information (for bugs)
Steps to Reproduce
(typescript)
IF YOUR ISSUE DEPENDS ON A PARTICULAR IMAGE BE SURE TO INCLUDE THIS AS WELL. WE CAN'T REPORDUCE IF WE DON'T HAVE YOUR IMAGE
Screenshots
Context
Failure Logs
The text was updated successfully, but these errors were encountered: