Skip to content

Instantly share code, notes, and snippets.

@alexdean
Last active January 2, 2016 17:29
Show Gist options
  • Save alexdean/8336781 to your computer and use it in GitHub Desktop.
Save alexdean/8336781 to your computer and use it in GitHub Desktop.
resizing an image with rmagick
require 'RMagick'
# http://www.imagemagick.org/RMagick/doc/comtasks.html#thumb
original_filename = 'image.jpg'
thumb_filename = 'thumb.jpg'
orig = Magick::Image.read(original_filename).first
rows, cols = [150, 150]
thumb = orig
# don't do a resize if original is already smaller than desired thumbnail size
if orig.rows > rows || orig.columns > cols
thumb = orig.resize_to_fill(rows, cols)
end
thumb.write(thumb_filename) # {self.quality = 80}
thumb.destroy! if thumb != orig
orig.destroy!
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment