Override transparency color when converting transparent PNG to JPG

Posted by Alexander Malfait on Stack Overflow See other posts from Stack Overflow or by Alexander Malfait
Published on 2010-05-31T08:06:53Z Indexed on 2010/05/31 8:12 UTC
Read the original article Hit count: 302

Filed under:
|
|

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.

© Stack Overflow or respective owner

Related posts about ruby

Related posts about rmagick