【问题标题】:How do I perform Cloudinary transformations with conditional logic?如何使用条件逻辑执行 Cloudinary 转换?
【发布时间】:2013-03-15 07:41:18
【问题描述】:

我正在将载波上传器转换为使用 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

【问题讨论】:

    标签: carrierwave cloudinary


    【解决方案1】:

    您可以从流程方法中返回您需要的转换。但是,在这种情况下,您可能希望将它们链接起来。你可以这样做:

    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
    

    【讨论】:

    • 跟这个有关,看看model.cropping怎么样?是有条件的。我们的过程是有人上传一张照片,然后他们会看到一个裁剪器,用于更新图像的裁剪参数。目前尚不清楚如何使用新的裁剪参数更新照片(重新创建!是carrierwave 中的方式)。 Cloudinary 可以做到这一点吗?
    • 当您使用坐标更新模型时,进一步调用cropped_original.url 将更新图像url并返回裁剪后的图像。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-07-08
    • 2010-10-29
    • 2011-08-21
    • 2016-08-05
    • 2021-08-14
    • 2012-07-08
    • 1970-01-01
    相关资源
    最近更新 更多