Carrierwave 在保存前调整图像大小
Carrierwave resize an image before saving
我正在使用 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
但是当我上传图片时,尺寸并没有变成 150x150。
有什么方法可以调整图像大小并将其保存为调整后的大小 (150x150)?
如果您想要将图片大小调整为恰好 150x150,即使这意味着裁剪图片,您需要 resize_to_fill
来自载波
调整图像大小以适应指定尺寸,同时保持原始图像的纵横比。如有必要,以更大的尺寸裁剪图像。
你确定你传递的参数正确吗?可能是它没有让您在那里输入。试试……像这样
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 中添加的唯一一行是:
处理 resize_to_fill:[150, 150]
这就是我将在我自己的案例中重构您的代码的方法。使用此上传器时,上传的图像将被缩放到不超过 150 x 150 像素。然后创建一个名为 thumb 的版本,它被缩放和裁剪到恰好 190 x 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
我正在使用 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
但是当我上传图片时,尺寸并没有变成 150x150。 有什么方法可以调整图像大小并将其保存为调整后的大小 (150x150)?
如果您想要将图片大小调整为恰好 150x150,即使这意味着裁剪图片,您需要 resize_to_fill
来自载波
调整图像大小以适应指定尺寸,同时保持原始图像的纵横比。如有必要,以更大的尺寸裁剪图像。
你确定你传递的参数正确吗?可能是它没有让您在那里输入。试试……像这样
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 中添加的唯一一行是: 处理 resize_to_fill:[150, 150]
这就是我将在我自己的案例中重构您的代码的方法。使用此上传器时,上传的图像将被缩放到不超过 150 x 150 像素。然后创建一个名为 thumb 的版本,它被缩放和裁剪到恰好 190 x 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