Skip to content

A hook to access sharp instance for extra processing (crop, maintain EXIF, etc) #52

Closed

Description

@zachleat

Activity

  1. tusamni commented on May 28, 2021

    @tusamni

    I'd love to be able to have a hook to access EXIF data.

  2. pinned this issue on Jul 19, 2021
  3. colingreen commented on Jan 14, 2022

    @colingreen

    I'd like to see this too – I want to get hold of the image's dominant colour from the Sharp stats: https://sharp.pixelplumbing.com/api-input#stats

  4. added
    needs-votesA feature request on the backlog that needs upvotes or downvotes. Remove this label when resolved.
    on Feb 24, 2022
  5. zachleat commented on Feb 24, 2022

    @zachleat
    MemberAuthor

    This repository is now using lodash style issue management for enhancements. This means enhancement issues will now be closed instead of leaving them open.

    View the enhancement backlog here. Don’t forget to upvote the top comment with 👍!

  6. ScreenDream commented on Sep 27, 2023

    @ScreenDream

    I already commented on the other issue report, but want to repeat the request here:

    Yes, keeping image metadata like EXIF, XMP, and IPTC and also the ICC colour profile are critical for me as well.
    I'm a visual artist building my website with eleventy ATM and will be unable to use the image plugin at all if I can't keep metadata and ICC profiles - my images look washed out and the contact information on the images is missing after the processing.

    I would be super thankful if somebody could implement the hooks to activate/access the "withMetadata" functionality that is already in sharp: https://sharp.pixelplumbing.com/api-output#withmetadata
    I am sadly not up to that myself.

    Thanks a ton!

    Tom

  7. philiprenich commented on Oct 1, 2023

    @philiprenich

    Would it be bad to do something as naïve as adding a sharpHook option that takes a Sharp instance as a parameter and returns a Sharp instance. Then in img.js (maybe around line 500?) have

            if(typeof this.options.sharpHook === "function") {
              sharpInstance = this.options.sharpHook(sharpInstance);
            }

    It's not foolproof by any means. It would allow the dev to completely override what 11ty-img is doing: one could sharp.resize(100, null) and it would ignore any resizing you've asked the plugin to do while not reporting correctly. Perhaps running the hook before everything else would be better. Still, I feel like there's some more user friendly way of doing this since this is pretty heavy-handed and permissive.


    EDIT

    Putting that code a bit higher does feel better. After the sharp instance clone on line 481, but before the current resize/rotate code means that any resizing the user does in the hook is overwritten by the plugin options. While that means ignoring explicit calls to resize in the hook, it feels less confusing to respect the primary options of the plugin.

  8. ScreenDream commented on Oct 1, 2023

    @ScreenDream

    I can't comment on the "would it be bad" part ;-)
    But otherwise I think this could be interesting if nobody has a better and more "non-bad" solution while leaving the plugin intact otherwise.
    Like a kind of "Pro-mode".

    My current workaround is setting the default for "withMetadata" to "true" in the sharp/lib/constructor.js (line 261).
    Dirty hack, but does what I need for the time being.
    My website is almost ready now thanks to this.

    But yeah, like I wrote on the other thread, IMO this is mission critical for any application of eleventy where the image IS the product.

    Thanks to everybody looking into this!

  9. philiprenich commented on Oct 1, 2023

    @philiprenich

    @ScreenDream Feel free to use my fork - though I won't guarantee this will always be around, though for the moment I'll be using it until there's a solution in the main repo here (bit easier than re-writing my code to use Sharp directly).

    Sounds like you're doing everything at build time though and don't need access to a proper package so your solution may be stable enough.

    Here's how you'd use a GitHub url in your package.json

  10. hansspiess commented on Feb 11, 2024

    @hansspiess

    First of all, thanks for this great plugin - it is a really valuable tool when working with image content!

    As this PR is closed: is the Feature (a hook exposing sharpInstance) being rejected? I think it would be a handy addition to the plugin, keeping it's API concise while opening possibilities for users.

    Edit: ok now i get it, closed PR != rejected feature. However as i have to work with a fork of @11ty/eleventy-img: i'm looking forward for this to make it into main.

  11. smth commented on Jun 7, 2024

    @smth

    I was just wondering about this. I wrote up an experiment here where I used Sharp to essentially get the background colour of an image. I called Sharp directly to do this, which feels like a doubling of work (assuming the data I generated already exists somewhere).

  12. removed
    needs-votesA feature request on the backlog that needs upvotes or downvotes. Remove this label when resolved.
    on Dec 20, 2024
  13. zachleat commented on Dec 20, 2024

    @zachleat
    MemberAuthor

    Shipping with v6.0.0-beta.1

    await Image("./test/exif-sample-large.jpg", {
      // Keep exif metadata
      transform: (sharp) => {
        sharp.keepExif();
      }
    });

    This is a preprocessing step that runs before the Image runs orientation fix, resize, and format conversion (all of which still run afterwards)

  14. ScreenDream commented on Jan 2, 2025

    @ScreenDream

    Is 6.0.0-beta.1 available already?
    I only see beta and release versions up to 5.0.0 with
    npm show @11ty/eleventy-img versions

    And will that keep both Exif metadata and ICC profiles like "keepMetadata()" does with Sharp?
    https://sharp.pixelplumbing.com/api-output#keepmetadata

    We are working on a new website and my old hack no longer works...

  15. zachleat commented on Jan 2, 2025

    @zachleat
    MemberAuthor

    Not yet—finishing up some last issue triage before we release! Soon!

  16. ScreenDream commented on Jan 2, 2025

    @ScreenDream

    Awesome! Thank you! ❤️❤️❤️

  17. unpinned this issue on Jan 4, 2025
  18. ScreenDream commented on Jan 6, 2025

    @ScreenDream

    Ok, so I installed version 6.0.0-beta.2 today following your announcement on LinkedIn and while the code was accepted, I still did not have the full metadata in the output jpg, webp and avif files with the code you posted above, but when I replaced "keepExif()" with "keepMetadata()", it was all there.
    AWESOME :-)
    Thanks a ton!

    Out of curiosity: Is this "transform: (sharp) => {...}" code a way to access everything sharp offers or is it only meant to be used for the metadata?

    Cheers and a great 2025 to you!

    Tom

  19. zachleat commented on Jan 7, 2025

    @zachleat
    MemberAuthor

    @ScreenDream it allows the full power of anything exposed in the Sharp api!

  20. added a commit that references this issue on Jan 8, 2025
  21. added a commit that references this issue on Jan 16, 2026
    40702a5
  22. marked Add ability to crop images #31 as a duplicate of this issue on Apr 24, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions