【问题标题】:Resizing Images based on orientation with MiniMagick使用 MiniMagick 根据方向调整图像大小
【发布时间】:2013-07-03 15:57:53
【问题描述】:

我想根据图片是横向还是纵向来不同地处理图片。

这是我的图片上传模型中的代码:

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?'"

我做错了什么?

【问题讨论】:

    标签: ruby-on-rails carrierwave minimagick


    【解决方案1】:

    我必须将新文件传递给 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
    

    【讨论】:

      猜你喜欢
      • 2019-01-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-08-28
      • 2018-01-25
      相关资源
      最近更新 更多