Adds width, height, and size to paperclip images.
Ruby
Latest commit 9d30443 May 3, 2016 @teeparham Merge pull request #43 from fishbrain/master
Fix original image meta not being set during creation when using delayed_paperclip
Failed to load latest commit information.
lib
spec Add failing test for original image meta not set during creation when… Apr 28, 2016
.gitignore
.rubocop.yml Generate offense-free rubocop todo file Dec 29, 2015
.rubocop_todo.yml
.travis.yml
Gemfile
LICENSE.txt
README.md
Rakefile [style] Double quotes Dec 29, 2015
paperclip-meta.gemspec

README.md

Paperclip Meta

Gem Version Build Status

Add width, height, and size to paperclip images.

Paperclip Meta gets image dimensions after post_process_styles using paperclip's Geometry.from_file.

Paperclip Meta works with paperclip version 4.x.

Setup

Add paperclip-meta to Gemfile:

gem 'paperclip-meta'

Create migration to add a *_meta column:

class AddAvatarMetaToUsers < ActiveRecord::Migration
  def change
    add_column :users, :avatar_meta, :text
  end
end

Rebuild all thumbnails to populate the meta column if you already have some attachments.

Now you can grab the size from the paperclip attachment:

image_tag user.avatar.url, size: user.avatar.image_size
image_tag user.avatar.url(:medium), size: user.avatar.image_size(:medium)
image_tag user.avatar.url(:thumb), size: user.avatar.image_size(:thumb)

Internals

The meta column is simple hash:

style: {
  width:  100,
  height: 100,
  size:   42000
}

This hash will be marshaled and base64 encoded before writing to model attribute.

height, width, image_size and aspect_ratio methods are provided:

user.avatar.width(:thumb)
=> 100
user.avatar.height(:medium)
=> 200
user.avatar.image_size
=> '60x70'
user.avatar.aspect_ratio
=> 1.5

You can pass the image style to these methods. If a style is not passed, the default style will be used.

Alternatives

https://github.com/thoughtbot/paperclip/wiki/Extracting-image-dimensions

Development

Test:

bundle
rake

Test paperclip 4.x:

BUNDLE_GEMFILE=./spec/gemfiles/Gemfile.paperclip-4 bundle
BUNDLE_GEMFILE=./spec/gemfiles/Gemfile.paperclip-4 rake