Created
September 20, 2012 08:03
-
-
Save kennx/3754570 to your computer and use it in GitHub Desktop.
mini_magick 裁切正方形图片
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
# 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