tags:

views:

803

answers:

4

I need to be able to quickly convert an image (inside a rails controller) so that the hosting company using managing our application can quickly test at any time to ensure that rmagick is not only successfully installed, but can be called throgh the rails stiack, what is the quickest clean code I can use to do this?

A: 

I'd log on to the server and try out your code in script/console. This will still go through the rails stack, but will allow you to quickly check that your code works the way you expect and that RMagick and ImageMagick are correctly installed without having to deploy anything.

When the time comes to write your actual code, I'd suggest putting the image conversion code inside a model, so you can call it outside the context of a controller.

tomafro
A: 

Use script/console, and call code in a model or a controller that does something like the following:

require 'RMagick'
include Magick
img = ImageList.new('myfile.jpg')
img.crop(0,0,10,10) # or whatever
img.write('mycroppedfile.jpg')
Asaf Bartov
+1  A: 

I wanted to do this so that I can easially hit it with a web browser, as I'm am deployng to managed servers, which I do not have shell access onto (for increased security).

So this is what I did

class DiagnosticsController < ApplicationController require 'RMagick'

  def rmagick
    images_path = "public/images"
    file_name = "rmagick_generated_thumb.jpg"
    file_path = images_path + "/"+ file_name

    File.delete file_path if File.exists? file_path
    img = Magick::Image.read("lib/sample_images/magic.jpg").first
    thumb = img.scale(0.25)
    @path = file_name
    thumb.write file_path
  end
end

and then in rmagick.html.erb

<%= image_tag @path %>

Now I can hit the controller, and if I see an image, I know rmagic is installed

Laurie Young
+6  A: 

require 'RMagick'

Magick::Image.new(110, 30){self.background_color = 'white'}.write('/tmp/test.jpg')

Scott