-
-
Notifications
You must be signed in to change notification settings - Fork 55
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
Crop Image to an aspect ratio (with transform option?) #271
Comments
A ha — I see what you mean. What about passing the dimensions to the Something like: await eleventyImage("./test/exif-sample-large.jpg", {
widths: [100, 300, 500],
transform: function(sharp, stats) {
sharp.resize({
width: stats.width,
height: stats.width,
fit: Sharp.fit.cover,
position: Sharp.strategy.entropy,
});
},
}); |
@zachleat ah, yes! I also had this idea (because it would enable a low effort quick fix), forgot to include it in the examples above 😅 But there would still be the problem mentioned above:
Maybe together with a new |
@jens-struct the current v6.0.0 has this code to regenerate file names (and metadata info) after the transform runs: Line 692 in d262378
|
I would also note this 6.0.0 code that uses the transform callback function name Line 145 in d262378
|
Oh, ok. I didn't saw this, thanks for the hint. Then simply adding |
@zachleat Would it be helpful, if i create a PR for this small change? |
If you’d like to contribute, I’d be happy to review and merge! |
@zachleat This issue is a followup of: https://indieweb.social/@djenz/113782569330355460
Lets say we want to crop the following js api usecase to an aspect ratio of 1:1.
We could resize
path/to/16x9.jpg
beforehand and provide the buffer as src for the image api:This will work. But it has the disadvantage that it can not fully take advantage of the js api transformation cache. It would resize the original image each time you call this code (i.e. through a shortcode). With a lot of images this alone can take a long time, even though the follow up transformations are cached.
Now there is the
transform
option, lets say we use it for illustration purpose in the same way:It wouldn't work, because it will ignore the
widths
property (size scale) now, as it will create 13 times an image with the same width/height -> cropWidth/cropHeight. As thetransform
option is run once per resize (per width of thewidths
size scale).We could split it out into a loop, i.e.:
This would only work if the
src
is used with one aspect ratio (cropping) throughout the whole codebase, as the output filename would be the same no matter the size differences introduced through thetransform
option (it is not taken into account by the 11ty img algorhythm that creates the filename hash). So different aspect ratio versions of the same src & width would overwrite each others out files.crop
optionTo omit all the extra custom code, i wish we would had something like this:
Which does all the above automatically:
transformOriginal
optionOr maybe a
transformOriginal
option that is only run once for the original image provided throughsrc
, and not on every resize of the widths scale like thetransform
option now.This might be better than the
crop
option, as it is more general and would enable more usecases than only the cropping. But it would also still require some custom code, which could maybe provided through an example in the 11ty docs.Other solutions?
But maybe there are other (better) ways, i haven't thought of? Ideas?
Thanks!
The text was updated successfully, but these errors were encountered: