使用CarrierWave调整图像大小而不保留原始高宽比

问题描述 投票:0回答:1

我正在使用CarrierWave为我上传的图像添加版本。 resize_to_fit正在调整图像的大小以适应指定的尺寸,同时保留原始宽高比。如何在不保留原始宽高比的情况下调整图像大小,也不能裁剪它们?

ruby-on-rails ruby carrierwave rmagick minimagick
1个回答
0
投票

您需要使用Carrierwave的manipulate!方法在上传器中定义自定义处理方法。这是我刚刚测试过的一个例子:

class Uploader < CarrierWave::Uploader::Base
    include CarrierWave::MiniMagick

    storage :file
    process :force_resize => [160, 160]

    def force_resize(width, height)
        manipulate! do |img|
            img.resize("#{width}x#{height}!")
            img
        end
    end
end

这会将图像大小调整为160x160px,而无需考虑宽高比(请注意resize参数末尾的感叹号)。

© www.soinside.com 2019 - 2024. All rights reserved.