Crop and resize an image using MiniMagick

During my dealings with twitter avatars (see here) I discovered how to resize and crop an image using MiniMagick. This is useful when dealing with twitter avatars as the large size ones that the twitter api provides are not always square and the smaller ones may be too small for some applications. Below is the simple function :

def resize_and_crop(image, size)         
  if image.width < image.height   
    remove = ((image.height - image.width)/2).round 
    image.shave("0x#{remove}") 
  elsif image.width > image.height 
    remove = ((image.width - image.height)/2).round
    image.shave("#{remove}x0")
  end
  image.resize("#{size}x#{size}")
  return image
end
  1. Thank you for this. I found it quite helpful.

Leave a Comment

NOTE - You can use these HTML tags and attributes:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="" highlight="">

Trackbacks and Pingbacks: