has_image 0.3.0 → 0.4.0
Sign up to get free protection for your applications and to get access to all the features.
- data/Changelog.md +100 -0
- data/MIT-LICENSE +15 -17
- data/README.md +159 -0
- data/Rakefile +6 -38
- data/lib/has_image.rb +31 -12
- data/lib/has_image/processor.rb +17 -18
- data/lib/has_image/railtie.rb +7 -0
- data/lib/has_image/storage.rb +31 -31
- data/lib/has_image/version.rb +3 -0
- data/lib/has_image/view_helpers.rb +3 -6
- data/{test_rails → test}/complex_pic_test.rb +10 -5
- data/{test_rails → test}/fixtures/bad_image.jpg +0 -0
- data/{test_rails → test}/fixtures/image.jpg +0 -0
- data/{test_rails → test}/fixtures/image.png +0 -0
- data/{test_rails → test}/pic_test.rb +4 -3
- data/test/processor_test.rb +9 -10
- data/test/storage_test.rb +28 -29
- data/test/test_helper.rb +47 -0
- metadata +92 -35
- data/CHANGELOG +0 -72
- data/FAQ +0 -25
- data/README.textile +0 -193
- data/init.rb +0 -1
- data/test_rails/database.yml +0 -3
- data/test_rails/schema.rb +0 -15
- data/test_rails/test_helper.rb +0 -52
data/FAQ
DELETED
@@ -1,25 +0,0 @@
|
|
1
|
-
= Frequently Asked Questions
|
2
|
-
|
3
|
-
HasImage is too new to have many FAQ items yet. {Ask
|
4
|
-
me}[mailto:norman@randomba.org] and they will be included; this is a work in
|
5
|
-
progress.
|
6
|
-
|
7
|
-
= How do I validate the mime type of my uploaded images?
|
8
|
-
|
9
|
-
You don't. Rather than examine the mime type, HasImage runs the "identify"
|
10
|
-
command on the file to determine if it is processable by ImageMagick, and if
|
11
|
-
it is, converts it to the format you specify, which defaults to JPEG.
|
12
|
-
|
13
|
-
This is better than checking for mime types, because your users may upload
|
14
|
-
exotic image types that you didn't even realize would work, such as Truevision
|
15
|
-
Targa images, or Seattle Film Works files.
|
16
|
-
|
17
|
-
If you wish to give users a list of file types they can upload, a good start
|
18
|
-
would be jpeg, png, bmp, and maybe gif and ttf if your installation of
|
19
|
-
ImageMagick understands them. You can find out what image types your
|
20
|
-
ImageMagick understands by running:
|
21
|
-
|
22
|
-
identify -list format
|
23
|
-
|
24
|
-
Ideally, if your users just upload files that "look like" images on their
|
25
|
-
computers, it HasImage should "just work."
|
data/README.textile
DELETED
@@ -1,193 +0,0 @@
|
|
1
|
-
h1. "HasImage":http://github.com/norman/has_image
|
2
|
-
|
3
|
-
Image attachment gem/plugin for Ruby on Rails
|
4
|
-
|
5
|
-
HasImage allows Ruby on Rails applications to have attached images. It is very
|
6
|
-
small and lightweight: it only requires one column in your model to store the
|
7
|
-
uploaded image's file name.
|
8
|
-
|
9
|
-
HasImage was created as a smaller, simpler, lighter alternative to
|
10
|
-
"attachment_fu":http://github.com/technoweenie/attachment_fu for applications
|
11
|
-
that need to handle uploaded images.
|
12
|
-
|
13
|
-
It is, by design, very simplistic: It only supports using a filesystem for
|
14
|
-
storage, and only supports
|
15
|
-
"MiniMagick":http://github.com/probablycorey/mini_magick as an image
|
16
|
-
processor. However, its code is very small, clean and hackable, so adding
|
17
|
-
support for other backends or processors should be fairly easy.
|
18
|
-
|
19
|
-
Some typical use cases are: websites that want to create photo galleries with
|
20
|
-
fixed-dimension thumbnails, or that want to store user profile pictures
|
21
|
-
without creating a separate model for the images.
|
22
|
-
|
23
|
-
It creates only one database record per image, and uses ImageMagick's
|
24
|
-
"crop":http://www.imagemagick.org/script/command-line-options.php#crop and
|
25
|
-
"center gravity":http://www.imagemagick.org/script/command-line-options.php#gravity
|
26
|
-
functions to produce thumbnails that generally look acceptable, unless the
|
27
|
-
image is a panorama, or the subject matter is close to one of the margins,
|
28
|
-
etc. For most sites where people upload pictures of themselves the generated
|
29
|
-
thumbnails will look good almost all the time.
|
30
|
-
|
31
|
-
h2. Another image attachment library? Why?
|
32
|
-
|
33
|
-
<blockquote>
|
34
|
-
The three chief virtues of a programmer are: Laziness, Impatience and Hubris. - "Larry Wall":http://en.wikipedia.org/wiki/Larry_Wall
|
35
|
-
</blockquote>
|
36
|
-
|
37
|
-
Attachment_fu is too large and general for some of the places I want to use
|
38
|
-
images. I sometimes found myself writing more code to hack attachment_fu than
|
39
|
-
it took to create this gem. In fact, most of the code here has been plucked
|
40
|
-
from my various projects that use attachment_fu.
|
41
|
-
|
42
|
-
The other image attachment libraries I found fell short of my needs for
|
43
|
-
various other reasons, so I decided to roll my own.
|
44
|
-
|
45
|
-
h2. HasImage vs. attachment_fu
|
46
|
-
|
47
|
-
h3. Advantages
|
48
|
-
|
49
|
-
* Simpler, smaller, more easily hackable codebase - and specialized for
|
50
|
-
images only.
|
51
|
-
* Installable via Ruby Gems. This makes version dependencies easy when using
|
52
|
-
Rails 2.1.
|
53
|
-
* Creates only one database record per image.
|
54
|
-
* Has built-in facilities for making distortion-free, fixed-size thumbnails.
|
55
|
-
* Doesn't regenerate the thumbnails every time you save your model. This means
|
56
|
-
you can easily use it, for example, inside a Member model to store member
|
57
|
-
avatars.
|
58
|
-
|
59
|
-
h3. Disadvantages
|
60
|
-
|
61
|
-
* Doesn't save image dimensions in the database. However, if you're using
|
62
|
-
fixed-sized images, you can read the size from MyModel.thumbnails[:my_size].
|
63
|
-
* No support for AWS or DBFile storage, only filesystem.
|
64
|
-
* Only supports "MiniMagick":http://github.com/probablycorey/mini_magick/tree as an image processor, no RMagick, GD, CoreImage, etc.
|
65
|
-
* No support for anything other than image attachments.
|
66
|
-
* Not as popular as attachment_fu, but then again if you use it you'll be cooler (until HasImage becomes popular).
|
67
|
-
|
68
|
-
h2. Examples
|
69
|
-
|
70
|
-
Point-and-drool use case. It's probably not what you want, but it may be
|
71
|
-
useful for bootstrapping.
|
72
|
-
|
73
|
-
class Member < ActiveRecord::Base
|
74
|
-
has_image
|
75
|
-
end
|
76
|
-
|
77
|
-
Single image, no thumbnails, with some size limits:
|
78
|
-
|
79
|
-
class Picture < ActiveRecord::Base
|
80
|
-
has_image :resize_to => "200x200",
|
81
|
-
:max_size => 3.megabytes,
|
82
|
-
:min_size => 4.kilobytes
|
83
|
-
end
|
84
|
-
|
85
|
-
Image with some thumbnails:
|
86
|
-
|
87
|
-
class Photo < ActiveRecord::Base
|
88
|
-
has_image :resize_to => "640x480",
|
89
|
-
:thumbnails => {
|
90
|
-
:square => "200x200",
|
91
|
-
:medium => "320x240"
|
92
|
-
},
|
93
|
-
:max_size => 3.megabytes,
|
94
|
-
:min_size => 4.kilobytes
|
95
|
-
end
|
96
|
-
|
97
|
-
It also provides a view helper to make displaying the images extremely simple:
|
98
|
-
|
99
|
-
<%= image_tag_for(@photo) # show the full-sized image %>
|
100
|
-
<%= image_tag_for(@photo, :thumb => :square) # show the square thumbnail %>
|
101
|
-
|
102
|
-
The image_tag_for helper calls Rails' image_tag, so you can pass in all the
|
103
|
-
regular options to set the alt property, CSS class, etc:
|
104
|
-
|
105
|
-
<%= image_tag_for(@photo, :alt => "my cool picture", :class => "photo") %>
|
106
|
-
|
107
|
-
Setting up forms for HasImage is simple, too:
|
108
|
-
|
109
|
-
<pre>
|
110
|
-
<% form_for(@photo, :html => {:multipart => true}) do |f| %>
|
111
|
-
<p>
|
112
|
-
<%= f.label :image_data %>
|
113
|
-
<%= f.file_field :image_data %>
|
114
|
-
</p>
|
115
|
-
<p>
|
116
|
-
<%= f.submit %>
|
117
|
-
</p>
|
118
|
-
<% end %>
|
119
|
-
</pre>
|
120
|
-
|
121
|
-
h2. Getting it
|
122
|
-
|
123
|
-
Has image can be installed as a gem, or as a Rails plugin. Gem installation
|
124
|
-
is easiest, and recommended:
|
125
|
-
|
126
|
-
gem install has_image
|
127
|
-
|
128
|
-
and add
|
129
|
-
|
130
|
-
require 'has_image'
|
131
|
-
|
132
|
-
to your environment.rb file.
|
133
|
-
|
134
|
-
Alternatively, you can install it as a Rails plugin:
|
135
|
-
|
136
|
-
./script plugin install git://github.com/norman/has_image.git
|
137
|
-
|
138
|
-
Rails versions before 2.1 do not support plugin installation using Git, so if
|
139
|
-
you're on 2.0 (or earlier), then please install the gem rather than the
|
140
|
-
plugin.
|
141
|
-
|
142
|
-
Then, make sure the model has a column named "has_image_file."
|
143
|
-
|
144
|
-
"Git repository":http://github.com/norman/has_image: git://github.com/norman/has_image.git
|
145
|
-
|
146
|
-
h2. Hacking it
|
147
|
-
|
148
|
-
Don't like the way it makes images? Want to pipe the images through some
|
149
|
-
"crazy fast seam carving library written in OCaml":http://eigenclass.org/hiki/seam-carving-in-ocaml, or watermark them
|
150
|
-
with your corporate logo? Happiness is just a "monkey-patch":http://en.wikipedia.org/wiki/Monkey_patch away:
|
151
|
-
|
152
|
-
module HasImage
|
153
|
-
class Processor
|
154
|
-
def resize_image(size)
|
155
|
-
# your new-and-improved thumbnailer code goes here.
|
156
|
-
end
|
157
|
-
end
|
158
|
-
end
|
159
|
-
|
160
|
-
HasImage follows a philosophy of ""skinny model, fat plugin":http://weblog.jamisbuck.org/2006/10/18/skinny-controller-fat-model."
|
161
|
-
This means that it tries to pollute your ActiveRecord model with as little
|
162
|
-
functionality as possible, so that in a sense, the model is acts like a
|
163
|
-
"controller" and the plugin like a "model" as regards the image handling
|
164
|
-
functionality. This makes it easier to test, hack, and reuse, because the
|
165
|
-
storage and processing functionality is largely independent of your model, and
|
166
|
-
of Rails.
|
167
|
-
|
168
|
-
My goal for HasImage is to keep it very small. If you need *a lot* of
|
169
|
-
functionality that's not here, instead of patching this code, you will likely
|
170
|
-
be better off using attachment_fu, which is more powerful, but also more
|
171
|
-
complex.
|
172
|
-
|
173
|
-
h2. Bugs
|
174
|
-
|
175
|
-
Please report them on "Lighthouse":http://randomba.lighthouseapp.com/projects/14674-has_image.
|
176
|
-
|
177
|
-
Copyright (c) 2008 "Norman Clarke":mailto:norman@randomba.org, released under
|
178
|
-
the MIT license
|
179
|
-
|
180
|
-
h2. Is anyone using this thing?
|
181
|
-
|
182
|
-
HasImage powers "FotoBlog":http://fotoblog.ciudad.com.ar, one of the largest
|
183
|
-
Rails sites in Argentina. If you're using it in your project let me know and
|
184
|
-
I'll add your link here.
|
185
|
-
|
186
|
-
h2. Acknowledgements
|
187
|
-
|
188
|
-
I'd like to thank the following contributors:
|
189
|
-
|
190
|
-
* "Adrian Mugnolo":http://github.com/xymbol
|
191
|
-
* "Gerrit Keiser":http://github.com/gerrit
|
192
|
-
* the folks from "Tricycle Developments":http://github.com/tricycle
|
193
|
-
* "Dima Sabanin":http://github.com/railsmonk
|
data/init.rb
DELETED
@@ -1 +0,0 @@
|
|
1
|
-
require 'has_image'
|
data/test_rails/database.yml
DELETED
data/test_rails/schema.rb
DELETED
@@ -1,15 +0,0 @@
|
|
1
|
-
ActiveRecord::Schema.define(:version => 1) do
|
2
|
-
|
3
|
-
create_table "pics", :force => true do |t|
|
4
|
-
t.string :has_image_file
|
5
|
-
t.datetime :created_at
|
6
|
-
t.datetime :updated_at
|
7
|
-
end
|
8
|
-
|
9
|
-
create_table 'complex_pics', :force => true do |t|
|
10
|
-
t.string :filename
|
11
|
-
t.integer :width, :height
|
12
|
-
t.timestamps
|
13
|
-
end
|
14
|
-
|
15
|
-
end
|
data/test_rails/test_helper.rb
DELETED
@@ -1,52 +0,0 @@
|
|
1
|
-
ENV['RAILS_ENV'] = 'test'
|
2
|
-
|
3
|
-
require 'test/unit'
|
4
|
-
require File.expand_path(File.join(File.dirname(__FILE__), '/../../../../config/environment.rb'))
|
5
|
-
$LOAD_PATH.unshift(File.dirname(__FILE__) + '/../lib')
|
6
|
-
require 'active_record/fixtures'
|
7
|
-
require 'action_controller/test_process'
|
8
|
-
|
9
|
-
config = YAML::load(IO.read(File.dirname(__FILE__) + '/database.yml'))
|
10
|
-
ActiveRecord::Base.logger = Logger.new(File.dirname(__FILE__) + "/debug.log")
|
11
|
-
|
12
|
-
db_adapter = ENV['DB']
|
13
|
-
|
14
|
-
# no db passed, try one of these fine config-free DBs before bombing.
|
15
|
-
db_adapter ||=
|
16
|
-
begin
|
17
|
-
require 'rubygems'
|
18
|
-
require 'sqlite3'
|
19
|
-
'sqlite3'
|
20
|
-
rescue MissingSourceFile
|
21
|
-
begin
|
22
|
-
require 'sqlite'
|
23
|
-
'sqlite'
|
24
|
-
rescue MissingSourceFile
|
25
|
-
end
|
26
|
-
end
|
27
|
-
|
28
|
-
if db_adapter.nil?
|
29
|
-
raise "No DB Adapter selected. Pass the DB= option to pick one, or install Sqlite3 or Sqlite."
|
30
|
-
end
|
31
|
-
|
32
|
-
ActiveRecord::Base.establish_connection(config[db_adapter])
|
33
|
-
|
34
|
-
load(File.dirname(__FILE__) + "/schema.rb")
|
35
|
-
|
36
|
-
Test::Unit::TestCase.fixture_path = File.dirname(__FILE__) + "/fixtures"
|
37
|
-
$LOAD_PATH.unshift(Test::Unit::TestCase.fixture_path)
|
38
|
-
|
39
|
-
class Test::Unit::TestCase #:nodoc:
|
40
|
-
include ActionController::TestProcess
|
41
|
-
def create_fixtures(*table_names)
|
42
|
-
if block_given?
|
43
|
-
Fixtures.create_fixtures(Test::Unit::TestCase.fixture_path, table_names) { yield }
|
44
|
-
else
|
45
|
-
Fixtures.create_fixtures(Test::Unit::TestCase.fixture_path, table_names)
|
46
|
-
end
|
47
|
-
end
|
48
|
-
|
49
|
-
self.use_transactional_fixtures = true
|
50
|
-
self.use_instantiated_fixtures = false
|
51
|
-
|
52
|
-
end
|