views:

853

answers:

2

I would like to alter the processing of thumbnails in paperclip by having imagemagick apply a drop shadow to all the thumbnails. What I'm stuck on is the actual imagemagick command that would pull this little miracle off. Everything I've tried returns an incorrectly scaled drop shadow without the original image.

def transformation_command
  scale, crop = @current_geometry.transformation_to(@target_geometry, crop?)
  trans = ""
  trans << " -resize \"#{scale}\""
  trans << " -crop \"#{crop}\" +repage" if crop
  # Apply Drop Shadow
  trans << " #{convert_options}" if convert_options? 
  trans
end

One I've tried...

def transformation_command
  scale, crop = @current_geometry.transformation_to(@target_geometry, crop?)
  trans = ""
  trans << " -resize \"#{scale}\""
  trans << " -crop \"#{crop}\" +repage" if crop
  trans << " \( +clone -background black -shadow 60x5+10+10 \) +swap -background none -layers merge +repage"
  trans << " #{convert_options}" if convert_options? 
  trans
end

I'm completely new to imagemagick, any help would be greatly appreciated.

A: 

I find it a lot easier to just use the rmagick interface rather then sending command line options to imagemagick itself.

If you use rmagick you can use the shadow method.

img = Image.read('slide.png').first
shadow = img.shadow(0, 0, 0.0, '20%')

and then composite the image over the shadow.

I wrote an article on using rmagick: http://schf.uc.org/articles/2006/10/18/render-greatlooking-collages-with-ruby-and-rmagick

Try reading it over it might give you a better understanding.

I also wrote an abstraction lib to rmagick which attempts to make it even easier to use. I called it RubyShop because it tried to mimic photoshop layer based compositing.. (I really hate the name and will probably change it if I ever resurrect the project)

Corban Brook
+3  A: 

After some trial and error and burying my head in the docs, I finally figured it out.

has_attached_file :image, 
  :styles => { :thumb => ["100x100#", :png] }, 
  :convert_options => { :thumb => '\( +clone -background black -shadow 70x4+0+0 \) +swap -background none -layers merge +repage' }
  1. Make sure you have the latest version of ImageMagick installed.
  2. ["100x100#", :png] will convert the image to png so the drop shadow is transparent.
  3. Under convert options, :thumb will only apply the conversion to the :thumb style, use :all to apply the conversion to all your styles.
  4. Tweak "70x4+0+0" to get the shadow you want.
MediaJunkie