Skip to content

Instantly share code, notes, and snippets.

@spllr
Created February 16, 2011 14:14
Show Gist options
  • Save spllr/829426 to your computer and use it in GitHub Desktop.
Save spllr/829426 to your computer and use it in GitHub Desktop.
Quick and dirty image resizer
require "rmagick"
require "fileutils"
[
{ :path => 'medium', :max_width => 125, :max_height => 450},
{ :path => 'normal', :max_width => 200, :max_height => 200},
{ :path => 'micro', :max_width => 65, :max_height => 65}
].each do |opts|
max_height = opts[:max_height]
max_width = opts[:max_width]
type = opts[:path]
image_base = File.expand_path(type, File.dirname(__FILE__))
new_images_base = File.expand_path(type, File.join(File.dirname(__FILE__), 'resized'))
FileUtils.mkdir_p(new_images_base)
Dir[image_base + '/*.gif'].each do |image_file|
image = ::Magick::Image.read(image_file).first
if image.rows > max_height
puts "will resize"
dst = Magick::Image.new(max_width, max_height) {
self.background_color = 'white'
}
resized_image = image.resize_to_fit!(max_width, max_height)
result = dst.composite(resized_image, Magick::CenterGravity, Magick::OverCompositeOp)
result.write(File.join(new_images_base, File.basename(image_file)))
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment