-
-
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
Add ability to crop images #31
Comments
@johnridesabike, I don’t have a solution per se, just a thought that I’m currently working through as I familiarize myself with this plugin too. I’ve got an dev site that I’m experimenting with passing URLs from Cloudinary as the |
I'd also love the ability to crop just some of the given widths, to change the aspect ratio for mobile, for example. |
@zachleat can we ask priority for this? E.g. think just a full screen hero background image optimization for mobiles, usually 800x1200, when we don't know the dimensions of the user selected picture (by some headless cms) but we need to resize it to be exactly 1200px height. I'm working on a tiny Webpack image loader based on Thank you! |
I wonder if we can use |
It seems to me that the However looking at the code of Maybe a new option and a little modification something like this could work?: let userSharpResizeOptions = this.options.sharpResizeOptions || {};
sharpInstance.resize({
resizeOptions,
...userSharpResizeOptions
}); ...then we would be able to use all the magic of https://sharp.pixelplumbing.com/api-resize#parameters |
Good eye! They are constructor options, I didn't notice that 😆 The name If someone does make a PR, please include the name in keysToKeep too |
Please consider this. Cropping images at build time seems so much better than just using CSS methods from a performance perspective. |
The code has changed a lot since then, so I wouldn't really expect it to get merged. If there is no PR by Jan 1st, I'll draft one. Although I wouldn't expect it to be merged and shipped any time soon from that. If you need the feature soon, I strongly recommend forking the repo and implementing |
Sure, I'm thinking of that, but also I'm absolutely certain that @zachleat could do this thing ways better than I could. I'm just a plain 11ty user and to be honest I would give my half hand for the JS skills that Zach has 😄 . Also as I read somewhere before, he said that issues with more likes will get more priority. So if you are reading this, please press the like icon on the first post 👍 if you need this feature. |
I believe this is the top feature request right now but it’s still going into the queue. Thanks folks!
|
hmmm wonder if this kinda dies or somebody have a good idea to crop images ? |
@mortendk I tend to use an external NPM script running in parallel to generate resized images. On bigger projects, I generally use external images services. |
@mortendk I figured sharp could be a good workaround, here is the Shortcode I added to get responsive picture generation in templating. const Image = require("@11ty/eleventy-img");
const Sharp = require('sharp');
module.exports = function(eleventyConfig) {
eleventyConfig.addShortcode("myImage", async function(src, alt, smallSize, midSize, bigSize) {
try {
console.log(`myImage shortcode called with src:`, src);
const resizedImageBuffer = await Promise.all([
resizeImage(src, smallSize[0], smallSize[1], "cover"),
resizeImage(src, midSize[0], midSize[1], "cover"),
resizeImage(src, bigSize[0], bigSize[1], "cover"),
]);
const metadata = await Promise.all(
resizedImageBuffer.map(async buffer => {
return await Image(buffer, {
formats: ["webp"],
outputDir: "./dist/assets/img/output",
urlPath: "/assets/img/output/",
});
})
);
console.log("Generated image metadata:", metadata);
let imageAttributes = {
alt,
loading: "lazy",
decoding: "async",
class: "o-fluidimage"
};
const imageHTML =
`<picture>
<source media="(min-width: 1050px)" srcset="${metadata[2].webp[0].url}">
<source media="(min-width: 750px)" srcset="${metadata[1].webp[0].url}">
<img
class="${ imageAttributes.class }"
alt="${ imageAttributes.alt }"
loading="${ imageAttributes.loading }
decoding="${ imageAttributes.decoding }"
src="${ metadata[0].webp[0].url }"
/>
</picture>`;
console.log(`Generated image HTML:`, imageHTML);
return imageHTML;
}
catch (error) {
console.error(`Error in myImage shortcode:`, error);
return "";
}
});
}
async function resizeImage(src, width, height, mode) {
return await Sharp(src)
.resize({ width: width, height: height, fit: mode })
.toBuffer();
} |
I just started using this plugin, and this is the main feature I’m missing the most.
Its use case is when you need your images to conform to specific aspect ratios, such as square thumbnails.
(Sure, you can accomplish this with CSS too, but I’d prefer cropping the images during the build process.)
Since the
widths
option is already an array, perhaps there could be a singleaspectRatio
option too? e.g.[1, 1]
or[16, 9]
.Bonus points if you could also pass all of the sharp resize options too, e.g.
position: "center"
https://sharp.pixelplumbing.com/api-resizeThe text was updated successfully, but these errors were encountered: