【问题标题】:Resize images with carrierwave if it exceeds height/width如果超过高度/宽度,则使用载波调整图像大小
【发布时间】:2013-10-18 14:45:59
【问题描述】:

我环顾四周,在我尝试自己做之后找不到解决方案。当用户上传照片时,如果超过我的最小和最大尺寸,我希望它们调整大小。但是我想要两个条件。横向(东/西)拍摄的照片应保持在我设置的尺寸范围内,高位(北/南)拍摄的照片也是如此。

例如,用户上传的照片位于很远的位置,尺寸为 3264x1840。上传文件的大小应调整为适合 584x329。如果上传小于 584x329 则不会调整大小。

另一个例子是,如果用户上传的照片是从高处拍摄的,尺寸为 2448 x 3264。上传的照片应调整大小以适合 247x329。

我试图将 MiniMagick 与此一起使用,因为我相信这是必需的。如果我只能使用 CarrierWave 那就完美了,但我认为 MiniMagick 应该用于调整照片大小。

我收到的错误是来自控制器中的 def create 的“未定义方法 resize' for #<ImageUploader:0x007f8606feb9b8>' and it points to@photo = Photo.new(params[:photo])”。

顺便说一句,尺寸很高,因为这些通常是您上传照片时手机的默认尺寸。

image_uploader.rb:

class ImageUploader < CarrierWave::Uploader::Base

   include CarrierWave::MiniMagick

 storage :file
  # storage :fog

  # Override the directory where uploaded files will be stored.
  # This is a sensible default for uploaders that are meant to be mounted:
  def store_dir
    "uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}"
  end


  process :resize => [584, 329]

  def resize_to_limit(width, height)
        manipulate! do |img|
          img.resize "#{width}x#{height}>"
          img = yield(img) if block_given?
          img
        end
      end

  # Create different versions of your uploaded files:
   version :thumb do
     process :resize_to_limit => [200, 200]
   end

end

照片控制器:

  def create
    @photo = Photo.new(params[:photo])
    @photo.user = current_user
    if @photo.save
      flash[:notice] = "Successfully created photos."
      redirect_to :back
    else
      render :action => 'new'
    end
  end

def resize(width, height, gravity = 'Center')
  manipulate! do |img|
    img.combine_options do |cmd|
      cmd.resize "#{width}"
      if img[:width] < img[:height]
        cmd.gravity gravity
        cmd.background "rgba(255,255,255,0.0)"
        cmd.extent "#{width}x#{height}"
      end
    end
    img = yield(img) if block_given?
    img
  end
end

【问题讨论】:

    标签: ruby-on-rails carrierwave minimagick


    【解决方案1】:

    将图片上传器中的:resize改为process :resize_to_fit =&gt; [584, 329]

    【讨论】:

      猜你喜欢
      • 2013-04-28
      • 2013-09-14
      • 2017-12-08
      • 1970-01-01
      • 1970-01-01
      • 2014-04-04
      • 2016-03-30
      • 2012-03-08
      • 2012-10-01
      相关资源
      最近更新 更多