Sunday, May 22, 2011

[Rails] Re: How do you crop an image with carrierwave 'after the fact' in rails?

This is the best I can do. There's probably an easier way to do it,
but this is my hack:

Here is my 'POST' controller action when the cropping info is passed:

def update_crop
@user = User.find(current_user.id)
@user.crop(params[:x].to_i, params[:y].to_i, params[:h].to_i,
params[:w].to_i)

redirect_to(profile_path, :notice => 'Your profile and avatar
was successfully updated.')
end

Here is the method to add to the User model that contains an "avatar"
image uploader:

def crop(x, y, h, w)
image = Magick::ImageList.new(avatar.current_path)
cropped_image = image.crop(x, y, h, w)
cropped_image.write(avatar.current_path)

avatar.recreate_versions!
end

Basically this just hi-jacks the current one, overwrites it, and then
tells Carrierwave to create the thumbnail again with the new cropped
version. Seems to work.

--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To post to this group, send email to rubyonrails-talk@googlegroups.com.
To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.

0 Comments:

Post a Comment

Subscribe to Post Comments [Atom]

<< Home


Real Estate