Skip to main content
 首页 » 编程设计

ruby-on-rails中回形针中按图像比例进行条件裁剪

2025年01月19日15sky-heaven

我正在将回形针与 ImageMagik 一起使用。我的问题是,仅当回形针的比例小于 X 时,如何才能将回形针裁剪为特定尺寸。

我正在寻找的是将所有图像裁剪到一定尺寸,除了高图像,我不想裁剪,只需缩放。

我当前的设置是:“X425”

我想要:"615X425#" 用于非高图像,"X425" 用于高\宽图像。

谢谢!乌里

请您参考如下方法:

条件格式

不久前,我们想使用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 代码