Skip to content

Instantly share code, notes, and snippets.

@tmaximini
Created July 14, 2011 22:30
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tmaximini/1083624 to your computer and use it in GitHub Desktop.
Save tmaximini/1083624 to your computer and use it in GitHub Desktop.
carrierwave upload problem
require 'RMagick'
class ArticleSet < ActiveRecord::Base
validates_presence_of :name
validates_length_of :name, :minimum => 3
# creator
belongs_to :user
# products m:n
has_many :products, :through => :set_items
has_many :set_items, :dependent => :destroy
# users m:n (people liking this set)
has_many :users, :through => :likes
has_many :likes, :class_name => 'ArticleSetLike', :dependent => :destroy
# comments
has_many :comments, :dependent => :destroy
accepts_nested_attributes_for :set_items, :allow_destroy => true
after_create :create_collage
mount_uploader :blog_image, BlogImageUploader
def create_collage
# load the template
#template
# template = Magick::Image.read("#{RAILS_ROOT}/public/images/set_template.png").first
# or blank white image
template = Magick::Image.new(600, 480){
self.background_color = 'white'
}
# 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
img_name = "tmp/sets/set_#{self.id}_#{Time.now.to_i}.png"
# save composite image to PNG
template.write(img_name) # this works, it generates the desired PNG
#save and upload to s3
tmp_image = File.open(img_name)
#self.blog_image = File.open(img_name)
# self.blog_image = tmp_image
self.write_blog_image_identifier
self.save!
tmp_image.destroy
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment