2014-07-02 40 views
2

我在ImageMagik中使用回形針。我的問題是,如何才能使回形針裁剪圖像到一定的大小,只有當它們的比例小於X.回形針 - 按圖像比例進行條件裁剪

我在尋找的是裁剪所有圖像到一定的角度,除了高大的圖像,這我不想裁剪,只是縮放。

我目前的設置是:"X425"

我想有:"615X425#"非高大圖像和"X425"高層\寬影像。

謝謝!烏里

回答

2

條件格式

前陣子,我們想用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 

我把resize代碼this answer

+1

哇!看起來很棒!非常感謝 –