3

画像がバージョンのサイズよりも大きい場合にのみ、carrierwave でバージョン (たとえば、親指) を作成できますか??

例:

version :thumb, :if => :is_thumbnable? do 
    process :resize_to_fit => [32,nil]
end

protected

def is_thumbnable?(file)
  image ||= MiniMagick::Image.open(file.path)
  if image.nil?
    if image['width'] >= 32 || image['height'] >= 32
      true
    else
      false
    end
  else
    false
  end
end
4

3 に答える 3

4

この場合、画像が指定された幅を超えた場合に、32ピクセルのサイズに操作する方法を定義しました。このコードをImageUploaderに配置します。

  version :thumb do 
    process :resize_to_width => [32, nil]
  end

  def resize_to_width(width, height)
    manipulate! do |img|
      if img[:width] >= width
        img.resize "#{width}x#{img[:height]}"
      end
      img = yield(img) if block_given?
      img
    end
  end
于 2012-09-12T17:06:47.870 に答える
0

実際、@ Roza ソリューションはうまくいきませんでした。次のようにメソッドを変更する必要がありました。

process :resize_to_width => [650, nil]

def resize_to_width(width, height)
  manipulate! do |img|
    if img.columns >= width
      img.resize(width)
    end
    img = yield(img) if block_given?
    img
  end
end

rmagick (2.13.2) と rails 3.2.13、carrierwave (0.8.0) を使用しています

于 2013-06-07T13:39:47.363 に答える