module CarrierWave::MiniMagick
def convert(format, page=nil, &block)
image.convert(:png)
=== Examples
[MiniMagick::Image] additional manipulations to perform
=== Yields
[format (#to_s)] an abbreviation of the format
=== Parameters
See http://www.imagemagick.org/script/command-line-options.php#format
Changes the image encoding format to the given format
#
def convert(format, page=nil, &block) minimagick!(block) do |builder| builder = builder.convert(format) builder = builder.loader(page: page) if page builder end end
def crop(left, top, width, height, combine_options: {}, &block)
[MiniMagick::Image] additional manipulations to perform
=== Yields
[height (Integer)] height of area to extract
[width (Integer)] width of area to extract
[top (integer)] top edge of area to extract
[left (integer)] left edge of area to extract
=== Parameters
outside the image bounds.
by [width] and [height]. The original image bottom/right edge is preserved if the cropping box falls
Crop the image to the contents of a box positioned at [left] and [top], with the dimensions given
#
def crop(left, top, width, height, combine_options: {}, &block) width, height = resolve_dimensions(width, height) minimagick!(block) do |builder| builder.crop(left, top, width, height) .apply(combine_options) end end
def height
[Integer] the image's height in pixels
=== Returns
Returns the height of the image in pixels.
#
def height mini_magick_image[:height] end
def manipulate!
[CarrierWave::ProcessingError] if manipulation failed.
=== Raises
[MiniMagick::Image] manipulations to perform
=== Yields
most cases.
CarrierWave::Uploader does, so you won't need to worry about this in
Any class that this module is mixed into must have a +current_path+ method.
This method assumes that the object responds to +current_path+.
=== Gotcha
probably use #minimagick!.
NOTE: This method exists mostly for backwards compatibility, you should
save the image to disk.
and then pass each of its frames to the supplied block. It will then
Manipulate the image with MiniMagick. This method will load up an image
#
def manipulate! cache_stored_file! if !cached? image = ::MiniMagick::Image.open(current_path) image = yield(image) FileUtils.mv image.path, current_path ::MiniMagick::Image.new(current_path).identify rescue ::MiniMagick::Error, ::MiniMagick::Invalid => e raise e if e.message =~ /(You must have .+ installed|is not installed|executable not found|delegate failed)/ message = I18n.translate(:"errors.messages.processing_error") raise CarrierWave::ProcessingError, message ensure image.destroy! if image end
def mini_magick_image
def mini_magick_image ::MiniMagick::Image.read(read) end
def minimagick!(block = nil)
=== Raises
[ImageProcessing::Builder] use it to define processing to be performed
=== Yields
most cases.
CarrierWave::Uploader does, so you won't need to worry about this in
Any class that this module is mixed into must have a +current_path+ method.
This method assumes that the object responds to +current_path+.
=== Gotcha
current image.
method will build a "convert" ImageMagick command and execute it on the
Process the image with MiniMagick, using the ImageProcessing gem. This
def minimagick!(block = nil) builder = ImageProcessing::MiniMagick.source(current_path) builder = yield(builder) result = builder.call result.close # backwards compatibility (we want to eventually move away from MiniMagick::Image) if block image = ::MiniMagick::Image.new(result.path, result) image = block.call(image) result = image.instance_variable_get(:@tempfile) end FileUtils.mv result.path, current_path if File.extname(result.path) != File.extname(current_path) move_to = current_path.chomp(File.extname(current_path)) + File.extname(result.path) file.content_type = Marcel::Magic.by_path(move_to).try(:type) file.move_to(move_to, permissions, directory_permissions) end rescue ::MiniMagick::Error, ::MiniMagick::Invalid => e raise e if e.message =~ /(You must have .+ installed|is not installed|executable not found)/ message = I18n.translate(:"errors.messages.processing_error") raise CarrierWave::ProcessingError, message end
def resize_and_pad(width, height, background=:transparent, gravity='Center', combine_options: {}, &block)
[MiniMagick::Image] additional manipulations to perform
=== Yields
[combine_options (Hash)] additional ImageMagick options to apply before resizing
[gravity (String)] how to position the image
[background (String, :transparent)] the color of the background as a hexcode, like "#ff45de"
[height (Integer)] the height to scale the image to
[width (Integer)] the width to scale the image to
=== Parameters
for gravity options.
See http://www.imagemagick.org/script/command-line-options.php#gravity
white for jpeg).
with the given color, which defaults to transparent (for gif and png,
the original aspect ratio. If necessary, will pad the remaining area
Resize the image to fit within the specified dimensions while retaining
#
def resize_and_pad(width, height, background=:transparent, gravity='Center', combine_options: {}, &block) width, height = resolve_dimensions(width, height) minimagick!(block) do |builder| builder.resize_and_pad(width, height, background: background, gravity: gravity) .apply(combine_options) end end
def resize_to_fill(width, height, gravity = 'Center', combine_options: {}, &block)
[MiniMagick::Image] additional manipulations to perform
=== Yields
[combine_options (Hash)] additional ImageMagick options to apply before resizing
[gravity (String)] the current gravity suggestion (default: 'Center'; options: 'NorthWest', 'North', 'NorthEast', 'West', 'Center', 'East', 'SouthWest', 'South', 'SouthEast')
[height (Integer)] the height to scale the image to
[width (Integer)] the width to scale the image to
=== Parameters
larger dimension.
the aspect ratio of the original image. If necessary, crop the image in the
Resize the image to fit within the specified dimensions while retaining
#
def resize_to_fill(width, height, gravity = 'Center', combine_options: {}, &block) width, height = resolve_dimensions(width, height) minimagick!(block) do |builder| builder.resize_to_fill(width, height, gravity: gravity) .apply(combine_options) end end
def resize_to_fit(width, height, combine_options: {}, &block)
[MiniMagick::Image] additional manipulations to perform
=== Yields
[combine_options (Hash)] additional ImageMagick options to apply before resizing
[height (Integer)] the height to scale the image to
[width (Integer)] the width to scale the image to
=== Parameters
specified in the smaller dimension but will not be larger than the specified values.
the original aspect ratio. The image may be shorter or narrower than
Resize the image to fit within the specified dimensions while retaining
#
def resize_to_fit(width, height, combine_options: {}, &block) width, height = resolve_dimensions(width, height) minimagick!(block) do |builder| builder.resize_to_fit(width, height) .apply(combine_options) end end
def resize_to_limit(width, height, combine_options: {}, &block)
[MiniMagick::Image] additional manipulations to perform
=== Yields
[combine_options (Hash)] additional ImageMagick options to apply before resizing
[height (Integer)] the height to scale the image to
[width (Integer)] the width to scale the image to
=== Parameters
in the smaller dimension but will not be larger than the specified values.
specified dimensions. The resulting image may be shorter or narrower than specified
the original aspect ratio. Will only resize the image if it is larger than the
Resize the image to fit within the specified dimensions while retaining
#
def resize_to_limit(width, height, combine_options: {}, &block) width, height = resolve_dimensions(width, height) minimagick!(block) do |builder| builder.resize_to_limit(width, height) .apply(combine_options) end end
def resolve_dimensions(*dimensions)
def resolve_dimensions(*dimensions) dimensions.map do |value| next value unless value.instance_of?(Proc) value.arity >= 1 ? value.call(self) : value.call end end
def width
[Integer] the image's width in pixels
=== Returns
Returns the width of the image in pixels.
#
def width mini_magick_image[:width] end