Module: CarrierWave::RMagick
- Defined in:
- lib/carrierwave/processing/rmagick.rb
Overview
This module simplifies manipulation with RMagick by providing a set of convenient helper methods. If you want to use them, you’ll need to require this file
require 'carrierwave/processing/rmagick'
And then include it in your uploader
MyUploade < CarrierWave::Uploader
include CarrierWave::RMagick
end
You can now use the provided helpers:
MyUploade < CarrierWave::Uploader
include CarrierWave::RMagick
process :resize_to_fit => [200, 200]
end
Or create your own helpers with the powerful manipulate! method. Check out the RMagick docs at www.imagemagick.org/RMagick/doc/ for more info
MyUploade < CarrierWave::Uploader
include CarrierWave::RMagick
process :do_stuff => 10.0
def do_stuff(blur_factor)
manipulate! do |img|
img = img.sepiatone
img = img.auto_orient
img = img.radial_blur(blur_factor)
end
end
end
Instance Method Summary collapse
-
#convert(format) {|img| ... } ⇒ Object
Changes the image encoding format to the given format.
-
#manipulate! {|img| ... } ⇒ Object
Manipulate the image with RMagick.
-
#resize_and_pad(width, height, background = :transparent, gravity = ::Magick::CenterGravity) {|img| ... } ⇒ Object
Resize the image to fit within the specified dimensions while retaining the original aspect ratio.
-
#resize_to_fill(width, height) {|img| ... } ⇒ Object
(also: #crop_resized)
From the RMagick documentation: “Resize the image to fit within the specified dimensions while retaining the aspect ratio of the original image.
-
#resize_to_fit(width, height) {|img| ... } ⇒ Object
(also: #resize)
From the RMagick documentation: “Resize the image to fit within the specified dimensions while retaining the original aspect ratio.
Instance Method Details
#convert(format) {|img| ... } ⇒ Object
Changes the image encoding format to the given format
55 56 57 58 59 60 61 |
# File 'lib/carrierwave/processing/rmagick.rb', line 55 def convert(format) manipulate! do |img| img.format = format.to_s.upcase img = yield(img) if block_given? img end end |
#manipulate! {|img| ... } ⇒ Object
Manipulate the image with RMagick. 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.
Note: This method assumes that the object responds to current_path any class that this is mixed into must have a current_path method.
144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 |
# File 'lib/carrierwave/processing/rmagick.rb', line 144 def manipulate! image = ::Magick::Image.read(current_path) if image.size > 1 list = ::Magick::ImageList.new image.each do |frame| list << yield( frame ) end list.write(current_path) else yield( image.first ).write(current_path) end rescue ::Magick::ImageMagickError => e raise CarrierWave::ProcessingError.new("Failed to manipulate with rmagick, maybe it is not an image? Original Error: #{e}") end |
#resize_and_pad(width, height, background = :transparent, gravity = ::Magick::CenterGravity) {|img| ... } ⇒ 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).
118 119 120 121 122 123 124 125 126 127 128 129 130 131 |
# File 'lib/carrierwave/processing/rmagick.rb', line 118 def resize_and_pad(width, height, background=:transparent, gravity=::Magick::CenterGravity) manipulate! do |img| img.resize_to_fit!(width, height) new_img = ::Magick::Image.new(width, height) if background == :transparent new_img = new_img.matte_floodfill(1, 1) else new_img = new_img.color_floodfill(1, 1, ::Magick::Pixel.from_color(background)) end new_img = new_img.composite(img, gravity, ::Magick::OverCompositeOp) new_img = yield(new_img) if block_given? new_img end end |
#resize_to_fill(width, height) {|img| ... } ⇒ Object Also known as: crop_resized
From the RMagick documentation: “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.”
96 97 98 99 100 101 102 |
# File 'lib/carrierwave/processing/rmagick.rb', line 96 def resize_to_fill(width, height) manipulate! do |img| img.resize_to_fill!(width, height) img = yield(img) if block_given? img end end |
#resize_to_fit(width, height) {|img| ... } ⇒ Object Also known as: resize
From the RMagick documentation: “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.”
75 76 77 78 79 80 81 |
# File 'lib/carrierwave/processing/rmagick.rb', line 75 def resize_to_fit(width, height) manipulate! do |img| img.resize_to_fit!(width, height) img = yield(img) if block_given? img end end |