RMagick: How to run scripts in rails

Hey,

I just managed to install ImageMagick and the RMagick gem for rails. But
I’m not sure how to run the scripts from the documentation.

For example:

  1. require ‘RMagick’
  2. include Magick
  3. cat = ImageList.new(“Cheetah.jpg”)
  4. cat.display
  5. exit

Does this go into the controller or view?? Seems simple but I’m new to
this woudl appreciate any help?

Cheers

First off, cat.display probably won’t work.

What you will need to do is something like this:

These go near the end of config/environment.rb:
require ‘RMagick’
include Magick

These go in a controller action, say “show_cat”:

def show_cat
cat = ImageList.new(“Cheetah.jpg”)
image = cat.first # first image frame in the image list
img.format = ‘JPG’ # force conversion to jpeg if needed

send_data(img.to_blob, { :type => ‘image/jpeg’,
:filename => ‘Cheetah.jpg’,
:disposition => “inline” })

end

I believe that send_data is a “show stopper” in that it does not
render a template once you use it. At least I don’t have a
show_cat.rhtml or other type of file for it to render. Note that this
method could use some error checking to make certain that an image is
actually loaded.

I also do this in the same controller, before calling send_data():

#
# Set expiration time, in hopes some of these can be cached.
#
cache_days = 1
expires_header = DateTime.now.new_offset(0) + cache_days
age_seconds = cache_days * 24 * 60 * 60
headers['Pragma'] = 'public'
headers['Cache-Control'] = "public max-age=#{age_seconds}"
headers['Expires'] = expires_header.strftime('%a, %d %b %Y %H:%M:%S 

GMT’)

This lets the image be cached. If you use unique URLs for each of
your images, you can get away with this.

–Michael

Ok thanks. So to display the images in the view do I just call the cat
variable?

I don’t think you understand how images work in html.

To display an image, you use the standard html tag. This calls your
controller with the action you want. For instance, I have a
“avatar_img_tag” helper action which generates image tags of the form:

for me.

When this html is processed by a browser, it will make another request
to your rails code to retrieve the image itself.

That controller has an action, “show”, which calls send_data. You
cannot really use the image data directly in your view; you must use
send_data to send the image data.

–Michael

Hey Michael,

Thanks for your info., but I’m still kind of confused. Sorry I’m still
new to this. Say for example I had something below, how would I display
the rotated image in the view (edit.rhtml)?

def edit
@image = Magick::ImageList.new("/home/Desktop/clown.jpg")
@image = @image.first
@image = @image.rotate(90)
@image = @image.write("/home/Desktop/clown.jpg")
end

Cheers

On 10/17/07, Rajeev K. [email protected] wrote:

def edit
@image = Magick::ImageList.new(“/home/Desktop/clown.jpg”)
@image = @image.first
@image = @image.rotate(90)
@image = @image.write(“/home/Desktop/clown.jpg”)
end

At that point, after the @image.write part (remove the @image = part
of that, btw) you have written it to a file.

In your edit.rhtml file, you would say something like :

and then in your controller, put something like the code I sent
previously that calls send_data() or send_file().

Just be careful using pathnames – remove all cases of … from them or
you’ll be burned.

Note that one “hit” to your server CANNOT both display HTML AND IMAGE
CONTENT. You have to do this in two steps.

–Michael

Sorry I have got another question.

I’m using the attachment_fu plugin to upload and display images. The
code below works where my image (stored on file system) is displayed in
the view. However, when I add the following line:

@image = Magick::ImageList.new(@image)

I get an error:

Magick::ImageMagickError in MainController#edit

unable to open image `/config/…//logos/0000/0001/300.jpg’: No such file
or directory

<–code–>

<-- controller -->
def edit
@logo = Logo.find(params[:id])
@image = “/#{RAILS_ROOT}/#{@logo.public_filename}”
end

<-- view -->

Logos
<%= image_tag(@image) %>

<%= link_to ‘Rotate’, :action => ‘rotate’ %>

<%= link_to ‘Back’, :action => ‘all’ %>

I don’t understand why, because the path to the file is correct. Any
suggestions?

Cheers

Hi,
I am trying to install RMAgick but just cant seem to get ImageMagick
working. (ubuntu)
Can you perhaps tell me what gems (including version numbers) you have
installed and in which order.
The actuall links to the gems you installed to get RMagick working would
be
a blessing.

Sorry for putting this question here, but you guy’s probably all got
this
working with ease.

Thanks in Advance

Scott A S wrote:

I get an error:
@logo = Logo.find(params[:id])


Posted via http://www.ruby-forum.com/.


View this message in context:
http://www.nabble.com/RMagick%3A-How-to-run-scripts-in-rails-tf4626597.html#a13332808
Sent from the RubyOnRails Users mailing list archive at Nabble.com.