Ruby on rails Carrier在保存前已调整图像大小

Ruby on rails Carrier在保存前已调整图像大小,ruby-on-rails,carrierwave,Ruby On Rails,Carrierwave,我正在使用Carrierwave进行图像上传,我需要在保存图像之前调整图像大小 在我的avatar_uploader.rb中,我有以下代码: class AvatarUploader < CarrierWave::Uploader::Base include CarrierWave::MiniMagick storage :file resize_to_fit(150, 150) def resize_to_fit(width, height) process :

我正在使用Carrierwave进行图像上传,我需要在保存图像之前调整图像大小

在我的avatar_uploader.rb中,我有以下代码:

class AvatarUploader < CarrierWave::Uploader::Base
  include CarrierWave::MiniMagick
  storage :file
  resize_to_fit(150, 150)

  def resize_to_fit(width, height)
    process :resize_to_fit => [width, height]
  end
end
类AvatarUploader[宽度、高度] 结束 结束 但当我上传图像时,大小不会更改为150x150。
是否有任何方法可以调整图像大小并将其保存为已调整大小(150x150)?

如果您想将图像大小调整为精确的150x150,即使这意味着裁剪图像,您需要
调整大小以填充

从卡里尔瓦夫


调整图像大小以适应指定尺寸,同时保留原始图像的纵横比。如有必要,裁剪较大尺寸的图像。

如果要将图像大小精确调整为150x150(即使这意味着裁剪图像),则需要
调整大小以填充图像。

从卡里尔瓦夫


调整图像大小以适应指定尺寸,同时保留原始图像的纵横比。如有必要,将图像裁剪成较大的尺寸。

您确定正确传递了参数吗?可能是没有得到你的意见。试试这样的东西

process :resize_to_fit => [150, 150]
特定版本的ro:

  version :thumbnail do
    process :resize_to_fit => [150, 150]
  end 

不要使用高度和宽度..

是否确实正确传递了参数?可能是没有得到你的意见。试试这样的东西

process :resize_to_fit => [150, 150]
特定版本的ro:

  version :thumbnail do
    process :resize_to_fit => [150, 150]
  end 

没有使用高度和宽度..

我的问题是行config.enable_processing=true 在我的carrierwave.rb中

我删除了它,现在的图像保存为150x150,因为我想

我需要在uploader.rb中添加的唯一一行是:
将处理大小调整为填充:[150150]

我的问题是行config.enable\u processing=true 在我的carrierwave.rb中

我删除了它,现在的图像保存为150x150,因为我想

我需要在uploader.rb中添加的唯一一行是:
处理调整大小以填充:[150150]

这是我将在自己的情况下重构代码的方法。使用此上载器时,上载的图像将缩放为不大于150×150像素。然后创建一个名为thumb的版本,将其缩放并裁剪为190×60像素

class AvatarUploader < CarrierWave::Uploader::Base

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

  storage :file

  process resize_to_fit: [150, 150]

  version :thumb do
    process resize_to_fill: [190, 60]
  end

  def extension_white_list
    %w(jpg jpeg gif png)
  end

end
类AvatarUploader
这是我在自己的情况下重构代码的方法。使用此上载器时,上载的图像将缩放为不大于150×150像素。然后创建一个名为thumb的版本,将其缩放并裁剪为190×60像素

class AvatarUploader < CarrierWave::Uploader::Base

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

  storage :file

  process resize_to_fit: [150, 150]

  version :thumb do
    process resize_to_fill: [190, 60]
  end

  def extension_white_list
    %w(jpg jpeg gif png)
  end

end
类AvatarUploader
谢谢您的回答。我将代码改为resize_to_fill,但我上传的图像仍然没有改变尺寸,也没有被裁剪。问题是您在类的主体中直接调用它的方式:process resize_to_fill=>[150150]再次感谢。我还有一个问题,我想从carrierwave.rb中删除行*config.enable_processing=true,现在它可以正常工作了,但无论如何,在强制执行维度时,调整_到_fill的大小是您需要的谢谢您的回答。我将代码改为resize_to_fill,但我上传的图像仍然没有改变尺寸,也没有被裁剪。问题是您在类的主体中直接调用它的方式:process resize_to_fill=>[150150]再次感谢。我还有一个问题,我想从carrierwave.rb中删除行*config.enable_processing=true,现在它可以正常工作了,但无论如何,在强制执行维度时,您需要调整_到_fill的大小