-
-
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
Fixes #271 #274
base: main
Are you sure you want to change the base?
Fixes #271 #274
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
const eleventyImage = require("../"); | ||
const Sharp = require("sharp"); | ||
|
||
const src = "../test/bio-2017.jpg"; | ||
const cropRatio = 16 / 7; | ||
|
||
async function sanitizeWidths(widths) { | ||
const { | ||
width: originalWidth, | ||
height: originalHeight, | ||
} = await Sharp(src).metadata(); | ||
|
||
const sanitizedWidths = widths.filter(width => { | ||
const height = Math.floor(width / cropRatio); | ||
return width <= originalWidth && height <= originalHeight; | ||
}); | ||
|
||
if (sanitizedWidths.length < widths.length) { | ||
sanitizedWidths.push(getMaxCropWidth(originalWidth, originalHeight)); | ||
} | ||
|
||
return sanitizedWidths; | ||
} | ||
|
||
function getMaxCropWidth(originalWidth, originalHeight) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. |
||
const cropWidth = originalWidth; | ||
const cropHeight = Math.floor(originalWidth / cropRatio); | ||
|
||
if (cropHeight > originalHeight) { | ||
return getMaxCropWidth(originalWidth - 1, originalHeight); | ||
} | ||
|
||
return cropWidth; | ||
} | ||
|
||
(async () => { | ||
|
||
const widths = await sanitizeWidths([100, 300, 500, 1600, 3200]); | ||
|
||
console.log(widths); | ||
|
||
const results = await eleventyImage(src, { | ||
widths, | ||
formats: ["webp"], | ||
dryRun: true, | ||
transform: function(sharp, stats) { | ||
sharp.resize({ | ||
width: stats.width, | ||
height: Math.floor(stats.width / cropRatio), | ||
fit: Sharp.fit.cover, | ||
position: Sharp.strategy.entropy, | ||
}); | ||
}, | ||
cacheKey: cropRatio, | ||
}); | ||
|
||
console.log(results); | ||
|
||
})(); |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -415,7 +415,8 @@ class Image { | |
"sharpWebpOptions", | ||
"sharpPngOptions", | ||
"sharpJpegOptions", | ||
"sharpAvifOptions" | ||
"sharpAvifOptions", | ||
"cacheKey" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 Also the naming of this option could maybe be improved, i.e. |
||
].sort(); | ||
|
||
let hashObject = {}; | ||
|
@@ -681,7 +682,7 @@ class Image { | |
throw new Error("Expected `function` type in `transform` option. Received: " + transform); | ||
} | ||
|
||
await transform(sharpInstance); | ||
await transform(sharpInstance, { ...stat }); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Probably better to pass a clone instead of a reference here? |
||
|
||
// Resized in a transform (maybe for a crop) | ||
let dims = Image.getDimensionsFromSharp(sharpInstance, stat); | ||
|
There was a problem hiding this comment.
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.