Git Product home page Git Product logo

paperclip-extended's Introduction

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

paperclip-extended's People

Stargazers

Angus H. avatar Prashant Kumar Gupta avatar

Watchers

Prashant Kumar Gupta avatar James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.