Skip to content

Fixes #271#274

Merged
zachleat merged 1 commit into
11ty:mainfrom
jens-struct:transform-with-stats
Jul 24, 2026
Merged

Fixes #271#274
zachleat merged 1 commit into
11ty:mainfrom
jens-struct:transform-with-stats

Conversation

@jens-struct

@jens-struct jens-struct commented Jan 17, 2025

Copy link
Copy Markdown
Contributor

@zachleat This is the PR for #271, it optimizes the js api for cropping.

I left some comments in the code to start some discussions.

optimize js api for cropping
Comment thread src/image.js
"sharpJpegOptions",
"sharpAvifOptions"
"sharpAvifOptions",
"cacheKey"

@jens-struct jens-struct Jan 17, 2025

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of this we could duplicate the logic of https://github.com/11ty/eleventy-img/blob/main/src/image.js#L142 here, but this would mean a not pretty workaround like:

    transform: {[cacheKey]: function(sharp, stats) {
      sharp.resize({
        width: stats.width,
        height: Math.floor(stats.width / cropRatio),
        fit: Sharp.fit.cover,
        position: Sharp.strategy.entropy,
      });
    }}[cacheKey],

where:

  cacheKey: 16 / 9

is way cleaner if you ask me 😅 So i arrived at the solution i pushed here, to be able to influence the hash part of the filename if needed. Please see the sample-crop.js for the usecase.

Also the naming of this option could maybe be improved, i.e. hashKey?

Comment thread sample/sample-crop.js
const src = "../test/bio-2017.jpg";
const cropRatio = 16 / 7;

async function sanitizeWidths(widths) {

@jens-struct jens-struct Jan 17, 2025

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This removes all widths that would cause upscaling of the original image.

I could probably go a step further and try to build this kind of sanitization into 11ty image.

Comment thread sample/sample-crop.js
return sanitizedWidths;
}

function getMaxCropWidth(originalWidth, originalHeight) {

@jens-struct jens-struct Jan 17, 2025

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This gets the max width for cropping without upscaling the original image.

I guess this is also a nice thing to have in the core? Although an opt-in through an option is probably a good idea, as it is likely not the wanted behavior for all usecases.

@jens-struct
jens-struct marked this pull request as ready for review January 17, 2025 18:05
Comment thread src/image.js
}

await transform(sharpInstance);
await transform(sharpInstance, { ...stat });

@jens-struct jens-struct Jan 17, 2025

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Probably better to pass a clone instead of a reference here?

@zachleat
zachleat merged commit fc1cfb7 into 11ty:main Jul 24, 2026
@zachleat

Copy link
Copy Markdown
Member

Shipping as manualCacheKey and a limited set of width, height, format passed into the transform callback in Image v7.0.0-alpha.5.

Thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

2 participants