lovell / sharp

High performance Node.js image processing, the fastest module to resize JPEG, PNG, WebP, AVIF and TIFF images. Uses the libvips library.
https://sharp.pixelplumbing.com
Apache License 2.0
28.98k stars 1.29k forks source link

Expose background option in constructor for PDF input #3321

Open omegayal opened 2 years ago

omegayal commented 2 years ago

Question about an existing feature

When install a custom libvips with PDF support, we can convert a PDF file to an PNG image with sharp. However, when the PDF has transparent background, the output image always shows white background.

What are you trying to achieve?

https://github.com/libvips/libvips/issues/995 It seems that libvips provides the option "background" when converting PDF to PNG with transparent background: vips copy transparent.pdf[background=0] x.png Does sharp also have such option? Thanks.

lovell commented 2 years ago

We'd need to expose the background option in the constructor API. Happy to accept a PR, if you're able.

https://sharp.pixelplumbing.com/api-constructor#parameters https://www.libvips.org/API/current/VipsForeignSave.html#vips-pdfload

calebmer commented 3 weeks ago

This is also something I'm interested in.

I explored writing a PR. Figured it would be easy since I could follow the code path to get the level constructor option to vips but after ~10min I got stuck on:

  1. According to the vips documentation background is a VipsArrayDouble (source). Couldn't find an example of how to construct a VipsArrayDouble in this repository. I did find the code for options.create.background (source) which I expect is in the neighborhood of what one would want here.
  2. It's unclear to me how to write a test for this given as a prerequisite you need a globally installed vips with PDFium (or Poppler).
lovell commented 3 weeks ago

@calebmer Great, thanks for taking a look at this. Perhaps you could copy the approach taken for properties such as resizeBackground that use the AttrAsVectorOfDouble helper? As you say, we won't be able to fully test this feature, but hopefully can at least have a couple of sanity checks e.g. valid vs invalid values.

calebmer commented 3 weeks ago

Ok, following the resizeBackground example I was able to attempt a fix: https://github.com/lovell/sharp/pull/4207