How to apply shadow to thumbnails using imagemagick and paperclip?

I would like to change the processing of thumbnails in a folder using imagemagick to apply a shadow to all thumbnails. What I'm stuck with is a real imagemagick team that would pull out this little miracle. Everything I tried returns an incorrectly scaled 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 

I tried one ...

 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 am completely new to imagemagick, any help would be greatly appreciated.

+4
source share
2 answers

After some trial and error and burying my head in the documents, I finally realized this.

 has_attached_file :image, :styles => { :thumb => ["100x100#", :png] }, :convert_options => { :thumb => '\( +clone -background black -shadow 70x4+0+0 \) +swap -background none -layers merge +repage' } 
  • Make sure the latest version of ImageMagick is installed.
  • ["100x100 #" ,: png] converts the image to png so that the shadow is transparent.
  • In the conversion options: thumb applies only to conversion to style: thumb, use: all to apply the conversion to all your styles.
  • Adjust "70x4 + 0 + 0" to get the desired shadow.
+4
source

It’s much easier for me to just use the rmagick interface rather than send command line options to imagine myself.

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 compose the image on top of the shadow.

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

Try reading it, it may give you a better understanding.

I also wrote a lib abstraction for rmagick that tries to make it even easier to use. I called it RubyShop because it tried to simulate a Photoshop-based layer layout .. (I really hate the name and will probably change it if I ever resurrect the project)

+1
source

All Articles