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

Include trimOffsetRight & trimOffsetBottom #4086

Open
Zirafnik opened this issue Apr 26, 2024 · 1 comment
Open

Include trimOffsetRight & trimOffsetBottom #4086

Zirafnik opened this issue Apr 26, 2024 · 1 comment
Labels

Comments

@Zirafnik
Copy link

Feature request

After trimming an image with .trim() you receive an info object, which includes the trimOffsetLeft and trimOffsetTop properties.

However, a picture can be asymmetric, meaning that .trim() will remove differing amounts of border on each side => trimOffsetLeft !== trimOffsetRight.

The current workaround is to calculate the right and bottom offset by hand, with the use of pre-processing metadata and properties included in the resulting info object,

This is the formula used:

Other Offset = Original Length - New Length - |Opposite Offset|

const image = sharp(inputFile);

const data = await image.metadata();
const buffer1 = await image
    .trim()
    .toBuffer({ resolveWithObject: true }); // we get new data after .trim()

const info = buffer1.info;

const trimOffset = {
    left: Math.abs(info.trimOffsetLeft),
    right: data.width - info.width - Math.abs(info.trimOffsetLeft),
    top: Math.abs(info.trimOffsetTop),
    bottom: data.height - info.height - Math.abs(info.trimOffsetTop),
};

Additionally, perhaps the original pre-processing values, such as original width & original height could be included in the resulting info object.

@lovell
Copy link
Owner

lovell commented Apr 26, 2024

Your approach to calculate these values looks correct and matches the advice previously given at #3696 (comment)

I'm unsure if this logic needs adding to and therefore fall under the maintenance burden of sharp.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants