Skip to content

Instantly share code, notes, and snippets.

@kennx
Created September 20, 2012 08:03
Show Gist options
  • Save kennx/3754570 to your computer and use it in GitHub Desktop.
Save kennx/3754570 to your computer and use it in GitHub Desktop.
mini_magick 裁切正方形图片
# mini_magick 裁切正方形图片 sinatra
def crop_thumb_image(file)
begin
image = MiniMagick::Image.new(file)
image_width,image_height = image[:width],image[:height]
if image_width > image_height
shaved = (image_width-image_height).abs / 2
image.shave("#{shaved}x0")
else
shaved = (image_height-image_width).abs / 2
image.shave("0x#{shaved}")
end
rescue MiniMagick::Error
flash[:errors] = "我的水平不高,这张图片可能我没办法处理,请换一张"
redirect back
rescue MiniMagick::Invalid
flash[:errors] = "你很坏,你上传的文件很可能不是一张图片!"
redirect back
else
image.resize("200x200")
image.path
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment