locomotive_carrierwave 0.5.0.1.beta1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (40) hide show
  1. data/README.rdoc +532 -0
  2. data/lib/carrierwave/compatibility/paperclip.rb +95 -0
  3. data/lib/carrierwave/locale/en.yml +5 -0
  4. data/lib/carrierwave/mount.rb +376 -0
  5. data/lib/carrierwave/orm/activerecord.rb +36 -0
  6. data/lib/carrierwave/orm/datamapper.rb +37 -0
  7. data/lib/carrierwave/orm/mongoid.rb +36 -0
  8. data/lib/carrierwave/orm/sequel.rb +45 -0
  9. data/lib/carrierwave/processing/image_science.rb +116 -0
  10. data/lib/carrierwave/processing/mini_magick.rb +261 -0
  11. data/lib/carrierwave/processing/rmagick.rb +278 -0
  12. data/lib/carrierwave/sanitized_file.rb +306 -0
  13. data/lib/carrierwave/storage/abstract.rb +33 -0
  14. data/lib/carrierwave/storage/cloud_files.rb +168 -0
  15. data/lib/carrierwave/storage/file.rb +54 -0
  16. data/lib/carrierwave/storage/grid_fs.rb +136 -0
  17. data/lib/carrierwave/storage/right_s3.rb +1 -0
  18. data/lib/carrierwave/storage/s3.rb +249 -0
  19. data/lib/carrierwave/test/matchers.rb +164 -0
  20. data/lib/carrierwave/uploader/cache.rb +148 -0
  21. data/lib/carrierwave/uploader/callbacks.rb +41 -0
  22. data/lib/carrierwave/uploader/configuration.rb +134 -0
  23. data/lib/carrierwave/uploader/default_url.rb +19 -0
  24. data/lib/carrierwave/uploader/download.rb +64 -0
  25. data/lib/carrierwave/uploader/extension_whitelist.rb +38 -0
  26. data/lib/carrierwave/uploader/mountable.rb +39 -0
  27. data/lib/carrierwave/uploader/processing.rb +85 -0
  28. data/lib/carrierwave/uploader/proxy.rb +62 -0
  29. data/lib/carrierwave/uploader/remove.rb +23 -0
  30. data/lib/carrierwave/uploader/rename.rb +62 -0
  31. data/lib/carrierwave/uploader/store.rb +98 -0
  32. data/lib/carrierwave/uploader/url.rb +33 -0
  33. data/lib/carrierwave/uploader/versions.rb +157 -0
  34. data/lib/carrierwave/uploader.rb +45 -0
  35. data/lib/carrierwave/validations/active_model.rb +79 -0
  36. data/lib/carrierwave/version.rb +3 -0
  37. data/lib/carrierwave.rb +101 -0
  38. data/lib/generators/templates/uploader.rb +47 -0
  39. data/lib/generators/uploader_generator.rb +7 -0
  40. metadata +390 -0
