I’ve a controller with an action that should read an image file from a
specified path and do some operations on it (mainly, resize it).
I try to read the image through this line of code (the image is placed
in applicationname\public\images.
img = Magick::Image.read(“world.jpg”)
The fact is that whenever I open that page, the browser freezes and I
have to restart my webserver (for instance, I’m using Webrick on Windows
XP)…
Here’s one I’m using. Let me know if you have questions. You can catch
any exceptions and output the error, which might help you figure out
what’s going on with your setup. Also check your logs for any reported
errors.
def submit_crop_image
require ‘RMagick’
@user = curr_user
tmpfile = "#{RAILS_ROOT}/public/images/users/tmp/#{@user.id}.jpg"
img001 = "#{RAILS_ROOT}/public/images/users/#{@user.id}_001.jpg"
img002 = "#{RAILS_ROOT}/public/images/users/#{@user.id}_002.jpg"
img003 = "#{RAILS_ROOT}/public/images/users/#{@user.id}_003.jpg"
size001 = 100
size002 = 50
size003 = 16
top = params['top'].to_i
left = params['left'].to_i
width = params['width'].to_i
height = params['height'].to_i
begin
img = Magick::Image::read(tmpfile).first
unless width == 0 or height == 0
img.crop!(left,top,width,height)
end
img.resize! size001, size001
img.write img001
img.resize! size002, size002
img.write img002
img.resize! size003, size003
img.write img003
File.delete tmpfile
rescue Exception => err
@upload_image_error = 'Could not process your image file. Please
Also, trying using “script/console” to play with this. The above lines
can be typed in there. It will make it easier to experiment without
crashing the development web server.
As you can see, #{RAILS_ROOT} is totally messed up…
The weird thing is that if I do:
File.size(myfile), it returns the filesize correctly, confirming that
the file exists… :-/
But when I call Image.read I get the same old error… No matter if I
use #{RAILS_ROOT} or not…
File Contents:// Place your application-specific JavaScript functions
and classes here
// This file is automatically included by javascript_include_tag
:defaults
=> nil
As you can see, #{RAILS_ROOT} is totally messed up…
No, your RAILS_ROOT is fine. You need parentheses around method
arguments when passing a block with { }:
Thanks to all for the help, so far, you’ve been great!
From my console now I can call the methods of the Image class with no
problems. Still I’m not able to do what I want, that is: