Basically, I'm trying to crop multiple images out of a remote/original scene.. Think flickr 'tagging', only, the pixs in the tagged boxes become their own unique images.. by way of simulating an upload from the controller using attachment fu.. can't quite figure out the specific method in fu that would allow me to do this.. I'm so near the edge right now!@ UGH =(
CONTROLLER: SCENE.RB # POST /yings # POST /yings.xml def create @ying = Ying.new(params[:ying]) scene = Scene.find(params[:ying][:scene_id]) scene.yings << @ying @ying.scale_to_scene(params[:ying], scene)
avatar = Avatar.create!(:uploaded_data => params[:ying] [:uploaded_data], :content_type => 'image/png', :filename=> "#{RAILS_ROOT}/public/images/avatars/yings/ #{@ying.id}_thing.png", :ying_id => self.id, :size => (@ying.width).to_i * (@ying.height).to_i ) avatar.ying_id = @ying.id avatar.scene_id = nil avatar.user_id = nil @ying.avatar = avatar avatar.thumbnails.clear # avatar.save!
respond_to do |format| if @ying.save_with_avatar(avatar) flash[:notice] = 'Ying was successfully created.' format.html { render :action => "show" } format.xml { render :xml => @ying, :status => :created, :location => @ying } else format.html { render :action => "new" } format.xml { render :xml => @ying.errors, :status => :unprocessable_entity } end end end
YING.rb def scale_to_scene(params, scene) x1 = params[:x1].to_f*scene.wscale.to_f y1 = params[:y1].to_i*scene.hscale.to_f width = params[:width].to_f*scene.wscale.to_f height = params[:height].to_f*scene.hscale.to_f img = Magick::Image.read("#{RAILS_ROOT}/ public#{scene.avatar.public_filename}")[0] chopped = img.crop(Magick::ForgetGravity, x1, y1, width, height) path = "#{RAILS_ROOT}/public/images/avatars/yings/ #{self.id}_thing.png" chopped = chopped.write(path) #create_or_update_thumbnail(path, suffix, *size) end
avatar_system.rb attr_accessor :uploaded_avatar_data
def save_with_avatar(params) avatar = Avatar.new begin self.transaction do if params[:uploaded_avatar_data] && params[:uploaded_avatar_data].size > 0 avatar.uploaded_data = (params[:uploaded_avatar_data]) avatar.thumbnails.clear avatar.save! self.avatar = avatar end save! end rescue if avatar.errors.on(:size) errors.add_to_base("Uploaded image is too big (500-KB max).") end if avatar.errors.on(:content_type) errors.add_to_base("Uploaded image content-type is not valid.") end false end end