Minimagick undefined方法`width ='

时间:2016-01-06 10:07:11

标签: amazon-s3 carrierwave minimagick

我想按照宽度和高度使用特定版本的图像。所以我跟着How-to:-Get-image-dimensions这个wiki,但得到了undefined method "width="。 我的 上传器 看起来像,

class S3uploaderUploader < CarrierWave::Uploader::Base

  include CarrierWave::MiniMagick
  # Include RMagick or MiniMagick support:
  # include CarrierWave::RMagick
  # include CarrierWave::MiniMagick
  process :store_dimensions


  # Choose what kind of storage to use for this uploader:
  # storage :file
  storage :fog

  def store_dir
    "uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}"
  end


  version :detail do
    process :quality => 90
    process :store_dimensions
  end


  version :mainVertical do
    process :quality => 80
    process :store_dimensions
    process :resize_to_fit => [240, 180]
  end

  version :mainHorizontal do
    process :quality => 80
    process :store_dimensions
    process :resize_to_fit => [240, 320]
  end

  private

  def store_dimensions
    if file && model
      model.width, model.height = ::MiniMagick::Image.open(file.file)[:dimensions]
    end
  end
end

我错过了什么?有什么建议?感谢。

1 个答案:

答案 0 :(得分:1)

我从这篇文章中得到了一个想法。 conditional-image-resizing-with-carrierwave。效果很好。

我改变了我的代码。

class S3uploaderUploader < CarrierWave::Uploader::Base

  include CarrierWave::MiniMagick

  storage :fog

  def store_dir
    "uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}"
  end

    version :detail do
      process :quality => 90
    end
    version :main, :if => :image?  do
        process :quality => 90
        process :resize_to_fit => [240, 180] ,:if => :horizontal?
        process :resize_to_fit => [240, 320]  ,:if => :vertical?
    end

  def image?(new_file)
      self.file.content_type.include? 'image'
  end

  def horizontal?(new_file)
    image = MiniMagick::Image.open(self.file.file)
    true if image[:height] < image[:width]
  end

  def vertical?(new_file)
    image = MiniMagick::Image.open(self.file.file)
    true if image[:height] > image[:width]
  end
end