I use Rmagic for resizing images, and i want a transparant background,
now I use a white background, but that is to beautiful on an gradiant
background :s:s
I have this code:
if !File.file?(file)
width = pic.columns
height = pic.rows
if (width < desired_width && height < desired_height) #no scale needed
else
if (width > height)
pic.scale!((desired_width.to_f/width.to_f))
else
pic.scale!((desired_height.to_f/height.to_f))
end
end
back = Magick::Image.new(desired_width.to_i,desired_height.to_i) {
self.background_color = ‘white’ #need to be transparant
self.format = ‘JPG’ #perhaps gif or png then
}
if (width < desired_width && height < desired_height) #no scale needed, center in white space
x = (desired_width - width) / 2
y = (desired_height - height) / 2
back.composite!(pic, Magick::CenterGravity, x, y,
Magick::InCompositeOp)
else
back.composite!(pic, Magick::CenterGravity, Magick::InCompositeOp)
end
File.open(file, “wb”) do |f|
f.write(back.to_blob)
end
end
I use Rmagic for resizing images, and i want a transparant background,
now I use a white background, but that is to beautiful on an gradiant
background :s:s
I have this code:
if !File.file?(file)
width = pic.columns
height = pic.rows
if (width < desired_width && height < desired_height) #no scale needed
else
if (width > height)
pic.scale!((desired_width.to_f/width.to_f))
else
pic.scale!((desired_height.to_f/height.to_f))
end
end
back = Magick::Image.new(desired_width.to_i,desired_height.to_i) {
self.background_color = ‘white’ #need to be transparant
self.format = ‘JPG’ #perhaps gif or png then
}
if (width < desired_width && height < desired_height) #no scale needed, center in white space
x = (desired_width - width) / 2
y = (desired_height - height) / 2
back.composite!(pic, Magick::CenterGravity, x, y,
Magick::InCompositeOp)
else
back.composite!(pic, Magick::CenterGravity, Magick::InCompositeOp)
end
File.open(file, “wb”) do |f|
f.write(back.to_blob)
end
end
Hmmm…well, the JPEG format doesn’t support transparency. You’ll have
to use GIF or PNG.
The following script composites a 240x320 JPG onto a 400x400 transparent
background and writes the resulting image in PNG format. I’m using
RMagick 1.14.1 and ImageMagick 6.2.9.
If you have any other questions about RMagick please email me at rmagick
AT rubyforge DOT org, or post in the RMagick help forum on RubyForge
(http://rubyforge.org/forum/forum.php?forum_id=33). I don’t read this
list very often but I get a copy of every posting to the forum. Good
luck!