8000 Add a hook to transform images? · Issue #6 · andrelandgraf/openimg · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Add a hook to transform images? #6

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 &ldqu 8000 o;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

Closed
davidesigner opened this issue Mar 16, 2025 · 2 comments
Closed

Add a hook to transform images? #6

davidesigner opened this issue Mar 16, 2025 · 2 comments

Comments

@davidesigner
Copy link
davidesigner commented Mar 16, 2025

It would be really nice to be able to transform the image.

My use case: add a colorize=white|black parameter.

Idea of the transform hook I would add:

import { getImgParams } from "openimg/node";

function loader({ request }) {
  const headers = new Headers();
  headers.set('Cache-Control', 'public, max-age=31536000, immutable');
  
  return getImgResponse(request, {
    headers,
    getImgParams: ({ request }) => {
      const defaultParams = getImgParams({ request });
  
      const colorize = url.searchParams.get('colorize') || undefined;
      if (colorize && !['white', 'black'].includes(colorize)) {
        return new Response(null, {
          status: 400,
          statusText: 'Search param "colorize" must be either "white" or "black" or unset',
        });
      }
  
      return {
        ...defaultParams,
        colorize,
      };
    },
    transform: async ({ params, pipeline }) => {
      const infoPromise = new Promise((resolve) => {
        pipeline.on('info', (info) => {
          resolve(info);
        });
      });
      const outputImgInfo = await infoPromise; // This could also be passed as function's parameter with `params, pipeline`.
  
      if (params.colorize) {
        if (!['png', 'svg'].includes(outputImgInfo.format)) {
          return new Response(null, {
            status: 400,
            statusText: 'Colorize is only supported for PNG and SVG images',
          });
        }
  
        if (outputImgInfo.format === 'svg') {
          // Convert SVG to PNG before colorizing
          pipeline.png();
        }
  
        switch (params.colorize) {
          case 'white':
            pipeline.grayscale().modulate({
              brightness: 100,
              saturation: 0,
            });
            break;
          case 'black':
            pipeline.grayscale().modulate({
              brightness: 0,
              saturation: 0,
            });
            break;
          default:
            return new Response(null, {
              status: 400,
              statusText: 'Unhandled colorize value',
            });
        }
      }
  
      return pipeline;
    },
  });
}
@andrelandgraf
Copy link
Owner

Thanks a ton for brining this up and opening a PR as well. I had to make a few changes for the custom cacheKey logic and just went ahead and did it myself (as discussed in your PR).

Transform hook now released as getSharpPipeline in v1.0.0. 🥳

@andrelandgraf
Copy link
Owner

Would love if you give it a spin, @davidesigner! I also exposed a new params property in the React component to make it easier to send custom search parameters as part of the image source to the optimizer endpoint.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants
0