Method: ImageProcessing::MiniMagick.resize_and_pad!

Defined in:
lib/image_processing/mini_magick.rb

.resize_and_pad!(image, width, height, background: "transparent", gravity: "Center") {|MiniMagick::Tool::Mogrify, MiniMagick::Tool::Convert| ... } ⇒ File, Tempfile

Resize the image to fit within the specified dimensions while retaining the original aspect ratio in the same way as #fill. Unlike #fill it will, if necessary, pad the remaining area with the given color, which defaults to transparent where supported by the image format and white otherwise.

The resulting image will always be exactly as large as the specified dimensions.

By default, the image will be placed in the center but this can be changed via the ‘gravity` option.

Parameters:

  • image (MiniMagick::image)

    the image to convert

  • width (#to_s)

    the width to fill out

  • height (#to_s)

    the height to fill out

  • background (string) (defaults to: "transparent")

    the color to use as a background

  • gravity (string) (defaults to: "Center")

    which part of the image to focus on

Yields:

  • (MiniMagick::Tool::Mogrify, MiniMagick::Tool::Convert)

Returns:

  • (File, Tempfile)

See Also:



140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
# File 'lib/image_processing/mini_magick.rb', line 140

def resize_and_pad!(image, width, height, background: "transparent", gravity: "Center")
  with_minimagick(image) do |img|
    img.combine_options do |cmd|
      yield cmd if block_given?
      cmd.resize "#{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}"
    end
  end
end