# RMagick: How to run scripts in rails

**URL:** <https://www.ruby-forum.com/t/rmagick-how-to-run-scripts-in-rails/117258>\
**Category:** Rails\
**Created:** [October 15, 2007, 1:27pm UTC](https://www.ruby-forum.com/t/rmagick-how-to-run-scripts-in-rails/117258 "2007-10-15T13:27:00Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![ratschi85](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/77aa72/32.png) [@ratschi85](https://www.ruby-forum.com/u/ratschi85)\
**Post date:** [October 15, 2007, 1:27pm UTC](https://www.ruby-forum.com/t/rmagick-how-to-run-scripts-in-rails/117258/1 "2007-10-15T13:27:00Z")

</div>

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. 
4. cat = ImageList.new(“Cheetah.jpg”)
5. cat.display
6. exit

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

Cheers

---

<div class="post-metadata">

**Author:** ![ratschi85](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/77aa72/32.png) [@ratschi85](https://www.ruby-forum.com/u/ratschi85)\
**Post date:** [October 16, 2007, 12:32am UTC](https://www.ruby-forum.com/t/rmagick-how-to-run-scripts-in-rails/117258/2 "2007-10-16T00:32:15Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![ratschi85](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/77aa72/32.png) [@ratschi85](https://www.ruby-forum.com/u/ratschi85)\
**Post date:** [October 16, 2007, 5:01pm UTC](https://www.ruby-forum.com/t/rmagick-how-to-run-scripts-in-rails/117258/3 "2007-10-16T17:01:23Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![ratschi85](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/77aa72/32.png) [@ratschi85](https://www.ruby-forum.com/u/ratschi85)\
**Post date:** [October 16, 2007, 5:20pm UTC](https://www.ruby-forum.com/t/rmagick-how-to-run-scripts-in-rails/117258/4 "2007-10-16T17:20:56Z")

</div>

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:

![](https://www.ruby-forum.com/avatar/show/1)

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

---

<div class="post-metadata">

**Author:** ![ratschi85](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/77aa72/32.png) [@ratschi85](https://www.ruby-forum.com/u/ratschi85)\
**Post date:** [October 17, 2007, 4:56pm UTC](https://www.ruby-forum.com/t/rmagick-how-to-run-scripts-in-rails/117258/5 "2007-10-17T16:56:22Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![ratschi85](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/77aa72/32.png) [@ratschi85](https://www.ruby-forum.com/u/ratschi85)\
**Post date:** [October 17, 2007, 5:26pm UTC](https://www.ruby-forum.com/t/rmagick-how-to-run-scripts-in-rails/117258/6 "2007-10-17T17:26:18Z")

</div>

On 10/17/07, Rajeev K. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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

---

<div class="post-metadata">

**Author:** ![ratschi85](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/77aa72/32.png) [@ratschi85](https://www.ruby-forum.com/u/ratschi85)\
**Post date:** [October 18, 2007, 12:49pm UTC](https://www.ruby-forum.com/t/rmagick-how-to-run-scripts-in-rails/117258/7 "2007-10-18T12:49:48Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![ratschi85](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/77aa72/32.png) [@ratschi85](https://www.ruby-forum.com/u/ratschi85)\
**Post date:** [October 22, 2007, 8:45am UTC](https://www.ruby-forum.com/t/rmagick-how-to-run-scripts-in-rails/117258/8 "2007-10-22T08:45:25Z")

</div>

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/](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](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](http://Nabble.com).
