# Having trouble with a remote-crop-then-resize using attachment fu =(

**URL:** <https://www.ruby-forum.com/t/having-trouble-with-a-remote-crop-then-resize-using-attachment-fu/132425>\
**Category:** Rails\
**Created:** [March 20, 2008, 1:13am UTC](https://www.ruby-forum.com/t/having-trouble-with-a-remote-crop-then-resize-using-attachment-fu/132425 "2008-03-20T01:13:06Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [March 20, 2008, 1:13am UTC](https://www.ruby-forum.com/t/having-trouble-with-a-remote-crop-then-resize-using-attachment-fu/132425/1 "2008-03-20T01:13:06Z")

</div>

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
