Created
August 7, 2010 15:49
-
-
Save ono/512912 to your computer and use it in GitHub Desktop.
This file contains 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' | |
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