dsturnbull-carrierwave 0.4.5
Sign up to get free protection for your applications and to get access to all the features.
- data/Generators +4 -0
- data/History.txt +118 -0
- data/Manifest.txt +107 -0
- data/README.rdoc +502 -0
- data/Rakefile +38 -0
- data/cucumber.yml +2 -0
- data/features/caching.feature +28 -0
- data/features/download.feature +20 -0
- data/features/file_storage.feature +37 -0
- data/features/file_storage_overridden_filename.feature +38 -0
- data/features/file_storage_overridden_store_dir.feature +38 -0
- data/features/file_storage_reversing_processor.feature +43 -0
- data/features/fixtures/bork.txt +1 -0
- data/features/fixtures/monkey.txt +1 -0
- data/features/grid_fs_storage.feature +32 -0
- data/features/mount_activerecord.feature +46 -0
- data/features/mount_datamapper.feature +46 -0
- data/features/step_definitions/activerecord_steps.rb +22 -0
- data/features/step_definitions/caching_steps.rb +14 -0
- data/features/step_definitions/datamapper_steps.rb +29 -0
- data/features/step_definitions/download_steps.rb +4 -0
- data/features/step_definitions/file_steps.rb +53 -0
- data/features/step_definitions/general_steps.rb +85 -0
- data/features/step_definitions/mount_steps.rb +19 -0
- data/features/step_definitions/store_steps.rb +18 -0
- data/features/support/activerecord.rb +30 -0
- data/features/support/datamapper.rb +7 -0
- data/features/support/env.rb +22 -0
- data/features/versions_basics.feature +50 -0
- data/features/versions_nested_versions.feature +70 -0
- data/features/versions_overridden_filename.feature +51 -0
- data/features/versions_overriden_store_dir.feature +41 -0
- data/lib/carrierwave/compatibility/paperclip.rb +95 -0
- data/lib/carrierwave/core_ext/blank.rb +46 -0
- data/lib/carrierwave/core_ext/inheritable_attributes.rb +104 -0
- data/lib/carrierwave/core_ext/module_setup.rb +51 -0
- data/lib/carrierwave/mount.rb +359 -0
- data/lib/carrierwave/orm/activerecord.rb +73 -0
- data/lib/carrierwave/orm/datamapper.rb +27 -0
- data/lib/carrierwave/orm/mongoid.rb +23 -0
- data/lib/carrierwave/orm/mongomapper.rb +27 -0
- data/lib/carrierwave/orm/sequel.rb +45 -0
- data/lib/carrierwave/processing/image_science.rb +101 -0
- data/lib/carrierwave/processing/mini_magick.rb +269 -0
- data/lib/carrierwave/processing/rmagick.rb +282 -0
- data/lib/carrierwave/sanitized_file.rb +268 -0
- data/lib/carrierwave/storage/abstract.rb +30 -0
- data/lib/carrierwave/storage/file.rb +48 -0
- data/lib/carrierwave/storage/grid_fs.rb +96 -0
- data/lib/carrierwave/storage/right_s3.rb +170 -0
- data/lib/carrierwave/storage/s3.rb +199 -0
- data/lib/carrierwave/test/matchers.rb +128 -0
- data/lib/carrierwave/uploader/cache.rb +145 -0
- data/lib/carrierwave/uploader/callbacks.rb +42 -0
- data/lib/carrierwave/uploader/configuration.rb +122 -0
- data/lib/carrierwave/uploader/default_url.rb +19 -0
- data/lib/carrierwave/uploader/download.rb +59 -0
- data/lib/carrierwave/uploader/extension_whitelist.rb +37 -0
- data/lib/carrierwave/uploader/mountable.rb +39 -0
- data/lib/carrierwave/uploader/processing.rb +83 -0
- data/lib/carrierwave/uploader/proxy.rb +62 -0
- data/lib/carrierwave/uploader/remove.rb +22 -0
- data/lib/carrierwave/uploader/store.rb +89 -0
- data/lib/carrierwave/uploader/url.rb +24 -0
- data/lib/carrierwave/uploader/versions.rb +146 -0
- data/lib/carrierwave/uploader.rb +44 -0
- data/lib/carrierwave.rb +96 -0
- data/merb_generators/uploader_generator.rb +22 -0
- data/rails_generators/uploader/USAGE +2 -0
- data/rails_generators/uploader/templates/uploader.rb +47 -0
- data/rails_generators/uploader/uploader_generator.rb +21 -0
- data/script/console +10 -0
- data/script/destroy +14 -0
- data/script/generate +14 -0
- data/spec/compatibility/paperclip_spec.rb +52 -0
- data/spec/fixtures/bork.txt +1 -0
- data/spec/fixtures/landscape.jpg +0 -0
- data/spec/fixtures/portrait.jpg +0 -0
- data/spec/fixtures/test.jpeg +1 -0
- data/spec/fixtures/test.jpg +1 -0
- data/spec/mount_spec.rb +538 -0
- data/spec/orm/activerecord_spec.rb +271 -0
- data/spec/orm/datamapper_spec.rb +168 -0
- data/spec/orm/mongoid_spec.rb +206 -0
- data/spec/orm/mongomapper_spec.rb +202 -0
- data/spec/orm/sequel_spec.rb +183 -0
- data/spec/processing/image_science_spec.rb +56 -0
- data/spec/processing/mini_magick_spec.rb +76 -0
- data/spec/processing/rmagick_spec.rb +75 -0
- data/spec/sanitized_file_spec.rb +601 -0
- data/spec/spec_helper.rb +99 -0
- data/spec/storage/grid_fs_spec.rb +82 -0
- data/spec/storage/right_s3_spec.rb +75 -0
- data/spec/storage/s3_spec.rb +95 -0
- data/spec/uploader/cache_spec.rb +209 -0
- data/spec/uploader/configuration_spec.rb +105 -0
- data/spec/uploader/default_url_spec.rb +85 -0
- data/spec/uploader/download_spec.rb +75 -0
- data/spec/uploader/extension_whitelist_spec.rb +44 -0
- data/spec/uploader/mountable_spec.rb +33 -0
- data/spec/uploader/paths_spec.rb +22 -0
- data/spec/uploader/processing_spec.rb +73 -0
- data/spec/uploader/proxy_spec.rb +54 -0
- data/spec/uploader/remove_spec.rb +70 -0
- data/spec/uploader/store_spec.rb +248 -0
- data/spec/uploader/url_spec.rb +87 -0
- data/spec/uploader/versions_spec.rb +298 -0
- metadata +351 -0
@@ -0,0 +1,269 @@
|
|
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
|
+
|
60
|
+
def self.included(base)
|
61
|
+
super
|
62
|
+
base.extend(ClassMethods)
|
63
|
+
end
|
64
|
+
|
65
|
+
module ClassMethods
|
66
|
+
def convert(format)
|
67
|
+
process :resize_to_limit => format
|
68
|
+
end
|
69
|
+
|
70
|
+
def resize_to_limit(width, height)
|
71
|
+
process :resize_to_limit => [width, height]
|
72
|
+
end
|
73
|
+
|
74
|
+
def resize_to_fit(width, height)
|
75
|
+
process :resize_to_fit => [width, height]
|
76
|
+
end
|
77
|
+
|
78
|
+
def resize_to_fill(width, height)
|
79
|
+
process :resize_to_fill => [width, height]
|
80
|
+
end
|
81
|
+
|
82
|
+
def resize_and_pad(width, height)
|
83
|
+
process :resize_to_fit => [width, height]
|
84
|
+
end
|
85
|
+
|
86
|
+
def resize_and_pad(width, height, background=:transparent, gravity=::Magick::CenterGravity)
|
87
|
+
process :resize_and_pad => [width, height, background, gravity]
|
88
|
+
end
|
89
|
+
end
|
90
|
+
|
91
|
+
##
|
92
|
+
# Changes the image encoding format to the given format
|
93
|
+
#
|
94
|
+
# See http://www.imagemagick.org/script/command-line-options.php#format
|
95
|
+
#
|
96
|
+
# === Parameters
|
97
|
+
#
|
98
|
+
# [format (#to_s)] an abreviation of the format
|
99
|
+
#
|
100
|
+
# === Yields
|
101
|
+
#
|
102
|
+
# [MiniMagick::Image] additional manipulations to perform
|
103
|
+
#
|
104
|
+
# === Examples
|
105
|
+
#
|
106
|
+
# image.convert(:png)
|
107
|
+
#
|
108
|
+
def convert(format)
|
109
|
+
manipulate! do |img|
|
110
|
+
img.format(format.to_s.upcase)
|
111
|
+
img = yield(img) if block_given?
|
112
|
+
img
|
113
|
+
end
|
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
|
+
# [MiniMagick::Image] additional manipulations to perform
|
130
|
+
#
|
131
|
+
def resize_to_limit(width, height)
|
132
|
+
manipulate! do |img|
|
133
|
+
img.resize "#{width}x#{height}>"
|
134
|
+
img = yield(img) if block_given?
|
135
|
+
img
|
136
|
+
end
|
137
|
+
end
|
138
|
+
|
139
|
+
##
|
140
|
+
# From the RMagick documentation: "Resize the image to fit within the
|
141
|
+
# specified dimensions while retaining the original aspect ratio. The
|
142
|
+
# image may be shorter or narrower than specified in the smaller dimension
|
143
|
+
# but will not be larger than the specified values."
|
144
|
+
#
|
145
|
+
# See even http://www.imagemagick.org/RMagick/doc/image3.html#resize_to_fit
|
146
|
+
#
|
147
|
+
# === Parameters
|
148
|
+
#
|
149
|
+
# [width (Integer)] the width to scale the image to
|
150
|
+
# [height (Integer)] the height to scale the image to
|
151
|
+
#
|
152
|
+
# === Yields
|
153
|
+
#
|
154
|
+
# [MiniMagick::Image] additional manipulations to perform
|
155
|
+
#
|
156
|
+
def resize_to_fit(width, height)
|
157
|
+
manipulate! do |img|
|
158
|
+
img.resize "#{width}x#{height}"
|
159
|
+
img = yield(img) if block_given?
|
160
|
+
img
|
161
|
+
end
|
162
|
+
end
|
163
|
+
|
164
|
+
##
|
165
|
+
# From the RMagick documentation: "Resize the image to fit within the
|
166
|
+
# specified dimensions while retaining the aspect ratio of the original
|
167
|
+
# image. If necessary, crop the image in the larger dimension."
|
168
|
+
#
|
169
|
+
# See even http://www.imagemagick.org/RMagick/doc/image3.html#resize_to_fill
|
170
|
+
#
|
171
|
+
# and
|
172
|
+
#
|
173
|
+
# http://www.clipclip.org/clips/detail/4365/jerrett-net-»-crop_resized-in-rmagick
|
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
|
+
# [MiniMagick::Image] additional manipulations to perform
|
183
|
+
#
|
184
|
+
def resize_to_fill(width, height, gravity = 'Center')
|
185
|
+
manipulate! do |img|
|
186
|
+
cols, rows = img[:dimensions]
|
187
|
+
img.combine_options do |cmd|
|
188
|
+
if width != cols || height != rows
|
189
|
+
scale = [width/cols.to_f, height/rows.to_f].max
|
190
|
+
cols = (scale * (cols + 0.5)).round
|
191
|
+
rows = (scale * (rows + 0.5)).round
|
192
|
+
cmd.resize "#{cols}x#{rows}"
|
193
|
+
end
|
194
|
+
cmd.gravity gravity
|
195
|
+
cmd.extent "#{width}x#{height}" if cols != width || rows != height
|
196
|
+
end
|
197
|
+
img = yield(img) if block_given?
|
198
|
+
img
|
199
|
+
end
|
200
|
+
end
|
201
|
+
|
202
|
+
##
|
203
|
+
# Resize the image to fit within the specified dimensions while retaining
|
204
|
+
# the original aspect ratio. If necessary, will pad the remaining area
|
205
|
+
# with the given color, which defaults to transparent (for gif and png,
|
206
|
+
# white for jpeg).
|
207
|
+
#
|
208
|
+
# See http://www.imagemagick.org/script/command-line-options.php#gravity
|
209
|
+
# for gravity options.
|
210
|
+
#
|
211
|
+
# === Parameters
|
212
|
+
#
|
213
|
+
# [width (Integer)] the width to scale the image to
|
214
|
+
# [height (Integer)] the height to scale the image to
|
215
|
+
# [background (String, :transparent)] the color of the background as a hexcode, like "#ff45de"
|
216
|
+
# [gravity (String)] how to position the image
|
217
|
+
#
|
218
|
+
# === Yields
|
219
|
+
#
|
220
|
+
# [MiniMagick::Image] additional manipulations to perform
|
221
|
+
#
|
222
|
+
def resize_and_pad(width, height, background=:transparent, gravity='Center')
|
223
|
+
manipulate! do |img|
|
224
|
+
img.combine_options do |cmd|
|
225
|
+
cmd.thumbnail "#{width}x#{height}>"
|
226
|
+
if background == :transparent
|
227
|
+
cmd.background "rgba(0, 0, 0, 0.0)"
|
228
|
+
else
|
229
|
+
cmd.background background
|
230
|
+
end
|
231
|
+
cmd.gravity gravity
|
232
|
+
cmd.extent "#{width}x#{height}"
|
233
|
+
end
|
234
|
+
img = yield(img) if block_given?
|
235
|
+
img
|
236
|
+
end
|
237
|
+
end
|
238
|
+
|
239
|
+
##
|
240
|
+
# Manipulate the image with RMagick. This method will load up an image
|
241
|
+
# and then pass each of its frames to the supplied block. It will then
|
242
|
+
# save the image to disk.
|
243
|
+
#
|
244
|
+
# === Gotcha
|
245
|
+
#
|
246
|
+
# This method assumes that the object responds to +current_path+.
|
247
|
+
# Any class that this module is mixed into must have a +current_path+ method.
|
248
|
+
# CarrierWave::Uploader does, so you won't need to worry about this in
|
249
|
+
# most cases.
|
250
|
+
#
|
251
|
+
# === Yields
|
252
|
+
#
|
253
|
+
# [MiniMagick::Image] manipulations to perform
|
254
|
+
#
|
255
|
+
# === Raises
|
256
|
+
#
|
257
|
+
# [CarrierWave::ProcessingError] if manipulation failed.
|
258
|
+
#
|
259
|
+
def manipulate!
|
260
|
+
image = ::MiniMagick::Image.from_file(current_path)
|
261
|
+
image = yield(image)
|
262
|
+
image.write(current_path)
|
263
|
+
::MiniMagick::Image.from_file(current_path)
|
264
|
+
rescue ::MiniMagick::MiniMagickError => e
|
265
|
+
raise CarrierWave::ProcessingError.new("Failed to manipulate with MiniMagick, maybe it is not an image? Original Error: #{e}")
|
266
|
+
end
|
267
|
+
|
268
|
+
end # MiniMagick
|
269
|
+
end # CarrierWave
|
@@ -0,0 +1,282 @@
|
|
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
|
+
|
72
|
+
def self.included(base)
|
73
|
+
super
|
74
|
+
base.extend(ClassMethods)
|
75
|
+
end
|
76
|
+
|
77
|
+
module ClassMethods
|
78
|
+
def convert(format)
|
79
|
+
process :convert => format
|
80
|
+
end
|
81
|
+
|
82
|
+
def resize_to_limit(width, height)
|
83
|
+
process :resize_to_limit => [width, height]
|
84
|
+
end
|
85
|
+
|
86
|
+
def resize_to_fit(width, height)
|
87
|
+
process :resize_to_fit => [width, height]
|
88
|
+
end
|
89
|
+
|
90
|
+
def resize_to_fill(width, height)
|
91
|
+
process :resize_to_fill => [width, height]
|
92
|
+
end
|
93
|
+
|
94
|
+
def resize_and_pad(width, height, background=:transparent, gravity=::Magick::CenterGravity)
|
95
|
+
process :resize_and_pad => [width, height, background, gravity]
|
96
|
+
end
|
97
|
+
end
|
98
|
+
|
99
|
+
##
|
100
|
+
# Changes the image encoding format to the given format
|
101
|
+
#
|
102
|
+
# See even http://www.imagemagick.org/RMagick/doc/magick.html#formats
|
103
|
+
#
|
104
|
+
# === Parameters
|
105
|
+
#
|
106
|
+
# [format (#to_s)] an abreviation of the format
|
107
|
+
#
|
108
|
+
# === Yields
|
109
|
+
#
|
110
|
+
# [Magick::Image] additional manipulations to perform
|
111
|
+
#
|
112
|
+
# === Examples
|
113
|
+
#
|
114
|
+
# image.convert(:png)
|
115
|
+
#
|
116
|
+
def convert(format)
|
117
|
+
manipulate!(:format => format)
|
118
|
+
end
|
119
|
+
|
120
|
+
##
|
121
|
+
# Resize the image to fit within the specified dimensions while retaining
|
122
|
+
# the original aspect ratio. Will only resize the image if it is larger than the
|
123
|
+
# specified dimensions. The resulting image may be shorter or narrower than specified
|
124
|
+
# in the smaller dimension but will not be larger than the specified values.
|
125
|
+
#
|
126
|
+
# === Parameters
|
127
|
+
#
|
128
|
+
# [width (Integer)] the width to scale the image to
|
129
|
+
# [height (Integer)] the height to scale the image to
|
130
|
+
#
|
131
|
+
# === Yields
|
132
|
+
#
|
133
|
+
# [Magick::Image] additional manipulations to perform
|
134
|
+
#
|
135
|
+
def resize_to_limit(width, height)
|
136
|
+
manipulate! do |img|
|
137
|
+
geometry = Magick::Geometry.new(width, height, 0, 0, Magick::GreaterGeometry)
|
138
|
+
new_img = img.change_geometry(geometry) do |new_width, new_height|
|
139
|
+
img.resize(new_width, new_height)
|
140
|
+
end
|
141
|
+
destroy_image(img)
|
142
|
+
new_img = yield(new_img) if block_given?
|
143
|
+
new_img
|
144
|
+
end
|
145
|
+
end
|
146
|
+
|
147
|
+
##
|
148
|
+
# From the RMagick documentation: "Resize the image to fit within the
|
149
|
+
# specified dimensions while retaining the original aspect ratio. The
|
150
|
+
# image may be shorter or narrower than specified in the smaller dimension
|
151
|
+
# but will not be larger than the specified values."
|
152
|
+
#
|
153
|
+
# See even http://www.imagemagick.org/RMagick/doc/image3.html#resize_to_fit
|
154
|
+
#
|
155
|
+
# === Parameters
|
156
|
+
#
|
157
|
+
# [width (Integer)] the width to scale the image to
|
158
|
+
# [height (Integer)] the height to scale the image to
|
159
|
+
#
|
160
|
+
# === Yields
|
161
|
+
#
|
162
|
+
# [Magick::Image] additional manipulations to perform
|
163
|
+
#
|
164
|
+
def resize_to_fit(width, height)
|
165
|
+
manipulate! do |img|
|
166
|
+
img.resize_to_fit!(width, height)
|
167
|
+
img = yield(img) if block_given?
|
168
|
+
img
|
169
|
+
end
|
170
|
+
end
|
171
|
+
|
172
|
+
##
|
173
|
+
# From the RMagick documentation: "Resize the image to fit within the
|
174
|
+
# specified dimensions while retaining the aspect ratio of the original
|
175
|
+
# image. If necessary, crop the image in the larger dimension."
|
176
|
+
#
|
177
|
+
# See even http://www.imagemagick.org/RMagick/doc/image3.html#resize_to_fill
|
178
|
+
#
|
179
|
+
# === Parameters
|
180
|
+
#
|
181
|
+
# [width (Integer)] the width to scale the image to
|
182
|
+
# [height (Integer)] the height to scale the image to
|
183
|
+
#
|
184
|
+
# === Yields
|
185
|
+
#
|
186
|
+
# [Magick::Image] additional manipulations to perform
|
187
|
+
#
|
188
|
+
def resize_to_fill(width, height)
|
189
|
+
manipulate! do |img|
|
190
|
+
img.crop_resized!(width, height)
|
191
|
+
img = yield(img) if block_given?
|
192
|
+
img
|
193
|
+
end
|
194
|
+
end
|
195
|
+
|
196
|
+
##
|
197
|
+
# Resize the image to fit within the specified dimensions while retaining
|
198
|
+
# the original aspect ratio. If necessary, will pad the remaining area
|
199
|
+
# with the given color, which defaults to transparent (for gif and png,
|
200
|
+
# white for jpeg).
|
201
|
+
#
|
202
|
+
# === Parameters
|
203
|
+
#
|
204
|
+
# [width (Integer)] the width to scale the image to
|
205
|
+
# [height (Integer)] the height to scale the image to
|
206
|
+
# [background (String, :transparent)] the color of the background as a hexcode, like "#ff45de"
|
207
|
+
# [gravity (Magick::GravityType)] how to position the image
|
208
|
+
#
|
209
|
+
# === Yields
|
210
|
+
#
|
211
|
+
# [Magick::Image] additional manipulations to perform
|
212
|
+
#
|
213
|
+
def resize_and_pad(width, height, background=:transparent, gravity=::Magick::CenterGravity)
|
214
|
+
manipulate! do |img|
|
215
|
+
img.resize_to_fit!(width, height)
|
216
|
+
new_img = ::Magick::Image.new(width, height)
|
217
|
+
if background == :transparent
|
218
|
+
filled = new_img.matte_floodfill(1, 1)
|
219
|
+
else
|
220
|
+
filled = new_img.color_floodfill(1, 1, ::Magick::Pixel.from_color(background))
|
221
|
+
end
|
222
|
+
destroy_image(new_img)
|
223
|
+
filled.composite!(img, gravity, ::Magick::OverCompositeOp)
|
224
|
+
destroy_image(img)
|
225
|
+
filled = yield(filled) if block_given?
|
226
|
+
filled
|
227
|
+
end
|
228
|
+
end
|
229
|
+
|
230
|
+
##
|
231
|
+
# Manipulate the image with RMagick. This method will load up an image
|
232
|
+
# and then pass each of its frames to the supplied block. It will then
|
233
|
+
# save the image to disk.
|
234
|
+
#
|
235
|
+
# === Gotcha
|
236
|
+
#
|
237
|
+
# This method assumes that the object responds to +current_path+.
|
238
|
+
# Any class that this module is mixed into must have a +current_path+ method.
|
239
|
+
# CarrierWave::Uploader does, so you won't need to worry about this in
|
240
|
+
# most cases.
|
241
|
+
#
|
242
|
+
# === Yields
|
243
|
+
#
|
244
|
+
# [Magick::Image] manipulations to perform
|
245
|
+
#
|
246
|
+
# === Raises
|
247
|
+
#
|
248
|
+
# [CarrierWave::ProcessingError] if manipulation failed.
|
249
|
+
#
|
250
|
+
def manipulate!(options={})
|
251
|
+
image = ::Magick::Image.read(current_path)
|
252
|
+
|
253
|
+
frames = if image.size > 1
|
254
|
+
list = ::Magick::ImageList.new
|
255
|
+
image.each do |frame|
|
256
|
+
list << yield( frame )
|
257
|
+
end
|
258
|
+
list
|
259
|
+
else
|
260
|
+
frame = image.first
|
261
|
+
frame = yield( frame ) if block_given?
|
262
|
+
frame
|
263
|
+
end
|
264
|
+
|
265
|
+
if options[:format]
|
266
|
+
frames.write("#{options[:format]}:#{current_path}")
|
267
|
+
else
|
268
|
+
frames.write(current_path)
|
269
|
+
end
|
270
|
+
destroy_image(frames)
|
271
|
+
rescue ::Magick::ImageMagickError => e
|
272
|
+
raise CarrierWave::ProcessingError.new("Failed to manipulate with rmagick, maybe it is not an image? Original Error: #{e}")
|
273
|
+
end
|
274
|
+
|
275
|
+
private
|
276
|
+
|
277
|
+
def destroy_image(image)
|
278
|
+
image.destroy! if image.respond_to?(:destroy!)
|
279
|
+
end
|
280
|
+
|
281
|
+
end # RMagick
|
282
|
+
end # CarrierWave
|