Skip to content
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 “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

export your map of filetype outputs #2642

Closed
toastal opened this issue Mar 29, 2021 · 5 comments
Closed

export your map of filetype outputs #2642

toastal opened this issue Mar 29, 2021 · 5 comments

Comments

@toastal
Copy link

toastal commented Mar 29, 2021

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

@lovell
Copy link
Owner

lovell commented Mar 29, 2021

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?

@toastal
Copy link
Author

toastal commented Mar 30, 2021

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.

@kapouer
Copy link
Contributor

kapouer commented Nov 18, 2021

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.

@lovell
Copy link
Owner

lovell commented Jul 11, 2022

Commit 905518f 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 lovell added this to the v0.31.0 milestone Jul 11, 2022
@lovell
Copy link
Owner

lovell commented Sep 5, 2022

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

@lovell lovell closed this as completed Sep 5, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants