RMagick Image.read doesn't... read!

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)…

How could I solve that?

Thanks in advance.

How could I solve that?

Thanks in advance.

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 

try again.’ + err
render :action => ‘edit_image’
else
@user.image_flag = ‘Y’
@user.save
redirect_to :action => “profile”, :username => @user.username
end
end

Thanks for the quick reply.

I get this error, which is pretty weird. I’ve created a variable with
the path to my image:

myfile = “#{RAILS_ROOT}/public/images/world.jpg”

but when I call the Read method of the Image class (img =
Magick::Image::read(myfile).first), I get this:

unable to open image `la/public/images/world.jpg’: No such file or
directory

It seems like the #{RAILS_ROOT} is truncated… :-/

Andrea G. wrote:

Thanks for the quick reply.

I get this error, which is pretty weird. I’ve created a variable with
the path to my image:

myfile = “#{RAILS_ROOT}/public/images/world.jpg”

but when I call the Read method of the Image class (img =
Magick::Image::read(myfile).first), I get this:

unable to open image `la/public/images/world.jpg’: No such file or
directory

It seems like the #{RAILS_ROOT} is truncated… :-/

For troubleshooting take RMagick out of the equation and just test find
the file with:

myfile = “#{RAILS_ROOT}/public/images/world.jpg”
File.open myfile, ‘r’ { |f| puts “Success!\n\nFile Contents:” + f.read
}

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.

Alex W. wrote:

myfile = “#{RAILS_ROOT}/public/images/world.jpg”
File.open myfile, ‘r’ { |f| puts “Success!\n\nFile Contents:” + f.read
}

This is what I get:

myfile = “#{RAILS_ROOT}/public/images/world.jpg”
=> “./script/…/config/…/config/…/public/images/world.jpg”

File.open myfile, ‘r’ { |f| puts “Success!\n\nFile Contents:” + f.read }
SyntaxError: compile error
(irb):2: syntax error
File.open myfile, ‘r’ { |f| puts “Success!\n\nFile Contents:” + f.read }
^
(irb):2: syntax error
File.open myfile, ‘r’ { |f| puts “Success!\n\nFile Contents:” + f.read }
^
(irb):2: syntax error
from (irb):2

As you can see, #{RAILS_ROOT} is totally messed up…

Andrea G. wrote:

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…

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 { }:

RAILS_ROOT
=> “script/…/config/…/config/…”

myfile = “#{RAILS_ROOT}/public/javascripts/application.js”
=> “script/…/config/…/config/…/public/javascripts/application.js”

File.open myfile, ‘r’ { |f| puts “Success!\n\nFile Contents:” + f.read }
SyntaxError: compile error
(irb):3: parse error, unexpected ‘{’, expecting $
File.open myfile, ‘r’ { |f| puts “Success!\n\nFile Contents:” + f.read }
^
from (irb):3

File.open(myfile, ‘r’) { |f| puts “Success!\n\nFile Contents:” + f.read }
Success!

File Contents:// Place your application-specific JavaScript functions
and classes here
// This file is automatically included by javascript_include_tag
:defaults
=> nil

Still I’m not able to do what I want, that is:

img.write “mythumbnail.jpg”

and display my image in the browser… Ideas?

Did you specify the path while writing?

img.write “#{RAILS_ROOT}/public/images/mythumbnail.jpg”

Display in the browser should be like displaying any other image with an
img tag.

Curtis S. wrote:

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:

img.write “mythumbnail.jpg”

and display my image in the browser… Ideas?

Curtis S. wrote:

Still I’m not able to do what I want, that is:

img.write “mythumbnail.jpg”

and display my image in the browser… Ideas?

Did you specify the path while writing?

img.write “#{RAILS_ROOT}/public/images/mythumbnail.jpg”

I have, but no results…

Is there another way to create thumbnails, besides RMagick? :frowning: