sharp
sharp copied to clipboard
export your map of filetype outputs
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
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?
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.
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.
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, {
v0.31.0 now available with this improvement, thank you for the suggestion/feedback.