2013-01-09 111 views

回答

2

安迪轟的答案的改良效果是什麼:

process :resize => [220, 220] 

protected 

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 
3

如果我理解正確的問題:

  • 的肖像圖像(比如480像素寬,640像素高),你會想它縮小到220px寬,然後再剪裁下來到220px高,導致在一個方形的圖像。

  • 對於風景圖像,您需要將其縮小到220px寬(因此高度將小於220px)。

如果這是正確的,你想有一個過程分爲兩個步驟:

  1. 大小調整爲220px寬,保持長寬比
  2. 裁剪以220px高(如縱向)

您可以通過使用manipulate!命令編寫自己的處理器來實現此目的(請參閱CarrierWave's own以獲取靈感)。

我想這大約是你之後

process :resize => [220, 220] 

protected 

def resize(width, height, gravity = 'Center') 
    manipulate! do |img| 
    img.combine_options do |cmd| 
     cmd.resize width.to_s 
     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 
+0

它說'不能轉換爲長整數在String'行'CMD .resize寬度'。 – ijo

+0

啊。然後將其更改爲'「#{width}」或'width.to_s'。 –

+0

但它然後說「未定義的方法」寫入「」:String' – ijo

相關問題