Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/teeparham/paperclip-meta
Adds width, height, and size to paperclip images.
https://github.com/teeparham/paperclip-meta
images paperclip rails ruby
Last synced: 8 days ago
JSON representation
Adds width, height, and size to paperclip images.
- Host: GitHub
- URL: https://github.com/teeparham/paperclip-meta
- Owner: teeparham
- License: mit
- Created: 2010-10-03T15:03:24.000Z (about 14 years ago)
- Default Branch: main
- Last Pushed: 2022-12-09T08:24:59.000Z (almost 2 years ago)
- Last Synced: 2024-03-14T20:52:04.365Z (8 months ago)
- Topics: images, paperclip, rails, ruby
- Language: Ruby
- Homepage:
- Size: 187 KB
- Stars: 158
- Watchers: 4
- Forks: 97
- Open Issues: 10
-
Metadata Files:
- Readme: README.md
- License: LICENSE.txt
Awesome Lists containing this project
README
# Paperclip Meta
[![Gem Version](https://badge.fury.io/rb/paperclip-meta.svg)](http://rubygems.org/gems/paperclip-meta)
[![Build Status](https://travis-ci.org/teeparham/paperclip-meta.svg?branch=master)](https://travis-ci.org/teeparham/paperclip-meta)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 5.x and 6.x.
Version 2.x works with paperclip version 4.x.
### Setup
Add paperclip-meta to Gemfile:
```ruby
gem 'paperclip-meta'
```Create migration to add a *_meta column:
```ruby
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:
```ruby
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:
```ruby
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:
```ruby
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:
```sh
bundle
bundle exec rake
```