2015-02-06 4 views
0

나는 Rails 4.2와 Mongoid가있는 웹 사이트를 만들고있다. mongoid-paperclip을 사용하고 있는데, 짧은면의 치수를 유지하면서 사각형에 이미지를 자르려고합니다 (이미지가 전체 사각형을 채울 것입니다). 여기 내 사용자 지정 프로세서의 :클립으로 이미지를 수동으로 자르는 방법은 무엇입니까?

module Paperclip 
    class Cropper < Thumbnail 
    def initialize(file, options = {}, attachment = nil) 
     super 
     @preserved_size = [@current_geometry.width, @current_geometry.height].min 
     @current_geometry.width = @preserved_size 
     @current_geometry.height = @preserved_size 
    end 

    def target 
     @attachment.instance 
    end 

    def transformation_command 
     if crop_command 
     crop_command + super.join(' ').sub(/ -crop \S+/, '').split(' ') 
     else 
     super 
     end 
    end 

    def crop_command 
     ["-crop", "#{@preserved_size}x#{@preserved_size}+#{@preserved_size}+#{@preserved_size}"] 
    end 
    end 
end 

그리고 그것은 외모에 붙어 모델이 라인을 가지고 :

has_mongoid_attached_file :image, styles: {square: {processors: [:cropper]}}

을하지만 작동하지 않습니다. 'square'라는 이미지 버전이 저장되지만 원래 이미지와 동일합니다. 어떻게 작동시킬 수 있습니까?

답변

2

클립 클립 프로세서를 사용하지 않고도이 문제를 해결할 수있었습니다. 치수의 끝에 #이 자른 이미지는 항상 지정된 차원보다는 이미지를 축소하는 것을 보장하는 것이

has_mongoid_attached_file :image, styles: lambda {|a| 
              tmp = a.queued_for_write[:original] 
              return {} if tmp.nil? 
              geometry = Paperclip::Geometry.from_file(tmp) 
              preserved_size = [geometry.width.to_i, geometry.height.to_i].min 
              {square: "#{preserved_size}x#{preserved_size}#"} 
              } 

참고 : 내 모델에서, 나는 람다를 사용하여 이미지의 styles 지정 .