Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Override transparency color when converting transparent PNG to JPG

I'm using Dragonfly to generate thumbnail images in a Rails app.

I'm serving all picture images as JPG's. Now the client is uploading transparent PNG files, like this one:

http://www.ibanez.co.jp/products/images/eg2010/ART120_TRF_12_02.png

Dragonfly uses RMagick to convert these images to JPG. The problem is that it converts the PNG images to JPG with a black background, and my site's design requires a white background. I've tried to override it like this:

encoded_image = Magick::Image.from_blob(image.data).first

if encoded_image.format.downcase == format
  image # do nothing
else
  encoded_image.format = format
  encoded_image.background_color = "white"
  encoded_image.transparent_color = "white"
  encoded_image.to_blob
end

But the produced JPG images still contain a black background. Does anyone know how to beat RMagick into using a white background when converting the transparent layer?

I know I could just serve as PNG, but then the images are 10 times as large, and the site is already pretty bandwidth heavy.

like image 269
Alexander Malfait Avatar asked Dec 28 '22 15:12

Alexander Malfait


1 Answers

You can create an ImageList to be able to put a white image with the same size as your original image UNDER the transparent picture. If you flatten the ImageList down to an image, you get an image with the transparent color replaced by whatever the second image contained.

img_list = Magick::ImageList.new
img_list.read("my_png_file.png")
img_list.new_image(img_list.first.columns, img_list.first.rows) { self.background_color = "white" } # Create new "layer" with white background and size of original image
image = img_list.reverse.flatten_images

This works for me but could be optimized further, i guess.

Hope that helps! Hendrik

like image 173
Hendrik Söbbing Avatar answered Apr 13 '23 00:04

Hendrik Söbbing