Posts Tagged ‘development’

Crop and resize an image using MiniMagick

Wednesday, January 21st, 2009

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