2014-09-27 94 views
1

我想從原始圖像生成25%,50%,75%大小的圖像,但似乎載波的resize_to_fill/fit不支持百分比。任何人都知道如何做到這一點?如何按載波比例縮放圖像

謝謝。

+0

爲什麼不只是計算正確的數字?你不能使用一個小功能或這樣的翻譯50%的寬度/長度? – Coffee 2014-09-27 17:00:35

+1

不知道我是否可以使用像:process:resize_to_fit => [image.width * 0.5,image.height * 0.5]。順便說一下,每張上傳的圖片的大小都不一樣。 – crax 2014-09-28 02:57:43

回答

5

經過一番研究,我發現了一個解決方案:

process :store_dimensions 

    version :r_3x do 
    process :resize_to_fit_by_percentage => 0.75 
    end 

    private 

    def resize_to_fit_by_percentage(percentage) 
    resize_to_fit model.width*percentage, nil 
    end 


    def store_dimensions 
    if file && model 
     model.width, model.height = ::MiniMagick::Image.open(file.file)[:dimensions] 
    end 
    end 

首先獲得上傳的圖片的尺寸,然後定義自定義大小調整方法(這裏是resize_to_fi_by_percentage),以及像代碼這種方法調整圖像確實。