sharp icon indicating copy to clipboard operation
sharp copied to clipboard

export your map of filetype outputs

Open toastal opened this issue 3 years ago • 4 comments

What are you trying to achieve? Allow consumers of sharp to use your supported types. Maybe this applies to supported inputs too.

Have you searched for similar feature requests? yes

What would you expect the API to look like? export formats

What alternatives have you considered? No.

Is there a sample image that helps explain? Here in Parcel's code they're copying your types, but like, they should be able to import this so they don't have to maintain parity https://github.com/parcel-bundler/parcel/blob/0934f9f/packages/transformers/image/src/ImageTransformer.js#L5

toastal avatar Mar 29 '21 12:03 toastal

Hi, it's possible to get close to this using the existing sharp.format runtime property, e.g. to filter for formats that support Stream-based output you might use:

Object.values(sharp.format)
  .filter(({ output }) => output.stream)
  .map(({ id } => id)
[ 'jpeg', 'png', 'webp', 'tiff', 'dz', 'heif', 'raw' ]

However this currently deals with containers rather than codecs, e.g the newer AVIF and HEIC codecs both use a heif container.

Perhaps this runtime property should additionally include a list of the possible codecs and file extensions for each format?

lovell avatar Mar 29 '21 13:03 lovell

And what limitations are there to exporting this Map so people can consume sharp in a wrapped sense using the exact inputs?

I'm not affiliated with that Parcel; I just find the need to copy part of your source for this library odd.

toastal avatar Mar 30 '21 04:03 toastal

Hi, i just landed here for the same reason.

Perhaps this runtime property should additionally include a list of the possible codecs and file extensions for each format?

that would be perfect.

kapouer avatar Nov 18 '21 21:11 kapouer

Commit https://github.com/lovell/sharp/commit/905518fab04f3466b2f1c99cd6705e065d28cebf exposes the suffs property of libvips' file loaders as fileSuffix e.g. sharp.format.jpeg.input.fileSuffix will be ['.jpg', '.jpeg', '.jpe'].

It also adds alias to the output e.g. sharp.format.jpeg.output.alias will be ['jpe', 'jpg'].

The values of alias are all valid for use with toFormat, for example you could calculate the list of valid runtime output formats that support streaming for a particular installation of libvips using the following code:

Object.values(sharp.format)
  .filter(f => f.output.stream) // only care about formats that support stream-based output
  .map(f => [f.id, f.output.alias].flat()) // concatenate format id and its aliases
  .flat()
  .filter(Boolean) // remove any undefined entries after flattening

The original request was to allow parcel to remove its own copy of an internal sharp data structure. Looking again at the parcel source, I think it could be simplified to call toFormat() directly, as it will already throw a suitable (and more detailed) error for an unknown format. This should remove the need to check/use FORMATS entirely.

-         imagePipeline[FORMATS.get(format)]({
+         imagePipeline.toFormat(format, {

lovell avatar Jul 11 '22 09:07 lovell

v0.31.0 now available with this improvement, thank you for the suggestion/feedback.

lovell avatar Sep 05 '22 09:09 lovell