Skip to content

Instantly share code, notes, and snippets.

Created July 14, 2011 22:30
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
What would you like to do?
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 ="#{RAILS_ROOT}/public/images/set_template.png").first
# or blank white image
template =, 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 =
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)
img_name = "tmp/sets/set_#{}_#{}.png"
# save composite image to PNG
template.write(img_name) # this works, it generates the desired PNG
#save and upload to s3
tmp_image =
#self.blog_image =
# self.blog_image = tmp_image
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment