1
我使用回形針將圖像文件附加到對象,一切都很好,但我還想在上載時掩蓋(最好只有一種圖像樣式 - main_feature)圖像。我可以使用masked_image.write('result.png')
保存我的蒙版圖像,但似乎無法讓它更新Paperclip的圖像屬性。用下面的代碼我得到這個錯誤:RMagick mask回形針圖像附件
No handler found for /var/folders/q1/xf59whv514lgw_xr10hb208m0000gn/T/gaither-nomask20130312-80235-yaj1s7.png PNG 1020x470 1020x470+0+0 DirectClass 8-bit
has_attached_file :image,
:styles =>{
:main_feature => "1020x470",
:top_feature => "345x159",
:smallest => "229x131"
},
:storage => :s3,
:s3_credentials => "#{Rails.root}/config/s3.yml",
:path => ":attachment/:id/:style.:extension",
:url => "/:id/:style/:basename.:extension",
:bucket => "bucketname"
after_image_post_process :post_process_image
def post_process_image
require 'rubygems'
require 'RMagick'
require "open-uri"
mask = Magick::ImageList.new
urlimage = open("http://bucket.s3.amazonaws.com/mask.png")
mask.from_blob(urlimage.read)
imgfile = Magick::Image.read(image.queued_for_write[:original].path).first
mask.matte = true
imgfile.matte = true
masked_image = imgfile.composite(mask, Magick::CenterGravity, Magick::CopyOpacityCompositeOp)
self.update_attribute(:image, masked_image)
end