paperclip snippets

SEO optimized image URLs with the Paperclip Rails plugin

Tagged paperclip, rails, ruby, plugin, seo, url  Languages ruby

Create config/initializers/paperclip.rb:

# http://wiki.github.com/thoughtbot/paperclip/interpolations
Paperclip.interpolates :to_param do |attachment, style|
  attachment.instance.to_param
end

In the model:

has_attached_file :image, 
                    :path => ":rails_root/public/images:to_param/:style/:basename.:extension",
                    :url => "/images:to_param/:style/:basename.:extension",
                    :styles => { :large  => "250x360#",
                                 :medium => "150x230#",
                                 :small  => "110x150#" }

Instead of URLs like:

/images/products/249/large/temp.jpg

You can get a URL based on, for example, a permalink as in the example above. In my case I get URLs like this:

/images/games/nintendo-wii/large/super-mario-galaxy.jpg

Crop + Gravity + Paperclip

Tagged paperclip, geometry, weight, center  Languages ruby

By default the cropping is weighted at the center of the Geometry. I want it to be north west, so I came up with this dirty hack for setting the gravity to north west:

module Paperclip
 
   # Defines the geometry of an image.
  class Geometry

     def cropping dst, ratio, scale
       if ratio.horizontal? || ratio.square?
         #"%dx%d+%d+%d" % [ dst.width, dst.height, 0, (self.height * scale - dst.height) / 2 ]
         "%dx%d+%d+%d" % [ dst.width, dst.height, 0, 0 ]
       else
         #"%dx%d+%d+%d" % [ dst.width, dst.height, (self.width * scale - dst.width) / 2, 0 ]
         "%dx%d+%d+%d" % [ dst.width, dst.height, 0, 0 ]
       end
     end
  end
end

Paperclip plugin + Phusion Passenger + path problem

Tagged paperclip, apache, phusion, passenger  Languages ruby

In config/initializers/paperclip.rb put:

if RAILS_ENV == "development"
  Paperclip.options[:command_path] = '/opt/local/bin/'
else
  Paperclip.options[:command_path] = '/usr/bin/'
end

Note image_magick_path is deprecated alias of command_path.