
First grab the source:
wget ftp://ftp.imagemagick.org/pub/ImageMagick/ImageMagick.tar.gz
Unarchive it:
tar xvzf ImageMagick.tar.gz
The old ./configure / make / sudo make install ritual:
cd ImageMagick-6.3.8 # Or whichever the current version is, of course. ./configure make sudo make install
You should be good to go. Lately I’ve been having luck with MiniMagick (all I need to do is crop/resize for this particular project).
Type this to make sure you can use ImageMagick from the command line at least:
convert -version
I love that ruby (and many scripting languages) make it so easily to “shell out” to scripts (as minimagick does). It really makes ruby performance alarmists look bad when shelling out to time-tested, battle-hardened C-based scripts is so easy, and works so well. (I’ve had success shelling out to the following in many apps: curl, imagemagick, wget, etc)
How to Use MiniMagick in your Rails App
Grab the gem:
sudo gem install mini_magick
Drop this in your config/environment.rb:
require 'rubygems' gem 'mini_magick' require 'mini_magick'
Example usage:
class Pic < ActiveRecord::Base
# Where size is a string like '90x90', '300x200', etc
def create_perfect_thumbnail(size)
image = MiniMagick::Image.from_file(self.pic_path)
height, width = image['height'].to_f, image['width'].to_f
# FIRST SHAVE off some of the image to make it square
if width < height
shave = ((height - width)/2).round
image.shave("0x#{shave}")
else
shave = ((width - height)/2).round
image.shave("#{shave}x0")
end
image.resize(size)
image.write(self.pic_path(size))
# I had issues on my linux box with the pic not being readable by the web server,
# following the resize. Set permissions o+r to fix this.
if RAILS_ENV == 'production' # Set permissions to o+r
`chmod o+r #{self.pic_path(size)}`
end
end
def pic_path(size)
# Just an example -- I normally group pics by user_id under a public static dir.
File.join(RAILS_ROOT, 'public', 'static', "#{size}_#{self.original_filename}")
end
end
That will shave off some of the pic, making a munged square from the original, before proceeding to make square thumbnails from that.