@@ -0,0 +1,261 @@
1
+ # encoding: utf-8
2
+
3
+ require 'mini_magick'
4
+
5
+ module CarrierWave
6
+
7
+ ##
8
+ # This module simplifies manipulation with MiniMagick by providing a set
9
+ # of convenient helper methods. If you want to use them, you'll need to
10
+ # require this file:
11
+ #
12
+ # require 'carrierwave/processing/mini_magick'
13
+ #
14
+ # And then include it in your uploader:
15
+ #
16
+ # class MyUploader < CarrierWave::Uploader::Base
17
+ # include CarrierWave::MiniMagick
18
+ # end
19
+ #
20
+ # You can now use the provided helpers:
21
+ #
22
+ # class MyUploader < CarrierWave::Uploader::Base
23
+ # include CarrierWave::MiniMagick
24
+ #
25
+ # process :resize_to_fit => [200, 200]
26
+ # end
27
+ #
28
+ # Or create your own helpers with the powerful manipulate! method. Check
29
+ # out the ImageMagick docs at http://www.imagemagick.org/script/command-line-options.php for more
30
+ # info
31
+ #
32
+ # class MyUploader < CarrierWave::Uploader::Base
33
+ # include CarrierWave::MiniMagick
34
+ #
35
+ # process :do_stuff => 10.0
36
+ #
37
+ # def do_stuff(blur_factor)
38
+ # manipulate! do |img|
39
+ # img = img.sepiatone
40
+ # img = img.auto_orient
41
+ # img = img.radial_blur blur_factor
42
+ # end
43
+ # end
44
+ # end
45
+ #
46
+ # === Note
47
+ #
48
+ # MiniMagick is a mini replacement for RMagick that uses the command line
49
+ # tool "mogrify" for image manipulation.
50
+ #
51
+ # You can find more information here:
52
+ #
53
+ # http://mini_magick.rubyforge.org/
54
+ # and
55
+ # http://github.com/probablycorey/mini_magick/
56
+ #
57
+ #
58
+ module MiniMagick
59
+ extend ActiveSupport::Concern
60
+
61
+ module ClassMethods
62
+ def convert(format)
63
+ process :convert => format
64
+ end
65
+
66
+ def resize_to_limit(width, height)
67
+ process :resize_to_limit => [width, height]
68
+ end
69
+
70
+ def resize_to_fit(width, height)
71
+ process :resize_to_fit => [width, height]
72
+ end
73
+
74
+ def resize_to_fill(width, height)
75
+ process :resize_to_fill => [width, height]
76
+ end
77
+
78
+ def resize_and_pad(width, height, background=:transparent, gravity=::Magick::CenterGravity)
79
+ process :resize_and_pad => [width, height, background, gravity]
80
+ end
81
+ end
82
+
83
+ ##
84
+ # Changes the image encoding format to the given format
85
+ #
86
+ # See http://www.imagemagick.org/script/command-line-options.php#format
87
+ #
88
+ # === Parameters
89
+ #
90
+ # [format (#to_s)] an abreviation of the format
91
+ #
92
+ # === Yields
93
+ #
94
+ # [MiniMagick::Image] additional manipulations to perform
95
+ #
96
+ # === Examples
97
+ #
98
+ # image.convert(:png)
99
+ #
100
+ def convert(format)
101
+ manipulate! do |img|
102
+ img.format(format.to_s.downcase)
103
+ img = yield(img) if block_given?
104
+ img
105
+ end
106
+ end
107
+
108
+ ##
109
+ # Resize the image to fit within the specified dimensions while retaining
110
+ # the original aspect ratio. Will only resize the image if it is larger than the
111
+ # specified dimensions. The resulting image may be shorter or narrower than specified
112
+ # in the smaller dimension but will not be larger than the specified values.
113
+ #
114
+ # === Parameters
115
+ #
116
+ # [width (Integer)] the width to scale the image to
117
+ # [height (Integer)] the height to scale the image to
118
+ #
119
+ # === Yields
120
+ #
121
+ # [MiniMagick::Image] additional manipulations to perform
122
+ #
123
+ def resize_to_limit(width, height)
124
+ manipulate! do |img|
125
+ img.resize "#{width}x#{height}>"
126
+ img = yield(img) if block_given?
127
+ img
128
+ end
129
+ end
130
+
131
+ ##
132
+ # From the RMagick documentation: "Resize the image to fit within the
133
+ # specified dimensions while retaining the original aspect ratio. The
134
+ # image may be shorter or narrower than specified in the smaller dimension
135
+ # but will not be larger than the specified values."
136
+ #
137
+ # See even http://www.imagemagick.org/RMagick/doc/image3.html#resize_to_fit
138
+ #
139
+ # === Parameters
140
+ #
141
+ # [width (Integer)] the width to scale the image to
142
+ # [height (Integer)] the height to scale the image to
143
+ #
144
+ # === Yields
145
+ #
146
+ # [MiniMagick::Image] additional manipulations to perform
147
+ #
148
+ def resize_to_fit(width, height)
149
+ manipulate! do |img|
150
+ img.resize "#{width}x#{height}"
151
+ img = yield(img) if block_given?
152
+ img
153
+ end
154
+ end
155
+
156
+ ##
157
+ # From the RMagick documentation: "Resize the image to fit within the
158
+ # specified dimensions while retaining the aspect ratio of the original
159
+ # image. If necessary, crop the image in the larger dimension."
160
+ #
161
+ # See even http://www.imagemagick.org/RMagick/doc/image3.html#resize_to_fill
162
+ #
163
+ # and
164
+ #
165
+ # http://www.clipclip.org/clips/detail/4365/jerrett-net-»-crop_resized-in-rmagick
166
+ #
167
+ # === Parameters
168
+ #
169
+ # [width (Integer)] the width to scale the image to
170
+ # [height (Integer)] the height to scale the image to
171
+ #
172
+ # === Yields
173
+ #
174
+ # [MiniMagick::Image] additional manipulations to perform
175
+ #
176
+ def resize_to_fill(width, height, gravity = 'Center')
177
+ manipulate! do |img|
178
+ cols, rows = img[:dimensions]
179
+ img.combine_options do |cmd|
180
+ if width != cols || height != rows
181
+ scale = [width/cols.to_f, height/rows.to_f].max
182
+ cols = (scale * (cols + 0.5)).round
183
+ rows = (scale * (rows + 0.5)).round
184
+ cmd.resize "#{cols}x#{rows}"
185
+ end
186
+ cmd.gravity gravity
187
+ cmd.extent "#{width}x#{height}" if cols != width || rows != height
188
+ end
189
+ img = yield(img) if block_given?
190
+ img
191
+ end
192
+ end
193
+
194
+ ##
195
+ # Resize the image to fit within the specified dimensions while retaining
196
+ # the original aspect ratio. If necessary, will pad the remaining area
197
+ # with the given color, which defaults to transparent (for gif and png,
198
+ # white for jpeg).
199
+ #
200
+ # See http://www.imagemagick.org/script/command-line-options.php#gravity
201
+ # for gravity options.
202
+ #
203
+ # === Parameters
204
+ #
205
+ # [width (Integer)] the width to scale the image to
206
+ # [height (Integer)] the height to scale the image to
207
+ # [background (String, :transparent)] the color of the background as a hexcode, like "#ff45de"
208
+ # [gravity (String)] how to position the image
209
+ #
210
+ # === Yields
211
+ #
212
+ # [MiniMagick::Image] additional manipulations to perform
213
+ #
214
+ def resize_and_pad(width, height, background=:transparent, gravity='Center')
215
+ manipulate! do |img|
216
+ img.combine_options do |cmd|
217
+ cmd.thumbnail "#{width}x#{height}>"
218
+ if background == :transparent
219
+ cmd.background "rgba(0, 0, 0, 0.0)"
220
+ else
221
+ cmd.background background
222
+ end
223
+ cmd.gravity gravity
224
+ cmd.extent "#{width}x#{height}"
225
+ end
226
+ img = yield(img) if block_given?
227
+ img
228
+ end
229
+ end
230
+
231
+ ##
232
+ # Manipulate the image with RMagick. This method will load up an image
233
+ # and then pass each of its frames to the supplied block. It will then
234
+ # save the image to disk.
235
+ #
236
+ # === Gotcha
237
+ #
238
+ # This method assumes that the object responds to +current_path+.
239
+ # Any class that this module is mixed into must have a +current_path+ method.
240
+ # CarrierWave::Uploader does, so you won't need to worry about this in
241
+ # most cases.
242
+ #
243
+ # === Yields
244
+ #
245
+ # [MiniMagick::Image] manipulations to perform
246
+ #
247
+ # === Raises
248
+ #
249
+ # [CarrierWave::ProcessingError] if manipulation failed.
250
+ #
251
+ def manipulate!
252
+ image = ::MiniMagick::Image.from_file(current_path)
253
+ image = yield(image)
254
+ image.write(current_path)
255
+ ::MiniMagick::Image.from_file(current_path)
256
+ rescue ::MiniMagick::Error, MiniMagick::Invalid => e
257
+ raise CarrierWave::ProcessingError.new("Failed to manipulate with MiniMagick, maybe it is not an image? Original Error: #{e}")
258
+ end
259
+
260
+ end # MiniMagick
261
+ end # CarrierWave
@@ -0,0 +1,278 @@
1
+ # encoding: utf-8
2
+
3
+ unless defined? Magick
4
+ begin
5
+ require 'rmagick'
6
+ rescue LoadError
7
+ require 'RMagick'
8
+ rescue LoadError
9
+ puts "WARNING: Failed to require rmagick, image processing may fail!"
10
+ end
11
+ end
12
+
13
+ module CarrierWave
14
+
15
+ ##
16
+ # This module simplifies manipulation with RMagick by providing a set
17
+ # of convenient helper methods. If you want to use them, you'll need to
18
+ # require this file:
19
+ #
20
+ # require 'carrierwave/processing/rmagick'
21
+ #
22
+ # And then include it in your uploader:
23
+ #
24
+ # class MyUploader < CarrierWave::Uploader::Base
25
+ # include CarrierWave::RMagick
26
+ # end
27
+ #
28
+ # You can now use the provided helpers:
29
+ #
30
+ # class MyUploader < CarrierWave::Uploader::Base
31
+ # include CarrierWave::RMagick
32
+ #
33
+ # process :resize_to_fit => [200, 200]
34
+ # end
35
+ #
36
+ # Or create your own helpers with the powerful manipulate! method. Check
37
+ # out the RMagick docs at http://www.imagemagick.org/RMagick/doc/ for more
38
+ # info
39
+ #
40
+ # class MyUploader < CarrierWave::Uploader::Base
41
+ # include CarrierWave::RMagick
42
+ #
43
+ # process :do_stuff => 10.0
44
+ #
45
+ # def do_stuff(blur_factor)
46
+ # manipulate! do |img|
47
+ # img = img.sepiatone
48
+ # img = img.auto_orient
49
+ # img = img.radial_blur(blur_factor)
50
+ # end
51
+ # end
52
+ # end
53
+ #
54
+ # === Note
55
+ #
56
+ # You should be aware how RMagick handles memory. manipulate! takes care
57
+ # of freeing up memory for you, but for optimum memory usage you should
58
+ # use destructive operations as much as possible:
59
+ #
60
+ # DON'T DO THIS:
61
+ # img = img.resize_to_fit
62
+ #
63
+ # DO THIS INSTEAD:
64
+ # img.resize_to_fit!
65
+ #
66
+ # Read this for more information why:
67
+ #
68
+ # http://rubyforge.org/forum/forum.php?thread_id=1374&forum_id=1618
69
+ #
70
+ module RMagick
71
+ extend ActiveSupport::Concern
72
+
73
+ module ClassMethods
74
+ def convert(format)
75
+ process :convert => format
76
+ end
77
+
78
+ def resize_to_limit(width, height)
79
+ process :resize_to_limit => [width, height]
80
+ end
81
+
82
+ def resize_to_fit(width, height)
83
+ process :resize_to_fit => [width, height]
84
+ end
85
+
86
+ def resize_to_fill(width, height, gravity=::Magick::CenterGravity)
87
+ process :resize_to_fill => [width, height, gravity]
88
+ end
89
+
90
+ def resize_and_pad(width, height, background=:transparent, gravity=::Magick::CenterGravity)
91
+ process :resize_and_pad => [width, height, background, gravity]
92
+ end
93
+ end
94
+
95
+ ##
96
+ # Changes the image encoding format to the given format
97
+ #
98
+ # See even http://www.imagemagick.org/RMagick/doc/magick.html#formats
99
+ #
100
+ # === Parameters
101
+ #
102
+ # [format (#to_s)] an abreviation of the format
103
+ #
104
+ # === Yields
105
+ #
106
+ # [Magick::Image] additional manipulations to perform
107
+ #
108
+ # === Examples
109
+ #
110
+ # image.convert(:png)
111
+ #
112
+ def convert(format)
113
+ manipulate!(:format => format)
114
+ end
115
+
116
+ ##
117
+ # Resize the image to fit within the specified dimensions while retaining
118
+ # the original aspect ratio. Will only resize the image if it is larger than the
119
+ # specified dimensions. The resulting image may be shorter or narrower than specified
120
+ # in the smaller dimension but will not be larger than the specified values.
121
+ #
122
+ # === Parameters
123
+ #
124
+ # [width (Integer)] the width to scale the image to
125
+ # [height (Integer)] the height to scale the image to
126
+ #
127
+ # === Yields
128
+ #
129
+ # [Magick::Image] additional manipulations to perform
130
+ #
131
+ def resize_to_limit(width, height)
132
+ manipulate! do |img|
133
+ geometry = Magick::Geometry.new(width, height, 0, 0, Magick::GreaterGeometry)
134
+ new_img = img.change_geometry(geometry) do |new_width, new_height|
135
+ img.resize(new_width, new_height)
136
+ end
137
+ destroy_image(img)
138
+ new_img = yield(new_img) if block_given?
139
+ new_img
140
+ end
141
+ end
142
+
143
+ ##
144
+ # From the RMagick documentation: "Resize the image to fit within the
145
+ # specified dimensions while retaining the original aspect ratio. The
146
+ # image may be shorter or narrower than specified in the smaller dimension
147
+ # but will not be larger than the specified values."
148
+ #
149
+ # See even http://www.imagemagick.org/RMagick/doc/image3.html#resize_to_fit
150
+ #
151
+ # === Parameters
152
+ #
153
+ # [width (Integer)] the width to scale the image to
154
+ # [height (Integer)] the height to scale the image to
155
+ #
156
+ # === Yields
157
+ #
158
+ # [Magick::Image] additional manipulations to perform
159
+ #
160
+ def resize_to_fit(width, height)
161
+ manipulate! do |img|
162
+ img.resize_to_fit!(width, height)
163
+ img = yield(img) if block_given?
164
+ img
165
+ end
166
+ end
167
+
168
+ ##
169
+ # From the RMagick documentation: "Resize the image to fit within the
170
+ # specified dimensions while retaining the aspect ratio of the original
171
+ # image. If necessary, crop the image in the larger dimension."
172
+ #
173
+ # See even http://www.imagemagick.org/RMagick/doc/image3.html#resize_to_fill
174
+ #
175
+ # === Parameters
176
+ #
177
+ # [width (Integer)] the width to scale the image to
178
+ # [height (Integer)] the height to scale the image to
179
+ #
180
+ # === Yields
181
+ #
182
+ # [Magick::Image] additional manipulations to perform
183
+ #
184
+ def resize_to_fill(width, height, gravity=::Magick::CenterGravity)
185
+ manipulate! do |img|
186
+ img.crop_resized!(width, height, gravity)
187
+ img = yield(img) if block_given?
188
+ img
189
+ end
190
+ end
191
+
192
+ ##
193
+ # Resize the image to fit within the specified dimensions while retaining
194
+ # the original aspect ratio. If necessary, will pad the remaining area
195
+ # with the given color, which defaults to transparent (for gif and png,
196
+ # white for jpeg).
197
+ #
198
+ # === Parameters
199
+ #
200
+ # [width (Integer)] the width to scale the image to
201
+ # [height (Integer)] the height to scale the image to
202
+ # [background (String, :transparent)] the color of the background as a hexcode, like "#ff45de"
203
+ # [gravity (Magick::GravityType)] how to position the image
204
+ #
205
+ # === Yields
206
+ #
207
+ # [Magick::Image] additional manipulations to perform
208
+ #
209
+ def resize_and_pad(width, height, background=:transparent, gravity=::Magick::CenterGravity)
210
+ manipulate! do |img|
211
+ img.resize_to_fit!(width, height)
212
+ new_img = ::Magick::Image.new(width, height)
213
+ if background == :transparent
214
+ filled = new_img.matte_floodfill(1, 1)
215
+ else
216
+ filled = new_img.color_floodfill(1, 1, ::Magick::Pixel.from_color(background))
217
+ end
218
+ destroy_image(new_img)
219
+ filled.composite!(img, gravity, ::Magick::OverCompositeOp)
220
+ destroy_image(img)
221
+ filled = yield(filled) if block_given?
222
+ filled
223
+ end
224
+ end
225
+
226
+ ##
227
+ # Manipulate the image with RMagick. This method will load up an image
228
+ # and then pass each of its frames to the supplied block. It will then
229
+ # save the image to disk.
230
+ #
231
+ # === Gotcha
232
+ #
233
+ # This method assumes that the object responds to +current_path+.
234
+ # Any class that this module is mixed into must have a +current_path+ method.
235
+ # CarrierWave::Uploader does, so you won't need to worry about this in
236
+ # most cases.
237
+ #
238
+ # === Yields
239
+ #
240
+ # [Magick::Image] manipulations to perform
241
+ #
242
+ # === Raises
243
+ #
244
+ # [CarrierWave::ProcessingError] if manipulation failed.
245
+ #
246
+ def manipulate!(options={})
247
+ image = ::Magick::Image.read(current_path)
248
+
249
+ frames = if image.size > 1
250
+ list = ::Magick::ImageList.new
251
+ image.each do |frame|
252
+ list << yield( frame )
253
+ end
254
+ list
255
+ else
256
+ frame = image.first
257
+ frame = yield( frame ) if block_given?
258
+ frame
259
+ end
260
+
261
+ if options[:format]
262
+ frames.write("#{options[:format]}:#{current_path}")
263
+ else
264
+ frames.write(current_path)
265
+ end
266
+ destroy_image(frames)
267
+ rescue ::Magick::ImageMagickError => e
268
+ raise CarrierWave::ProcessingError.new("Failed to manipulate with rmagick, maybe it is not an image? Original Error: #{e}")
269
+ end
270
+
271
+ private
272
+
273
+ def destroy_image(image)
274
+ image.destroy! if image.respond_to?(:destroy!)
275
+ end
276
+
277
+ end # RMagick
278
+ end # CarrierWave