使用 Carrierwave 保存生成的图像 - 无表格,全部在模型内
这是一个棘手的问题,我希望有人可以帮助我。
我有一个article_set,其中有很多set_item,每个set item都有一个图像。 我想创建一幅图像拼贴画,因此是多幅图像中的一幅,并用载波保存它。
这就是我的模型中发生的情况:
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 值。 当然,它应该包含生成文件的文件名...... 非常感谢任何帮助。
谢谢
this is a tricky one, I hope someone can help me.
I have a article_set, which has many set_items, each set item has an image.
I want to create an collage of the images, so one image out of many, and save it with carrierwave.
that's whats going on in my model:
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
So far, so good. I can save the set and it runs though this method, but I guess the last part
#save and upload to s3
self.blog_image.store!(template)
self.blog_image = self.blog_image.store_path
self.write_carrierwave_identifier
self.save
is probably incorrect, in my database I get only NULL values for blog_image.
It should, of course, contain the file name of the generated file...
any help highly appreciated.
thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
最后第三行看起来有点狡猾:
如果你删除它,我相当确定它应该可以工作。
That 3rd last line looks a bit dodgy:
If you remove that i'm fairly sure it should work.