norman-has_image 0.2.3 → 0.3.0
Sign up to get free protection for your applications and to get access to all the features.
- data/CHANGELOG +12 -0
- data/README.textile +193 -0
- data/Rakefile +3 -1
- data/init.rb +1 -1
- data/lib/has_image.rb +152 -104
- data/lib/has_image/processor.rb +58 -31
- data/lib/has_image/storage.rb +72 -45
- data/test/storage_test.rb +44 -2
- data/test_rails/complex_pic_test.rb +42 -0
- data/test_rails/pic_test.rb +72 -9
- data/test_rails/schema.rb +7 -1
- data/test_rails/test_helper.rb +1 -3
- metadata +6 -9
- data/README +0 -161
- data/test_rails/pic.rb +0 -3
data/test_rails/schema.rb
CHANGED
data/test_rails/test_helper.rb
CHANGED
@@ -1,9 +1,8 @@
|
|
1
|
-
$:.unshift(File.dirname(__FILE__) + '/../lib')
|
2
|
-
|
3
1
|
ENV['RAILS_ENV'] = 'test'
|
4
2
|
|
5
3
|
require 'test/unit'
|
6
4
|
require File.expand_path(File.join(File.dirname(__FILE__), '/../../../../config/environment.rb'))
|
5
|
+
$LOAD_PATH.unshift(File.dirname(__FILE__) + '/../lib')
|
7
6
|
require 'active_record/fixtures'
|
8
7
|
require 'action_controller/test_process'
|
9
8
|
|
@@ -51,4 +50,3 @@ class Test::Unit::TestCase #:nodoc:
|
|
51
50
|
self.use_instantiated_fixtures = false
|
52
51
|
|
53
52
|
end
|
54
|
-
require 'pic'
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: norman-has_image
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.3.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Norman Clarke
|
@@ -9,7 +9,7 @@ autorequire:
|
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
11
|
|
12
|
-
date: 2008-10-
|
12
|
+
date: 2008-10-22 00:00:00 -07:00
|
13
13
|
default_executable:
|
14
14
|
dependencies:
|
15
15
|
- !ruby/object:Gem::Dependency
|
@@ -27,15 +27,13 @@ executables: []
|
|
27
27
|
|
28
28
|
extensions: []
|
29
29
|
|
30
|
-
extra_rdoc_files:
|
31
|
-
|
32
|
-
- CHANGELOG
|
33
|
-
- FAQ
|
30
|
+
extra_rdoc_files: []
|
31
|
+
|
34
32
|
files:
|
35
33
|
- CHANGELOG
|
36
34
|
- FAQ
|
37
35
|
- MIT-LICENSE
|
38
|
-
- README
|
36
|
+
- README.textile
|
39
37
|
- init.rb
|
40
38
|
- lib/has_image.rb
|
41
39
|
- lib/has_image/processor.rb
|
@@ -47,7 +45,6 @@ homepage: http://randomba.org
|
|
47
45
|
post_install_message:
|
48
46
|
rdoc_options:
|
49
47
|
- --main
|
50
|
-
- README
|
51
48
|
- --inline-source
|
52
49
|
- --line-numbers
|
53
50
|
require_paths:
|
@@ -76,8 +73,8 @@ test_files:
|
|
76
73
|
- test_rails/fixtures/bad_image.jpg
|
77
74
|
- test_rails/fixtures/image.jpg
|
78
75
|
- test_rails/fixtures/image.png
|
79
|
-
- test_rails/pic.rb
|
80
76
|
- test_rails/pic_test.rb
|
77
|
+
- test_rails/complex_pic_test.rb
|
81
78
|
- test_rails/schema.rb
|
82
79
|
- test_rails/test_helper.rb
|
83
80
|
- test/processor_test.rb
|
data/README
DELETED
@@ -1,161 +0,0 @@
|
|
1
|
-
= HasImage[http://github.com/norman/has_image] -- Image attachment gem/plugin for Ruby on Rails
|
2
|
-
|
3
|
-
HasImage[http://github.com/norman/has_image] was created as a smaller,
|
4
|
-
simpler, lighter alternative to
|
5
|
-
attachment_fu[http://github.com/technoweenie/attachment_fu] for applications
|
6
|
-
that need to handle uploaded images.
|
7
|
-
|
8
|
-
It creates only one database record per image, requires only one column in
|
9
|
-
your model, and creates great-looking fixed-dimension thumbnails by using
|
10
|
-
{ImageMagick's}[http://www.imagemagick.org/]
|
11
|
-
resize[http://www.imagemagick.org/script/command-line-options.php#resize],
|
12
|
-
crop[http://www.imagemagick.org/script/command-line-options.php#crop] and
|
13
|
-
gravity[http://www.imagemagick.org/script/command-line-options.php#gravity]
|
14
|
-
functions.
|
15
|
-
|
16
|
-
Some typical use cases are: websites that want to create photo galleries with
|
17
|
-
fixed-dimension thumbnails, or that want to store user profile pictures
|
18
|
-
without creating a separate model for the images.
|
19
|
-
|
20
|
-
It supports only filesystem storage, and uses only MiniMagick[http://github.com/probablycorey/mini_magick] to process
|
21
|
-
images. However, the codebase is very small, simple, readable, and hackable.
|
22
|
-
So it should be easy to modify or enhance its functionality with different
|
23
|
-
storage or processor options.
|
24
|
-
|
25
|
-
== Another image attachment library? Why?
|
26
|
-
|
27
|
-
<em>The three chief virtues of a programmer are: Laziness, Impatience and Hubris.</em> - {Larry Wall}[http://en.wikipedia.org/wiki/Larry_Wall]
|
28
|
-
|
29
|
-
Attachment_fu is too large and general for some of the places I want to use
|
30
|
-
images. I sometimes found myself writing more code to hack attachment_fu than
|
31
|
-
it took to create this gem. In fact, most of the code here has been plucked
|
32
|
-
from my various projects that use attachment_fu.
|
33
|
-
|
34
|
-
The other image attachment libraries I found fell short of my needs for
|
35
|
-
various other reasons, so I decided to roll my own.
|
36
|
-
|
37
|
-
== Examples
|
38
|
-
|
39
|
-
Point-and-drool use case. It's probably not what you want, but it may be
|
40
|
-
useful for bootstrapping.
|
41
|
-
|
42
|
-
class Member < ActiveRecord::Base
|
43
|
-
has_image
|
44
|
-
end
|
45
|
-
|
46
|
-
Single image, no thumbnails, with some size limits:
|
47
|
-
|
48
|
-
class Picture < ActiveRecord::Base
|
49
|
-
has_image :resize_to => "200x200",
|
50
|
-
:max_size => 3.megabytes,
|
51
|
-
:min_size => 4.kilobytes
|
52
|
-
end
|
53
|
-
|
54
|
-
Image with some thumbnails:
|
55
|
-
|
56
|
-
class Photo < ActiveRecord::Base
|
57
|
-
has_image :resize_to => "640x480",
|
58
|
-
:thumbnails => {
|
59
|
-
:square => "200x200",
|
60
|
-
:medium => "320x240"
|
61
|
-
},
|
62
|
-
:max_size => 3.megabytes,
|
63
|
-
:min_size => 4.kilobytes
|
64
|
-
end
|
65
|
-
|
66
|
-
It also provides a view helper to make displaying the images extremely simple:
|
67
|
-
|
68
|
-
<%= image_tag_for(@photo) # show the full-sized image %>
|
69
|
-
<%= image_tag_for(@photo, :thumb => :square) # show the square thumbnail %>
|
70
|
-
|
71
|
-
The image_tag_for helper calls Rails' image_tag, so you can pass in all the
|
72
|
-
regular options to set the alt property, CSS class, etc:
|
73
|
-
|
74
|
-
<%= image_tag_for(@photo, :alt => "my cool picture", :class => "photo") %>
|
75
|
-
|
76
|
-
Setting up forms for has_image is simple, too:
|
77
|
-
|
78
|
-
<% form_for(@photo, :html => {:multipart => true}) do |f| %>
|
79
|
-
<p>
|
80
|
-
<%= f.label :image_data %>
|
81
|
-
<%= f.file_field :image_data %>
|
82
|
-
</p>
|
83
|
-
<p>
|
84
|
-
<%= f.submit %>
|
85
|
-
</p>
|
86
|
-
<% end %>
|
87
|
-
|
88
|
-
== Getting it
|
89
|
-
|
90
|
-
Has image can be installed as a gem, or as a Rails plugin. Gem installation
|
91
|
-
is easiest, and recommended:
|
92
|
-
|
93
|
-
gem install has_image
|
94
|
-
|
95
|
-
and add
|
96
|
-
|
97
|
-
require 'has_image'
|
98
|
-
|
99
|
-
to your environment.rb file.
|
100
|
-
|
101
|
-
Alternatively, you can install it as a Rails plugin:
|
102
|
-
|
103
|
-
./script plugin install git://github.com/norman/has_image.git
|
104
|
-
|
105
|
-
Rails versions before 2.1 do not support plugin installation using Git, so if
|
106
|
-
you're on 2.0 (or earlier), then please install the gem rather than the
|
107
|
-
plugin.
|
108
|
-
|
109
|
-
Then, make sure the model has a column named "has_image_file."
|
110
|
-
|
111
|
-
{Git repository}[http://github.com/norman/has_image]:
|
112
|
-
|
113
|
-
git://github.com/norman/has_image.git
|
114
|
-
|
115
|
-
== Hacking it
|
116
|
-
|
117
|
-
Don't like the way it makes images? Want to pipe the images through some
|
118
|
-
{crazy fast seam carving library written in
|
119
|
-
OCaml}[http://eigenclass.org/hiki/seam-carving-in-ocaml], or watermark them
|
120
|
-
with your corporate logo? Happiness is just a monkey-patch[http://en.wikipedia.org/wiki/Monkey_patch] away:
|
121
|
-
|
122
|
-
module HasImage
|
123
|
-
class Processor
|
124
|
-
def resize_image(size)
|
125
|
-
# your new-and-improved thumbnailer code goes here.
|
126
|
-
end
|
127
|
-
end
|
128
|
-
end
|
129
|
-
|
130
|
-
HasImage[http://github.com/norman/has_image] follows a philosophy of "{skinny
|
131
|
-
model, fat plugin}[http://weblog.jamisbuck.org/2006/10/18/skinny-controller-fat-model]."
|
132
|
-
This means that it tries to pollute your ActiveRecord model with as little
|
133
|
-
functionality as possible, so that in a sense, the model is acts like a
|
134
|
-
"controller" and the plugin like a "model" as regards the image handling
|
135
|
-
functionality. This makes it easier to test, hack, and reuse, because the
|
136
|
-
storage and processing functionality is largely independent of your model, and
|
137
|
-
of Rails.
|
138
|
-
|
139
|
-
My goal for HasImage[http://github.com/norman/has_image] is to keep it very
|
140
|
-
small. If you need *a lot* of functionality that's not here, instead of patching
|
141
|
-
this code, you will likely be better off using
|
142
|
-
attachment_fu[http://github.com/technoweenie/attachment_fu], which is much
|
143
|
-
more powerful, but also more complex.
|
144
|
-
|
145
|
-
== Bugs
|
146
|
-
|
147
|
-
Please report them on Lighthouse[http://randomba.lighthouseapp.com/projects/14674-has_image].
|
148
|
-
|
149
|
-
At the time of writing (July 2008),
|
150
|
-
HasImage[http://github.com/norman/has_image] is in its infancy. Your patches,
|
151
|
-
bug reports and withering criticism are more than welcome.
|
152
|
-
|
153
|
-
== Links
|
154
|
-
|
155
|
-
* {HasImage RDocs}[http://randomba.org/projects/has_image] (regenerated nightly)
|
156
|
-
* {HasImage on GitHub}[http://github.com/norman/has_image]
|
157
|
-
* {HasImage on Rubyforge}[http://rubyforge.org/projects/has-image/]
|
158
|
-
* {HasImage on Lighthouse}[http://randomba.lighthouseapp.com/projects/14674-has_image]
|
159
|
-
|
160
|
-
Copyright (c) 2008 {Norman Clarke}[mailto:norman@randomba.org], released under
|
161
|
-
the MIT license
|
data/test_rails/pic.rb
DELETED