これはトリッキーなものです。誰かが私を助けてくれることを願っています。
多くのset_itemsがあるarticle_setがあり、各setitemには画像があります。画像のコラージュを作成したいので、多くの画像のうち1つを作成し、それをcarrierwaveで保存します。
それが私のモデルで起こっていることです:
require 'RMagick'
class ArticleSet < ActiveRecord::Base
# ...
after_create :create_collage
mount_uploader :blog_image, BlogImageUploader
def create_collage
# load the template
template = Magick::Image.read("#{RAILS_ROOT}/public/images/set_template.png").first
# go through all set items by z_index and add lay it over the template
for item in self.set_items
photo = Magick::Image.read(item.product.assets.first.image.url(:thumb).to_s).first
photo.scale(item.width, item.height)
# composite item over template offsetting pos_x and pos_y for the template border
template.composite!(photo, item.pos_x, item.pos_y, Magick::OverCompositeOp)
end
# save composite image to JPG
template.write("set_#{self.id}_#{Time.now.to_i}.jpg")
#save and upload to s3
self.blog_image.store!(template)
self.blog_image = self.blog_image.store_path
self.write_carrierwave_identifier
self.save
end
end
ここまでは順調ですね。セットを保存してこのメソッドで実行できますが、最後の部分だと思います
#save and upload to s3
self.blog_image.store!(template)
self.blog_image = self.blog_image.store_path
self.write_carrierwave_identifier
self.save
おそらく正しくありません。私のデータベースでは、blog_imageに対してNULL値しか取得していません。もちろん、生成されたファイルのファイル名が含まれている必要があります...助けていただければ幸いです。
ありがとう