I am using the ImageProcessing gem in combination with MiniMagick and Shrine. When I use the method resize_to_limit!/2
the resulting derivative has a black background when the original image has a transparent ground. How can I remedy this? I would prefer to maintain the alpha but I can live with a white background instead of black.
Attacher.derivatives do |original|
magick = ImageProcessing::MiniMagick.source(original)
{ resized: magick.resize_to_limit!(215, 50) }
end
It appears that MiniMagick will convert the file to JPG if the original file has no extension (see: Method documentation and method implementation)
You might be tempted to say that your file has an extension - well it has, but during direct upload to S3 (which I assume you might be doing with Shrine as you're facing this particular issue) the file gets saved without the extension in the filename, and the information about the extension gets preserved in the file metadata instead.
How to fix it, so that original PNG file will remain PNG with the transparent background? You can either fetch the metadata and convert the file to its original extension, or convert to
''
so that it does not trigger the default conversion to JPGcode snippets:
or as I mentioned, you can play around and try to fetch the extension from the metadata: