I’m currently working with Rmagick on an image editing feature for a
rails app. I’m adding an editing function which would allow the user to
rotate, add effects, etc…to uploaded images. The interface is worked
around ajax functions, So for ex: my rotate function is something like
this:
render :update do |page|
page.replace_html('imageContainer', :partial => 'image')
page.visual_effect :highlight, 'imageContainer'
end
end
View:
edit.rhtml:
<%= render :partial => "image" %>
</div
Rotate | Crop | Resize
_image.rhtml partial:
What happens is the image gets rotated succesfully, but the RJS seems to
load before RMagick gets the image written to the filesystem, so when
the RJS re-loads is doesnt show the changes to the image.
Is there a way to allow the client to make changes, such as rotating
then cropping, then hit save, then RMagick writes the image? Like, how
would you keep the current image in a temporary memory?
Excuse the long post, but it really has me stumped.
If the image is really large, performing RMagick operations on it could
take
time. I was under the impression that this was blocking, but maybe I’m
wrong, but anyways, try moving your thumbnail manipulations before the
original image:
Also, an implementation consideration: you could consider queueing up
the
effects applied (in a session variable array, [‘rotate:90’, ‘scale:2’]
etc.
perhaps) and only applying the effects to the original image when the
user
hits “save”. Do the operations only on the thumb meanwhile since only
that’s
getting displayed.
What happens is the image gets rotated succesfully, but the RJS seems to
load before RMagick gets the image written to the filesystem, so when
the RJS re-loads is doesnt show the changes to the image.
Is there a way to allow the client to make changes, such as rotating
then cropping, then hit save, then RMagick writes the image? Like, how
would you keep the current image in a temporary memory?
Two things:
The #to_blob method returns the image as a string.
If you’re only dealing with 1 image at a time, use the Magick::Image
class instead of the Magick::ImageList class.
I’m having the same problem generating thumbnails (only I’m using
Magick::Image rather than ImageList). I’ve tried a bunch of stuff but
nothing seems to work so far. Incidently the problem seems to occur
regardless of the image size.
I’m probably going to resort to one of the following:
a spinner gif and an ajax request to see if the image is there yet
or
checking if the file is on disk before returning the generated
thumbnail path
(and manually blocking the return until the file is written)
both options make me cringe a little though…
It works pretty well – you might find it useful to have a look at the
code.
Or you can just use it as it – it only takes about ten minutes to build
a simple image-editing Rails app if you follow the README.
–Al Evans
Thanks for the advice Vishnu, and the Rails Engine Al (nice work btw).
It seemed my initial problem of the image not reloading after an action
was becuase it was being cached…a simple solution of adding a ?<%=
Time.now.to_i %> after the image path fixed it.
But it does seem more logical (and performace friendly) to queue up the
effects, or even make a copy of the image while it’s being edited to
allow revert/undo functions, etc…