swissspidy / media-experiments

WordPress media experiments
GNU General Public License v2.0
84 stars 1 forks source link

Browser-generated PNGs wrongly have alpha channel #492

Open swissspidy opened 4 months ago

swissspidy commented 4 months ago

Extracting this from #491.

If not explicitly setting canvas.getContext( '2d', { alpha: false }, the resulting image after canvas-based resizing and compression will have an alpha channel.

Docs: https://developer.mozilla.org/en-US/docs/Web/API/OffscreenCanvas/getContext#alpha

This leads to incorrect has_transparency values as even vips then thinks the image has alpha channels.

Right now I'm doing alpha: [ 'image/png', 'image/webp' ].includes( sourceType ),, which helps for JPEGs, but obviously not PNGs or WebP image.

I feel like I have to use vips to check transparency already very early, and then pass on the alpha information to the canvas worker.