2013-07-03 4 views
3

이미지가 가로인지 세로인지에 따라 이미지를 다르게 처리하고 싶습니다.MiniMagick으로 방향을 기준으로 이미지 크기 조정

def is_landscape? 
    if @file 
     image = ::MiniMagick::Image.open(file) 
     Rails.logger.info "from in is_landscape? : #{image[:width] > image[:height]}" 
     image[:width] >= image[:height] 
    end 
    end 

    def is_portrait? 
    Rails.logger.info "from in is_portrait? : #{image[:height] > image[:width]}" 
    image[:height] > image[:width] 
    end 

    process :resize_to_fill => [667, 500], if: :is_landscape? 

    process :resize_to_fill => [500, 667], if: :is_portrait? 

    version :preview do 
    process :resize_to_fill => [380,285] 
    end 

    version :thumb do 
     process :resize_to_fill => [105,79], if: :is_landscape? 
     process :resize_to_fill => [105, 158], if: :is_portrait? 
    end 

나는 오류 내가 잘못 뭐하는 거지

"ArgumentError (wrong number of arguments (1 for 0)): app/uploaders/image_path_uploader.rb:31:in `is_landscape?'" 

받고 있어요 :

이 내 이미지 업 로더 모델에서 코드?

답변

7

새 파일을 is_landscape에 전달해야 했습니까? 와 is_portrait? 그것을 작동시키는 방법 :

def is_landscape?(new_file) 
     image = ::MiniMagick::Image::read(File.binread(@file.file)) 
     Rails.logger.info "from in is_landscape? : #{image[:width] > image[:height]}" 
     image[:width] >= image[:height] 
    end 

    def is_portrait?(new_file) 
    Rails.logger.info "from in is_portrait? : #{ !is_landscape?(new_file)}" 
    !is_landscape?(new_file) 
    end 

    process :resize_to_fill => [667, 500], if: :is_landscape? 

    process :resize_to_fill => [500, 667], if: :is_portrait? 

    version :preview do 
    process :resize_to_fill => [380,285] 
    end 

    version :thumb do 
     process :resize_to_fill => [105,79], if: :is_landscape? 
     process :resize_to_fill => [105, 158], if: :is_portrait? 
    end