In a NodeJS application, users are allowed to upload images with jpeg, jpg and png file extensions. When saving original and resized images, all the images are saved with jpg extension instead of the original file extensions.
An image with the file extension png, when uploaded before resizing, displays as original even when saved with jpg file extension. But when the same image is resized and saved in jpg format, the transparent background in the png image is converted to a dark background.
Is there a way to remove this dark background in the resize and save step for png file extensions?
Thanks @Adetoyinbo1 for an example of the nodejs sharp module. I compressed a few images with png file extension to JPEG output format. The images, that have a white background, get converted to JPEG format with a white background.
If png images have a transparent background, then compression process adds a dark background, regardless of the output file extension.
Just tried Webp and it retains the original background. But this option is not available in Wappler’s in-built Resize step.
I am saving images with jpeg, jpg and png file extensions, both original and resized, in jpg output format. There’s no user input for format selection when uploading these images.
A jpeg file doesn't support transparency, default it gets flattened with the black color. I can make it possible to allow you setting a different color instead of black for the transparent part.
Here an update, I've added background as an option to the save step. The background is only used when saving to jpeg and defaults to black. It will do nothing when the input image doesn't have transparency.
Hi @patrick, thanks for adding an option to set a background colour in the Save Image Step.
I replaced the existing file in /lib/modules/ directory with the updated image.js file. But it’s not showing the new option to set background colour when JPEG format is selected.
I have restarted Wappler and Docker services a few times, but the updated option in Save Image step is still not displaying.
Hi @patrick, the updated image.js file is not showing background option in the Save Image step. Could you please guide me if I need to make any other changes for the new option to display? I’m using Docker on the local machine.
Perfect. I have tested by editing the json directly and background colour is updating correctly for transparent backgrounds according to the hex value given.
Many thanks, @patrick, your help is greatly appreciated.