Module: CarrierWave::MiniMagick
- Extended by:
- ActiveSupport::Concern
- Defined in:
- lib/carrierwave/processing/mini_magick.rb
Overview
This module simplifies manipulation with MiniMagick by providing a set of convenient helper methods. If you want to use them, you’ll need to require this file:
require 'carrierwave/processing/mini_magick'
And then include it in your uploader:
class MyUploader < CarrierWave::Uploader::Base
include CarrierWave::MiniMagick
end
You can now use the provided helpers:
class MyUploader < CarrierWave::Uploader::Base
include CarrierWave::MiniMagick
process :resize_to_fit => [200, 200]
end
Or create your own helpers with the powerful manipulate! method. Check out the ImageMagick docs at www.imagemagick.org/script/command-line-options.php for more info
class MyUploader < CarrierWave::Uploader::Base
include CarrierWave::MiniMagick
process :radial_blur => 10
def radial_blur(amount)
manipulate! do |img|
img.radial_blur(amount)
img = yield(img) if block_given?
img
end
end
end
Note
MiniMagick is a mini replacement for RMagick that uses the command line tool “mogrify” for image manipulation.
You can find more information here:
mini_magick.rubyforge.org/ and github.com/minimagick/minimagick/
Defined Under Namespace
Modules: ClassMethods
Instance Method Summary collapse
-
#convert(format, page = nil) ⇒ Object
Changes the image encoding format to the given format.
-
#height ⇒ Object
Returns the height of the image in pixels.
-
#manipulate! ⇒ Object
Manipulate the image with MiniMagick.
-
#resize_and_pad(width, height, background = :transparent, gravity = 'Center', combine_options: {}) ⇒ Object
Resize the image to fit within the specified dimensions while retaining the original aspect ratio.
-
#resize_to_fill(width, height, gravity = 'Center', combine_options: {}) ⇒ Object
Resize the image to fit within the specified dimensions while retaining the aspect ratio of the original image.
-
#resize_to_fit(width, height, combine_options: {}) ⇒ Object
Resize the image to fit within the specified dimensions while retaining the original aspect ratio.
-
#resize_to_limit(width, height, combine_options: {}) ⇒ Object
Resize the image to fit within the specified dimensions while retaining the original aspect ratio.
-
#width ⇒ Object
Returns the width of the image in pixels.
Instance Method Details
#convert(format, page = nil) ⇒ Object
Changes the image encoding format to the given format
See www.imagemagick.org/script/command-line-options.php#format
Parameters
- format (#to_s)
-
an abreviation of the format
Yields
- MiniMagick::Image
-
additional manipulations to perform
Examples
image.convert(:png)
112 113 114 115 116 117 118 119 |
# File 'lib/carrierwave/processing/mini_magick.rb', line 112 def convert(format, page=nil) @format = format @page = page manipulate! do |img| img = yield(img) if block_given? img end end |
#height ⇒ Object
Returns the height of the image in pixels.
Returns
- Integer
-
the image’s height in pixels
278 279 280 |
# File 'lib/carrierwave/processing/mini_magick.rb', line 278 def height mini_magick_image[:height] end |
#manipulate! ⇒ Object
Manipulate the image with MiniMagick. This method will load up an image and then pass each of its frames to the supplied block. It will then save the image to disk.
Gotcha
This method assumes that the object responds to current_path
. Any class that this module is mixed into must have a current_path
method. CarrierWave::Uploader does, so you won’t need to worry about this in most cases.
Yields
- MiniMagick::Image
-
manipulations to perform
Raises
- CarrierWave::ProcessingError
-
if manipulation failed.
302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 |
# File 'lib/carrierwave/processing/mini_magick.rb', line 302 def manipulate! cache_stored_file! if !cached? image = ::MiniMagick::Image.open(current_path) begin image.format(@format.to_s.downcase, @page) if @format image = yield(image) image.write(current_path) if @format move_to = current_path.chomp(File.extname(current_path)) + ".#{@format}" file.content_type = ::MIME::Types.type_for(move_to).first.to_s file.move_to(move_to, , ) end image.run_command("identify", current_path) ensure image.destroy! end rescue ::MiniMagick::Error, ::MiniMagick::Invalid => e = I18n.translate(:"errors.messages.mini_magick_processing_error", :e => e) raise CarrierWave::ProcessingError, end |
#resize_and_pad(width, height, background = :transparent, gravity = 'Center', combine_options: {}) ⇒ Object
Resize the image to fit within the specified dimensions while retaining the original aspect ratio. If necessary, will pad the remaining area with the given color, which defaults to transparent (for gif and png, white for jpeg).
See www.imagemagick.org/script/command-line-options.php#gravity for gravity options.
Parameters
- width (Integer)
-
the width to scale the image to
- height (Integer)
-
the height to scale the image to
- background (String, :transparent)
-
the color of the background as a hexcode, like “#ff45de”
- gravity (String)
-
how to position the image
Yields
- MiniMagick::Image
-
additional manipulations to perform
240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 |
# File 'lib/carrierwave/processing/mini_magick.rb', line 240 def resize_and_pad(width, height, background=:transparent, gravity='Center', combine_options: {}) width = dimension_from width height = dimension_from height manipulate! do |img| img. do |cmd| cmd.thumbnail "#{width}x#{height}>" if background == :transparent cmd.background "rgba(255, 255, 255, 0.0)" else cmd.background background end cmd.gravity gravity cmd.extent "#{width}x#{height}" cmd, end img = yield(img) if block_given? img end end |
#resize_to_fill(width, height, gravity = 'Center', combine_options: {}) ⇒ Object
Resize the image to fit within the specified dimensions while retaining the aspect ratio of the original image. If necessary, crop the image in the larger dimension.
Parameters
- width (Integer)
-
the width to scale the image to
- height (Integer)
-
the height to scale the image to
- gravity (String)
-
the current gravity suggestion (default: ‘Center’; options: ‘NorthWest’, ‘North’, ‘NorthEast’, ‘West’, ‘Center’, ‘East’, ‘SouthWest’, ‘South’, ‘SouthEast’)
Yields
- MiniMagick::Image
-
additional manipulations to perform
191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 |
# File 'lib/carrierwave/processing/mini_magick.rb', line 191 def resize_to_fill(width, height, gravity = 'Center', combine_options: {}) width = dimension_from width height = dimension_from height manipulate! do |img| cols, rows = img[:dimensions] img. do |cmd| if width != cols || height != rows scale_x = width/cols.to_f scale_y = height/rows.to_f if scale_x >= scale_y cols = (scale_x * (cols + 0.5)).round rows = (scale_x * (rows + 0.5)).round cmd.resize "#{cols}" else cols = (scale_y * (cols + 0.5)).round rows = (scale_y * (rows + 0.5)).round cmd.resize "x#{rows}" end end cmd.gravity gravity cmd.background "rgba(255,255,255,0.0)" cmd.extent "#{width}x#{height}" if cols != width || rows != height cmd, end img = yield(img) if block_given? img end end |
#resize_to_fit(width, height, combine_options: {}) ⇒ Object
Resize the image to fit within the specified dimensions while retaining the original aspect ratio. The image may be shorter or narrower than specified in the smaller dimension but will not be larger than the specified values.
Parameters
- width (Integer)
-
the width to scale the image to
- height (Integer)
-
the height to scale the image to
Yields
- MiniMagick::Image
-
additional manipulations to perform
163 164 165 166 167 168 169 170 171 172 173 174 |
# File 'lib/carrierwave/processing/mini_magick.rb', line 163 def resize_to_fit(width, height, combine_options: {}) width = dimension_from width height = dimension_from height manipulate! do |img| img. do |cmd| cmd.resize "#{width}x#{height}" cmd, end img = yield(img) if block_given? img end end |
#resize_to_limit(width, height, combine_options: {}) ⇒ Object
Resize the image to fit within the specified dimensions while retaining the original aspect ratio. Will only resize the image if it is larger than the specified dimensions. The resulting image may be shorter or narrower than specified in the smaller dimension but will not be larger than the specified values.
Parameters
- width (Integer)
-
the width to scale the image to
- height (Integer)
-
the height to scale the image to
Yields
- MiniMagick::Image
-
additional manipulations to perform
136 137 138 139 140 141 142 143 144 145 146 147 |
# File 'lib/carrierwave/processing/mini_magick.rb', line 136 def resize_to_limit(width, height, combine_options: {}) width = dimension_from width height = dimension_from height manipulate! do |img| img. do |cmd| cmd.resize "#{width}x#{height}>" cmd, end img = yield(img) if block_given? img end end |
#width ⇒ Object
Returns the width of the image in pixels.
Returns
- Integer
-
the image’s width in pixels
267 268 269 |
# File 'lib/carrierwave/processing/mini_magick.rb', line 267 def width mini_magick_image[:width] end |