public
Description: Extension to Paperclip plugin for Ruby on Rails. It adds some possibly useful functionalities that original plugin lacks.
Homepage: http://codetunes.com/2008/07/30/custom-thumbnail-generation-with-paperclip
Clone URL: git://github.com/netguru/paperclip-extended.git
netguru (author)
Tue Sep 16 02:18:20 -0700 2008
commit  133352aa0bb979b67cfd46a414fdae1956fa73d6
tree    f835ef3915d256d042d080356a98984000a0d9d0
parent  0d1034fef805706840761edf392f6d56867b6c75
name age message
file MIT-LICENSE Wed Jul 30 02:50:57 -0700 2008 first commit [netguru]
file README Loading commit data...
file README.rdoc
file Rakefile Wed Jul 30 02:50:57 -0700 2008 first commit [netguru]
file init.rb Wed Jul 30 02:50:57 -0700 2008 first commit [netguru]
directory lib/
directory test/ Wed Jul 30 02:50:57 -0700 2008 first commit [netguru]
README.rdoc

PaperclipExtended

PaperclipExtended extends Paperclip plugin by Jon Yurek and thoughtbot. It adds some possibly useful functionalities that original plugin lacks.

Note that PaperclipExtended plugin is not a replacement for Paperclip. It requires that you have Paperclip plugin already installed.

PaperclipExtended is known to work with Paperclip 2.1.2 (current version at the time of development). Note that extensions provided by PaperclipExtended may be included in Paperclip in future, so this plugin will not be needed anymore.

Functionalities provided by PaperclipExtended

  • Custom commands Enables user to define additional options that will be passed to ImageMagick convert command after thumbnail generation by Paperclip.
  • File name normalization You can use :normalized_basename string in attachment’s :path or :url definition, which is later interpolated. It works just like :basename, but it is normalized by substituting unusual characters with underscores.

Usage

Custom commands

In your model:

  class User < ActiveRecord::Base
    has_attached_file :avatar, :styles => { :medium => "300x300>", :thumb => "100x100>" }, :commands => { :medium => "-background white -gravity center -extent 300x300 +repage" }
  end

The string you pass in commands hash will be attached to convert command after usual thumbnail generation by Paperclip.

The result convert command will be now: convert -scale "300x300>" -background white -gravity center -extent 300x300 +repage

Instead of just: convert -scale "300x300>"

The commands parameter is optional, also you can define it only for certain styles as above.

Read ImageMagick Command Line Options documentation for information on what can be put in commands parameter (www.imagemagick.org/script/command-line-options.php).

File name normalization

In your model:

  class User < ActiveRecord::Base
    has_attached_file :avatar,
                      :styles => { :thumb => "80x80#" },
                      :path => "#{RAILS_ROOT}/public/avatars/:id/:normalized_basename_:style.:extension",
                      :url => "/avatars/:id/:normalized_basename_:style.:extension"
  end

:normalized_basename works just like :basename, the only difference is that file’s basename is normalized by substituting unusual chars with underscores. By "unusual characters" are those that are not matched by /[A-Za-z0-9_-]/ regular expression. For example if "my avatar.jpg" file is uploaded it’s normalized basename would be "my_avatar.jpg". Such normalization may be useful in situations where you normally would url_encode the filename or something like that.

Copyright © 2008 Michal Szajbe (codetunes.com) and netguru (netguru.pl), released under the MIT license