11ty / eleventy-img

Utility to perform build-time image transformations.

Home Page:https://www.11ty.dev/docs/plugins/image/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Implement option to switch set of formats on transparency

Snapstromegon opened this issue · comments

I want to build an image shortcode that does not output jpgs when the input image is/(might be) using transparency and instead use pngs.
Since the documentation points out that relying on the file extension isn't correct, I'd love to use the metadata sharp exposes to make this decision.

Right now I use it something like this:

Image(src, {
  widths: [256, 512, 1024, null],
  formats: [
    "avif",
    "webp",
    src.toLowerCase().endsWith(".png") ? "png" : "jpeg",
  ],
  outputDir: "_site/img/",
});

I don't know what the most elegant way of doing this would be, but I think such a feature would help more people than jsut me.