Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Extension to Paperclip plugin for Ruby on Rails. It adds some possibly useful functionalities that original plugin lacks.

branch: master

This branch is 0 commits ahead and 0 commits behind master

Fetching latest commit…

Cannot retrieve the latest commit at this time

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.

  • Multiple S3 buckets Enables you to upload attachments to different S3 buckets. Modern web browsers are limited to download 2-4 files from one host at the same time, so placing your files in 4 different buckets (hosts) will let browsers download 8-16 files simultaneously.

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.

Multiple S3 buckets

Modern web browsers are limited to download 2-4 files from one host at the same time, so placing your files in 4 different buckets (hosts) will let browsers download 8-16 files simultaneously.

In your model:

class User < ActiveRecord::Base
  has_attached_file :avatar,
                    :storage => :s3,
                    :s3_credentials => "#{RAILS_ROOT}/config/s3.yml",
                    :path => "avatars/:id/:style_:extension",
                    :bucket => lambda do |attachment|
                      i = attachment.instance.id % 4
                      "bucket_#{i}"
                    end
end

This will place each avatar in one of four buckets: bucket_0, bucket_1, bucket_2 or bucket_3. The exact bucket is chosen at runtime and in this case it's based on models id.

Getting attachment's path: puts User.find(1).avatar.url(:original) # =>

If your attachments are images and you display many of them on the same page, now you should see them loading much faster.

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

Something went wrong with that request. Please try again.