Never been to DZone Snippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

Thumbnailer in Ruby and RMagick (See related posts)

require 'RMagick'

maxwidth = 120
maxheight = 160
aspectratio = maxwidth.to_f / maxheight.to_f
imgfile = 'world'

pic = Magick::Image.read(imgfile + '.jpg').first
imgwidth = pic.columns
imgheight = pic.rows
imgratio = imgwidth.to_f / imgheight.to_f
imgratio > aspectratio ? scaleratio = maxwidth.to_f / imgwidth : scaleratio = maxheight.to_f / imgheight
thumb = pic.resize(scaleratio)

white_bg = Magick::Image.new(maxwidth, thumb.height)
pic = white_bg.composite(thumb, Magick::CenterGravity, Magick::OverCompositeOp)
pic.write(imgfile + '.thumb.jpg')

Comments on this post

Yonk posts on Dec 22, 2005 at 22:29
i'm pretty new to ruby etc ... but i guess the line

white_bg = Magick::Image.new(maxwidth, thumb.height)

should be

white_bg = Magick::Image.new(maxwidth, thumb.rows)

kanatu posts on Jul 02, 2006 at 05:48
according to the rmagick documentation, "The thumbnail method is a fast resizing method suitable for use when the size of the resulting image is < 10% of the original."

it should fit right in where you have
thumb = pic.resize(scaleratio)

You need to create an account or log in to post comments to this site.


Click here to browse all 4858 code snippets

Related Posts