Skip to content

Commit

Permalink
First version
Browse files Browse the repository at this point in the history
  • Loading branch information
y8 committed Oct 3, 2010
1 parent b357190 commit 1cb5f21
Show file tree
Hide file tree
Showing 5 changed files with 121 additions and 0 deletions.
50 changes: 50 additions & 0 deletions README
@@ -0,0 +1,50 @@
=Paperclip Meta

Simple gem to let paperclip play nice with thumbnails width, height and size.

Paperclip Meta will get image dimensions right after post_process_styles using paperclips own Geometry.from_file. This should make paperclip-meta storage independent.

==Quick Start

Create migration:

class AddMetaToAvatar < ActiveRecord::Migration
def self.up
add_column :users, :avatar_meta, :text
end

def self.down
remove_column :users, :avatar_meta
end
end

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

Now you can use meta-magic:

<%= 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

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.

New methods provided:
- width
- height
- size

You can pass thumbnail style to all this methods. If style not passed, default_style will be used.

==TODO

* It will be nice to write some tests. :D
8 changes: 8 additions & 0 deletions Rakefile
@@ -0,0 +1,8 @@
require 'rake'
require 'rake/testtask'
require 'rake/rdoctask'

desc "Build the gem into the current directory"
task :gem => :gemspec do
`gem build #{spec.name}.gemspec`
end
1 change: 1 addition & 0 deletions init.rb
@@ -0,0 +1 @@
require File.join(File.dirname(__FILE__), "lib", "paperclip_meta")
43 changes: 43 additions & 0 deletions lib/paperclip_meta.rb
@@ -0,0 +1,43 @@
module Paperclip
class Attachment
alias original_post_process_styles post_process_styles

# If model has #{name}_meta column we getting sizes of processed
# thumbnails and saving it to #{name}_meta column.
def post_process_styles
original_post_process_styles

if instance.respond_to?(:"#{name}_meta=")
meta = {}

@queued_for_write.each do |style, file|
geo = Geometry.from_file file
meta[style] = {:width => geo.width.to_i, :height => geo.height.to_i, :size => File.size(file) }
end

instance_write(:meta, ActiveSupport::Base64.encode64(Marshal.dump(meta)))
end
end

# Meta access methods
[:width, :height, :size].each do |meth|
define_method(meth) do |*args|
style = args.first || default_style
meta_read(style, meth)
end
end

def image_size(style = default_style)
"#{width(style)}x#{height(style)}"
end

private
def meta_read(style, item)
if instance.respond_to?(:"#{name}_meta")
if meta = Marshal.load(ActiveSupport::Base64.decode64(instance_read(:meta)))
meta.key?(style) ? meta[style][item] : nil
end
end
end
end
end
19 changes: 19 additions & 0 deletions paperclip-meta.gemspec
@@ -0,0 +1,19 @@
include_files = ["README*", "LICENSE", "Rakefile", "init.rb", "{lib,test}/**/*"].map do |glob|
Dir[glob]
end.flatten

spec = Gem::Specification.new do |s|
s.name = "paperclip-meta"
s.version = "0.1"
s.author = "Alexey Bondar"
s.email = "y8@ya.ru"
s.homepage = "http://github.com/y8/paperclip-meta"
s.description = "Add width, height and size methods to paperclip thumbnails"
s.summary = "Thumbnail dimensions for paperclip"
s.platform = Gem::Platform::RUBY
s.files = include_files
s.require_path = "lib"
s.rubyforge_project = "paperclip-meta"
s.has_rdoc = false
s.add_dependency 'paperclip'
end

0 comments on commit 1cb5f21

Please sign in to comment.