Created
February 16, 2011 14:14
-
-
Save spllr/829426 to your computer and use it in GitHub Desktop.
Quick and dirty image resizer
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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