Topic: Problems with Image Manipulation using ImageMagick and Rails

Hi All,
I'm trying to get familiar with the language and I figure the best way is to tinker and create some little web aps. The one I'm having problems with is manipulating an image I've pulled from Flickr and then writing my name on it. I've got the image pulled from flickr and saved to the public/images folder in my application tree, but I'm having problems writing the image with Image Magick.

Basically, in order to get this far I've been using three tutorials and I've borrowed bits from each and attempted to tie them together:
ImageMagick
Open-URI
and Flickr API

I can't figure out the image manipulation bit, can someone walk me through it and point out where I'm going wrong?

Error:

Magick::ImageMagickError in CodeController#flickrtest 
unable to open image `#RAILS_ROOT/public/images/image6.jpg': No such file or directory
RAILS_ROOT: C:/Documents and Settings/Richard Dowden/workspace/flickr/config/..

Application Trace | Framework Trace | Full Trace
#{RAILS_ROOT}/app/controllers/code_controller.rb:31:in `read'
#{RAILS_ROOT}/app/controllers/code_controller.rb:31:in `writeImage'
#{RAILS_ROOT}/app/controllers/code_controller.rb:20:in `flickrtest'
-e:3:in `load'
-e:3


Here's my code in the flickr_controller
class CodeController < ApplicationController
  def flickrtest
    flickruri = "http://www.flickr.com/services/xmlrpc/"
    server = XMLRPC::Client.new2(flickruri)
    flickrkey = "-----flickAPIKEY-----"
    nsid = "-----flickrNSID-----"
    details = {:api_key => flickrkey, :user_id => nsid}
    result = server.call("flickr.people.getPublicPhotos", details)
    @doc = REXML::Document.new result
   
    #get the number of photos
    docLength = @doc.root.elements.size
    @random = rand(docLength)+1
   
    loc = @doc.root.elements[@random]
    @image_url = "http://static.flickr.com/" \
   "#{loc.attributes["server"]}/" \
   "#{loc.attributes["id"]}_#{loc.attributes["secret"]}_b.jpg"
   
   writeImage

  end
 
  private
  def writeImage
    open("#{RAILS_ROOT}/public/images/image" << @random.to_s << ".jpg", "wb").
      write(open(@image_url).read)
     
    imgOut = Magick::Image.read("#RAILS_ROOT" \
              << "/public/images/image" << @random.to_s << ".jpg").first
   
    drawable = Magick::Draw.new
    drawable.pointsize = 30.0
    drawable.font = ("#{RAILS_ROOT}/artwork/fonts/REZB.ttf")
    drawable.fill = 'white'
    drawable.gravity = Magick::CenterGravity
   
    drawable.annotate(imgOut, 0, 0, -3, -6, "Richard Dowden")
    send_data imgOut.to_blob, :filename => "image" << @random.to_s << ".jpg",
                                :disposition => 'inline',
                                :type => "image/jpg"
  end
end


and in the view:
<%= image_tag("image" << @random.to_s << ".jpg")%>

In the future I want to bypass this storage of the image on the server and send the edited image straight to the user, but I'm not sure how to do that yet... one step at a time...

Sorry this post was so long, but I'm not sure which bits are relevant yet!

Many Thanks in advance!
Daf

Re: Problems with Image Manipulation using ImageMagick and Rails

You are missing the {} around RAILSROOT

imgOut = Magick::Image.read("#{RAILS_ROOT}" << "/public/images/image" << @random.to_s << ".jpg").first

You probably don't even need the quotes:

imgOut = Magick::Image.read(RAILS_ROOT << "/public/images/image" << @random.to_s << ".jpg").first

Or if you want to stick it all in one string:

imgOut = Magick::Image.read("#{RAILS_ROOT}/public/images/image#{@random}.jpg").first

Ends up being a little shorter that way.

Railscasts - Free Ruby on Rails Screencasts

Re: Problems with Image Manipulation using ImageMagick and Rails

Ahh great, thanks!

Such a daft mistake! I'll be sure to make a note of that.

One last thing, the #{RAILS_ROOT} variable; why doesn't this work when used within a view? I'm guessing it's probably not a good idea have files stored outside of the public directory, but I was just wondering whether it's possible?

Re: Problems with Image Manipulation using ImageMagick and Rails

RAILS_ROOT is a constant, so it should work everywhere (AFAIK, haven't exactly tested it). Can you post the code that isn't working?

Or are you referring to giving the public access to a file outside of the public directory? This is a big security risk if you are attempting to do this. Best to make a symbolic link to the file or something so you have control over what the public can access..

Railscasts - Free Ruby on Rails Screencasts

Re: Problems with Image Manipulation using ImageMagick and Rails

Well, what I was attempting to would give them access to the file outside of the public directory; I've not heard about symbolic links, are there any tutorials or further information about them?

Re: Problems with Image Manipulation using ImageMagick and Rails

See the wikipedia entry. Unfortunately they aren't available on Windows, but if you are deploying to Linux or something you can use them there. I'm not sure if Ruby can follow Windows shortcuts but might be worth a try.

Railscasts - Free Ruby on Rails Screencasts

Re: Problems with Image Manipulation using ImageMagick and Rails

Ahh it's no worry, I didn't know you were referring to feature in linux file systems, thought it was a feature of rails. I'll just steer clear of accessing files other than those in public. Thanks for all your help!