Skip to content

Instantly share code, notes, and snippets.

@ono
Created August 7, 2010 15:49
Show Gist options
  • Save ono/512912 to your computer and use it in GitHub Desktop.
Save ono/512912 to your computer and use it in GitHub Desktop.
require 'rmagick'
dir = File.dirname(__FILE__)
Dir.glob "#{dir}/**/*.jpg" do |filename|
image = Magick::Image.read(filename)[0]
# note: there is another useful method which you can use to resize and crop square
# image.resize_to_fill(200)
# You can set size here. The original resolution should be kept.
[[720,540,"large"],[240,180,"thumbnail"]].each do |size|
w_scale = image.columns / (1.0 * size[0])
h_scale = image.rows / (1.0 * size[1])
scale = w_scale >= h_scale ? w_scale : h_scale
scale = 1.0 / scale
small_image = image.resize(scale)
small_image.write File.dirname(filename) + "/" + File.basename(filename, ".jpg") + "_#{size[2]}.jpg"
small_image.destroy!
end
image.destroy!
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment