How to process only on original image file using CarrierWave?

三世轮回 提交于 2019-12-08 02:02:18

问题


I use CarrierWave to generate versions (thumbnails with different sizes) and also to add a watermark on each versions.

I have currently manage to apply the watermark for each thumbnails but I would like to add it on the original image to.

Here is what I tried:

def watermark
  manipulate! do |img|
    watermark   = Magick::Image.read(Rails.root.join('app/assets/images/watermark_512.png')).first
    img         = img.composite(watermark, Magick::CenterGravity, Magick::OverCompositeOp)
  end
end

version :original do
  process :watermark
end

version :thumb_512 do
  process :resize_to_fit => [512, 512]
  process :watermark
end

version :thumb_256 do
  process :resize_to_fit => [256, 256]
  process :watermark
end

But this does not work. However I tried to simply add

process :watermark

outside any "version" block but all it does is adding twice the watermark on my thumbnails.


回答1:


You can use store callbacks provided by CarrierWave in your uploader class like this

class SomeUploader < CarrierWave::Uploader::Base
  # Your code ...

  before :store, :watermark_method

  def watermark_method(*args)
    # watermark it!
  end
end


来源:https://stackoverflow.com/questions/13903737/how-to-process-only-on-original-image-file-using-carrierwave

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!