From: Chris Pine Date: 2004-12-21T21:33:15+09:00 Subject: Re: Programatically resizing photos Hey, Phil, I use RMagick and have been very happy with it. Here's a snippet from the program that generates our family picture website. The "640" code just scales the picture to make sure it fits inside a 640x640 box (so one dimension is == 640 and the other is <= 640), and the "thumb" code makes sure the new image is 160x120, filling with white any area not covered by a photo of a different aspect ratio. (Since there are many thumbnails on a page, I wanted them to be all the same size.) You're welcome to check out our site (http://pine.fm), and tell me if you want any more of the code. ----------- def makeImage (fileName, size) if !FileTest.exist?('pictures/'+fileName) # TODO: Error report? return 'Cannot find picture "'+fileName+'".' end altText = if PICTURES[fileName] PICTURES[fileName][:desc] else 'This picture needs info.' end case size when 640 # # Make 640x640 image # smallFileName = 'pictures_640/640__' + fileName if !FileTest.exist?(smallFileName) image = Magick::ImageList.new('pictures/'+fileName) image = image.change_geometry('640x640') do |cols, rows, img| img.resize!(cols, rows) end image.write(smallFileName) { self.quality = 85 } end #img(:src=>smallFileName) '' when :thumb # # Make thumbnail image # smallFileName = 'pictures_thumbs/thumb__' + fileName if !FileTest.exist?(smallFileName) image = Magick::ImageList.new('pictures/'+fileName) thumb = nil image.change_geometry('160x120') do |cols, rows, img| thumb = img.resize(cols, rows) end white_bg = Magick::Image.new(160, 120) image = white_bg.composite(thumb, Magick::CenterGravity, Magick::OverCompositeOp) image.write(smallFileName) { self.quality = 85 } end #img(:src=>smallFileName) '' else raise 'In "makeImage()"... Bad Size: '+size.to_s end end