Paperclip-Autosizer 0.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/MIT-LICENSE +20 -0
- data/README.rdoc +94 -0
- data/Rakefile +37 -0
- data/lib/paperclip_autosizer.rb +40 -0
- data/lib/paperclip_processors/autosize.rb +9 -0
- data/lib/photo.rb +29 -0
- data/lib/tasks/paperclip_autosizer.rake +4 -0
- data/test/test_helper.rb +3 -0
- data/test/test_test.rb +8 -0
- metadata +59 -0
data/MIT-LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2010 [name of plugin creator]
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.rdoc
ADDED
@@ -0,0 +1,94 @@
|
|
1
|
+
=Paperclip Autosizer
|
2
|
+
|
3
|
+
Paperclip Autosizer is a simple system to save "WxH" information to the database.
|
4
|
+
The system works entirely in concert with the Paperclip API and makes no additional calls
|
5
|
+
to imagemagick. This allows the programmer to use a stock paperclip gem or plugin
|
6
|
+
and take advantage of upgrades to the most current version. It also runs without
|
7
|
+
significant changes to performance or memory footprint. Compatibility problems
|
8
|
+
should therefore be low and I hope this will be a useful safe solution for forward
|
9
|
+
looking projects. These were at least my specific concerns with other well written
|
10
|
+
but forked solutions and what drove me to write this.
|
11
|
+
|
12
|
+
The system operates by sending the original image dimensions from a slight subclass of
|
13
|
+
the standard thumbnail processor to the model. Final images sizes are obtained
|
14
|
+
through pure calculation. It sounds strange to calculate and resize an image in
|
15
|
+
IM and then calculate that again in the model, but it's actually the best solution
|
16
|
+
I found to this problem. Using a few lines of already preloaded code to allocate
|
17
|
+
space for a few integers and floats, do two or three multiplications and divisions
|
18
|
+
and retain a single non-destroyed variable to save to the database is not high overhead.
|
19
|
+
This is much faster than calling out to identify, more compatible across server
|
20
|
+
environments and easy. Furthermore thanks to the elegant and little used paperclip
|
21
|
+
api options, the autosizer only runs after a post_processing event has succeeded
|
22
|
+
and the sizes are written to the database during the normal rails model save method.
|
23
|
+
Columns are auto-detected allowing for optional autosizing of each attached_file or
|
24
|
+
specific style of attachment completely at the programmers discretion.
|
25
|
+
|
26
|
+
This is my first public software so please feel free to criticize. I know the syntax
|
27
|
+
is pretty fast and I will make it human friendly when I get the chance.
|
28
|
+
|
29
|
+
Author:: Andrew Eisberg
|
30
|
+
Copyright:: Copyright (c) 2009 Andrew Eisberg
|
31
|
+
License:: MIT License (http://www.opensource.org/licenses/mit-license.php)
|
32
|
+
|
33
|
+
==Limitations
|
34
|
+
|
35
|
+
Currently only the '>' geometry flag is written as that's what I use 99% of the time
|
36
|
+
when I also want a size. It would be quite easy to write in additional methods to
|
37
|
+
support other IM geometries. These things are really just a few lines and not the
|
38
|
+
rocket science part of IM.
|
39
|
+
|
40
|
+
==Usage
|
41
|
+
|
42
|
+
To use Autosizer, simply copy the paperclip_autosizer.rb file to the models folder
|
43
|
+
and the paperclip_processors folder to the lib folder. Make the model you wish to
|
44
|
+
autosize a subclass of the PaperclipAutosizer class. Tell paperclip to use the
|
45
|
+
autosize processor (a 100% compatible subclass of the default thumbnail processor)
|
46
|
+
and add the line +after_post_process :autosize_attached_files+ after the
|
47
|
+
has_attached_file declarations:
|
48
|
+
|
49
|
+
class User < PaperclipAutosizer
|
50
|
+
has_attached_file :avatar, :styles => { :thumb => "100x100>" },
|
51
|
+
:processors => [:autosize]
|
52
|
+
has_attached_file :center, :styles => { :large => "500x500>" },
|
53
|
+
:processors => [:autosize]
|
54
|
+
after_post_process :autosize_attached_files
|
55
|
+
end
|
56
|
+
|
57
|
+
This is sufficient to handle multiple attached_files. If only one attachment of
|
58
|
+
many is being autosized, the stricter callback +after_<attachment>_post_process+
|
59
|
+
can be used instead for greater efficiency.
|
60
|
+
|
61
|
+
In the migration for the model, simply add columns for each attached_file/style
|
62
|
+
combination you wish to retain sizes for. PaperclipAutosizer will see them and
|
63
|
+
ignore any saved attachments it doesn't see columns for. Columns must conform
|
64
|
+
to the format +t.string :(attachment)_(style)_size+ to be recognized by the model.
|
65
|
+
|
66
|
+
t.string :avatar_thumb_size
|
67
|
+
t.string :center_large_size
|
68
|
+
|
69
|
+
The sizes can then be easily called in the view.
|
70
|
+
|
71
|
+
image_tag(@user.avatar.url(:thumb), :size => @user.avatar_thumb_size)
|
72
|
+
image_tag(@user.center.url(:large), :size => @user.center_large_size)
|
73
|
+
|
74
|
+
|
75
|
+
==TextMate
|
76
|
+
|
77
|
+
Here is a textmate snippet you can use if you want. If you're not used to hacking
|
78
|
+
stuff into your TM, here's how to do it. Make a new bundle with your name. Then a new
|
79
|
+
snippet and paste this in. Give it a tab trigger of 'autosizer' or what you want and
|
80
|
+
a scope selector of 'source.ruby.rails'. Then just type autosizer and tab in a model.
|
81
|
+
|
82
|
+
# Paperclip Method for ${1/[[:alpha:]]+|(_)/(?1::\u$0)/g} Attachment with Autosizing
|
83
|
+
has_attached_file :${1:attachment_name}, :styles => { :${2:large} => "${3:750}x${4:750}>",
|
84
|
+
:${5:thumb} => "${6:150}x${7:150}>" },
|
85
|
+
:processors => [:autosize],
|
86
|
+
:url => "/system/auto_load/${1}s/:id/:style/:basename.:extension",
|
87
|
+
:path => ":rails_root/public/system/auto_load/:attachment/:id/:style/:basename.:extension",
|
88
|
+
:default_url => "/system/auto_load/defaults/default_:style.png"
|
89
|
+
|
90
|
+
validates_attachment_size :${1}, :less_than => 1.megabytes
|
91
|
+
validates_attachment_content_type :${1}, :content_type => ['image/jpeg', 'image/png']
|
92
|
+
after_${1}_post_process :autosize_attached_files
|
93
|
+
|
94
|
+
$0
|
data/Rakefile
ADDED
@@ -0,0 +1,37 @@
|
|
1
|
+
#!/usr/bin/env rake
|
2
|
+
begin
|
3
|
+
require 'bundler/setup'
|
4
|
+
rescue LoadError
|
5
|
+
puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
|
6
|
+
end
|
7
|
+
begin
|
8
|
+
require 'rdoc/task'
|
9
|
+
rescue LoadError
|
10
|
+
require 'rdoc/rdoc'
|
11
|
+
require 'rake/rdoctask'
|
12
|
+
RDoc::Task = Rake::RDocTask
|
13
|
+
end
|
14
|
+
|
15
|
+
RDoc::Task.new(:rdoc) do |rdoc|
|
16
|
+
rdoc.rdoc_dir = 'rdoc'
|
17
|
+
rdoc.title = 'PaperclipAutosizer'
|
18
|
+
rdoc.options << '--line-numbers'
|
19
|
+
rdoc.rdoc_files.include('README.rdoc')
|
20
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
21
|
+
end
|
22
|
+
|
23
|
+
|
24
|
+
|
25
|
+
Bundler::GemHelper.install_tasks
|
26
|
+
|
27
|
+
require 'rake/testtask'
|
28
|
+
|
29
|
+
Rake::TestTask.new(:test) do |t|
|
30
|
+
t.libs << 'lib'
|
31
|
+
t.libs << 'test'
|
32
|
+
t.pattern = 'test/**/*_test.rb'
|
33
|
+
t.verbose = false
|
34
|
+
end
|
35
|
+
|
36
|
+
|
37
|
+
task :default => :test
|
@@ -0,0 +1,40 @@
|
|
1
|
+
class PaperclipAutosizer < ActiveRecord::Base
|
2
|
+
self.abstract_class = true
|
3
|
+
|
4
|
+
attr_writer :autosizer_attachment_name, :autosizer_original_file_geometry
|
5
|
+
|
6
|
+
private
|
7
|
+
def autosizer_attachment
|
8
|
+
send(@autosizer_attachment_name)
|
9
|
+
end
|
10
|
+
|
11
|
+
def autosize_attached_files
|
12
|
+
styles_to_autosize.each_pair do |style, column_for_style|
|
13
|
+
send(:"#{column_for_style}=", calculate_size_of_reduced_image(style))
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
def styles_to_autosize
|
18
|
+
autosizer_attachment.styles.keys.inject({}) do |accumulator, style|
|
19
|
+
column_for_style = [@autosizer_attachment_name, style.to_s, "size"].join("_")
|
20
|
+
if self.class.column_names.include?(column_for_style)
|
21
|
+
accumulator[style] = column_for_style
|
22
|
+
end
|
23
|
+
accumulator
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
def calculate_size_of_reduced_image(style)
|
28
|
+
target_width, target_height = autosizer_attachment.styles[style][:geometry].split("x").collect{|x| x.to_f}
|
29
|
+
width, height = @autosizer_original_file_geometry.width, @autosizer_original_file_geometry.height
|
30
|
+
original_ratio = width.to_f / height.to_f
|
31
|
+
if (original_ratio <= 1 && target_height < height)
|
32
|
+
width = original_ratio * target_height
|
33
|
+
height = target_height
|
34
|
+
elsif (target_width < width)
|
35
|
+
width = target_width
|
36
|
+
height = (1 / original_ratio) * target_width
|
37
|
+
end
|
38
|
+
[width.round, height.round].join("x")
|
39
|
+
end
|
40
|
+
end
|
@@ -0,0 +1,9 @@
|
|
1
|
+
module Paperclip
|
2
|
+
class Autosize < Thumbnail
|
3
|
+
def initialize file, options = {}, attachment = nil
|
4
|
+
super
|
5
|
+
attachment.instance.send(:autosizer_attachment_name=, attachment.name.to_s)
|
6
|
+
attachment.instance.send(:autosizer_original_file_geometry=, @current_geometry)
|
7
|
+
end
|
8
|
+
end
|
9
|
+
end
|
data/lib/photo.rb
ADDED
@@ -0,0 +1,29 @@
|
|
1
|
+
class Photo < PaperclipAutosizer
|
2
|
+
|
3
|
+
# Paperclip Method for Photograph Attachment
|
4
|
+
has_attached_file :photograph, :styles => { :large => "750x750>",
|
5
|
+
:thumb => "150x150>" },
|
6
|
+
:processors => [:autosize],
|
7
|
+
:url => "/images/auto_load/photographs/:id/:style/:basename.:extension",
|
8
|
+
:path => ":rails_root/public/images/auto_load/:attachment/:id/:style/:basename.:extension",
|
9
|
+
:default_url => "/images/auto_load/defaults/default_:style.png"
|
10
|
+
|
11
|
+
validates_attachment_size :photograph, :less_than => 1.megabytes
|
12
|
+
validates_attachment_content_type :photograph, :content_type => ['image/jpeg', 'image/png']
|
13
|
+
after_photograph_post_process :autosize_attached_files
|
14
|
+
|
15
|
+
|
16
|
+
|
17
|
+
# Paperclip Method for Micrograph Attachment
|
18
|
+
has_attached_file :micrograph, :styles => { :large => "750x750>",
|
19
|
+
:thumb => "150x150>" },
|
20
|
+
:processors => [:autosize],
|
21
|
+
:url => "/images/auto_load/micrographs/:id/:style/:basename.:extension",
|
22
|
+
:path => ":rails_root/public/images/auto_load/:attachment/:id/:style/:basename.:extension",
|
23
|
+
:default_url => "/images/auto_load/defaults/default_:style.png"
|
24
|
+
|
25
|
+
validates_attachment_size :micrograph, :less_than => 1.megabytes
|
26
|
+
validates_attachment_content_type :micrograph, :content_type => ['image/jpeg', 'image/png']
|
27
|
+
after_micrograph_post_process :autosize_attached_files
|
28
|
+
|
29
|
+
end
|
data/test/test_helper.rb
ADDED
data/test/test_test.rb
ADDED
metadata
ADDED
@@ -0,0 +1,59 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: Paperclip-Autosizer
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Andrew Eisberg
|
9
|
+
- Ben Woosley
|
10
|
+
autorequire:
|
11
|
+
bindir: bin
|
12
|
+
cert_chain: []
|
13
|
+
date: 2011-09-11 00:00:00.000000000Z
|
14
|
+
dependencies: []
|
15
|
+
description: Insert PaperclipAutosizer description.
|
16
|
+
email:
|
17
|
+
executables: []
|
18
|
+
extensions: []
|
19
|
+
extra_rdoc_files: []
|
20
|
+
files:
|
21
|
+
- lib/paperclip_autosizer.rb
|
22
|
+
- lib/paperclip_processors/autosize.rb
|
23
|
+
- lib/photo.rb
|
24
|
+
- lib/tasks/paperclip_autosizer.rake
|
25
|
+
- MIT-LICENSE
|
26
|
+
- Rakefile
|
27
|
+
- README.rdoc
|
28
|
+
- test/test_helper.rb
|
29
|
+
- test/test_test.rb
|
30
|
+
homepage:
|
31
|
+
licenses: []
|
32
|
+
post_install_message:
|
33
|
+
rdoc_options: []
|
34
|
+
require_paths:
|
35
|
+
- lib
|
36
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
37
|
+
none: false
|
38
|
+
requirements:
|
39
|
+
- - ! '>='
|
40
|
+
- !ruby/object:Gem::Version
|
41
|
+
version: '0'
|
42
|
+
segments:
|
43
|
+
- 0
|
44
|
+
hash: -1675942464531920957
|
45
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
46
|
+
none: false
|
47
|
+
requirements:
|
48
|
+
- - ! '>='
|
49
|
+
- !ruby/object:Gem::Version
|
50
|
+
version: '0'
|
51
|
+
requirements: []
|
52
|
+
rubyforge_project:
|
53
|
+
rubygems_version: 1.8.8
|
54
|
+
signing_key:
|
55
|
+
specification_version: 3
|
56
|
+
summary: Insert PaperclipAutosizer summary.
|
57
|
+
test_files:
|
58
|
+
- test/test_helper.rb
|
59
|
+
- test/test_test.rb
|