2013-03-25 49 views
1

我正在將載波上傳器轉換爲使用Cloudinary。我有一些類似這樣的方法,輸出散列在Cloudinary格式中,但不幸的是,在版本塊內部,您無法訪問外部方法。我想知道做什麼的最好方法是關於雲霧,或者甚至是可能的。如何使用條件邏輯執行Cloudinary轉換?

def custom_crop 
    if model.cropping? 
    cloudinary_transformation({x: model.crop_x.to_i, 
    y: model.crop_y.to_i, 
    width: model.crop_w.to_i, 
    height: model.crop_h.to_i, 
    crop: :crop}) 
    end 
end 

def watermark 
    if model.respond_to?(:watermarking?) && model.watermarking? 
    cloudinary_transformation({overlay: "watermark_x8b0vp", 
    gravity: :south_east, 
    x: 0, 
    y: 106}) 
    end 
end 

的代碼我非常想運行是這樣的:

version :cropped_original do 
    process :custom_crop 
    process :watermark 
    resize_to_fill(81, 50, :center) 
end 

回答

2

可以回到你的工藝方法所需要的變革。但是,在這種情況下,您可能想鏈接它們。你這樣做如下:

def custom_crop_and_watermark 
    transformation = [] 
    if model.cropping? 
    transformation << {x: model.crop_x.to_i, 
    y: model.crop_y.to_i, 
    width: model.crop_w.to_i, 
    height: model.crop_h.to_i, 
    crop: :crop} 
    end 
    if model.respond_to?(:watermarking?) && model.watermarking? 
    transformation << {overlay: "watermark_x8b0vp", gravity: :south_east, x: 0, y: 106} 
    end 
    {:transformation=>transformation} 
end 
+0

與此相關,請參閱model.cropping?是有條件的。我們的過程是有人上傳一張照片,然後他們會看到一個裁剪器來更新圖像的裁剪參數。目前尚不清楚如何更新照片(重新創建!是載波的方式),以及新的參數。 Cloudinary可能嗎? – bobbywilson0 2013-03-27 18:42:25

+0

當您使用座標更新模型時,對cropped_original.url的更多調用將更新圖像url並返回裁剪後的圖像。 – 2013-03-28 11:23:50