GithubHelp home page GithubHelp logo

borzik / paperclip-meta Goto Github PK

View Code? Open in Web Editor NEW

This project forked from kshnurov/paperclip-meta

0.0 0.0 0.0 192 KB

Adds width, height, and size to paperclip images.

License: MIT License

Ruby 100.00%

paperclip-meta's Introduction

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 5.x and 6.x.

Version 2.x 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
bundle exec rake

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.