【问题标题】:Paperclip - Conditional cropping by image ratio回形针 - 按图像比例进行条件裁剪
【发布时间】:2014-07-02 07:27:38
【问题描述】:
我正在使用带有 ImageMagik 的回形针。我的问题是,只有当它们的比例小于 X 时,我才能将回形针裁剪图像制作成一定的尺寸。
我正在寻找的是将所有图像裁剪到一定的尺寸,除了高大的图像,我不想裁剪,只需缩放。
我目前的设置是:"X425"
我想要:"615X425#" 用于非高图像,"X425" 用于高\宽图像。
谢谢!乌里
【问题讨论】:
标签:
ruby-on-rails
image
paperclip
crop
【解决方案1】:
条件格式
不久前,我们想使用conditional styling in Paperclip,然后想出了this 和since found this:
#app/models/attachment.rb
Class Attachment < ActiveRecord::Base
has_attached_file :image,
styles: Proc.new { |instance| instance.resize }
private
def resize
geo = Paperclip::Geometry.from_file(photo.to_file(:original))
ratio = geo.width/geo.height
min_width = 142
min_height = 119
if ratio > 1
# Horizontal Image
final_height = min_height
final_width = final_height * ratio
"#{final_width.round}x#{final_height.round}!"
else
# Vertical Image
final_width = min_width
final_height = final_width * ratio
"#{final_height.round}x#{final_width.round}!"
end
end
end
我从this answer获取了resize代码