Module: ImageProcessing::MiniMagick

Defined in:
lib/image_processing/mini_magick.rb

Class Method Summary collapse

Class Method Details

._copy_to_tempfile(file) ⇒ Object

Creates a copy of the file and stores it into a Tempfile. Works for any IO object that responds to ‘#read(length = nil, outbuf = nil)`.



172
173
174
175
176
177
# File 'lib/image_processing/mini_magick.rb', line 172

def _copy_to_tempfile(file)
  args = [File.basename(file.path, ".*"), File.extname(file.path)] if file.respond_to?(:path)
  tempfile = Tempfile.new(args || "image", binmode: true)
  IO.copy_stream(file, tempfile.path)
  tempfile
end

._with_minimagick(image) {|image| ... } ⇒ Object

Convert an image into a MiniMagick::Image for the duration of the block, and at the end return a File object.

Yields:

  • (image)


164
165
166
167
168
# File 'lib/image_processing/mini_magick.rb', line 164

def _with_minimagick(image)
  image = ::MiniMagick::Image.new(image.path, image)
  yield image
  image.instance_variable_get("@tempfile")
end

.convert!(image, format) {|MiniMagick::Tool::Mogrify, MiniMagick::Tool::Convert| ... } ⇒ File, Tempfile

Changes the image encoding format to the given format

Parameters:

  • image (MiniMagick::Image)

    the image to convert

  • format (String)

    the format to convert to

Yields:

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

Returns:

  • (File, Tempfile)

See Also:



26
27
28
29
30
# File 'lib/image_processing/mini_magick.rb', line 26

def convert!(image, format, &block)
  _with_minimagick(image) do |img|
    img.format(format.downcase, nil, &block)
  end
end

.nondestructive_alias(name, original) ⇒ Object



11
12
13
14
15
# File 'lib/image_processing/mini_magick.rb', line 11

def self.nondestructive_alias(name, original)
  define_method(name) do |image, *args, &block|
    send(original, _copy_to_tempfile(image), *args, &block)
  end
end

.resample!(image, width, height) {|MiniMagick::Tool::Mogrify, MiniMagick::Tool::Convert| ... } ⇒ File, Tempfile

Resample the image to fit within the specified resolution while retaining the original image size.

The resulting image will always be the same pixel size as the source with an adjusted resolution dimensions.

Parameters:

  • img (MiniMagick::Image)

    the image to convert

  • width (#to_s)

    the dpi width

  • height (#to_s)

    the dpi height

Yields:

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

Returns:

  • (File, Tempfile)

See Also:



152
153
154
155
156
157
158
159
# File 'lib/image_processing/mini_magick.rb', line 152

def resample!(image, width, height)
  _with_minimagick(image) do |img|
    img.combine_options do |cmd|
      yield cmd if block_given?
      cmd.resample "#{width}x#{height}"
    end
  end
end

.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:



123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
# File 'lib/image_processing/mini_magick.rb', line 123

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

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

Resize the image so that it is at least as large in both dimensions as specified, then crops any excess outside the specified dimensions.

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

By default, the center part of the image is kept, and the remainder cropped off, 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

  • 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:



90
91
92
93
94
95
96
97
98
99
# File 'lib/image_processing/mini_magick.rb', line 90

def resize_to_fill!(image, width, height, gravity: "Center")
  _with_minimagick(image) do |img|
    img.combine_options do |cmd|
      yield cmd if block_given?
      cmd.resize "#{width}x#{height}^"
      cmd.gravity gravity
      cmd.extent "#{width}x#{height}"
    end
  end
end

.resize_to_fit!(image, width, height) {|MiniMagick::Tool::Mogrify, MiniMagick::Tool::Convert| ... } ⇒ File, Tempfile

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:

  • image (MiniMagick::Image)

    the image to convert

  • width (#to_s)

    the width to fit into

  • height (#to_s)

    the height to fit into

Yields:

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

Returns:

  • (File, Tempfile)


64
65
66
67
68
69
70
71
# File 'lib/image_processing/mini_magick.rb', line 64

def resize_to_fit!(image, width, height)
  _with_minimagick(image) do |img|
    img.combine_options do |cmd|
      yield cmd if block_given?
      cmd.resize "#{width}x#{height}"
    end
  end
end

.resize_to_limit!(image, width, height) {|MiniMagick::Tool::Mogrify, MiniMagick::Tool::Convert| ... } ⇒ File, Tempfile

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 either dimension but will not be larger than the specified values.

Parameters:

  • image (MiniMagick::Image)

    the image to convert

  • width (#to_s)

    the maximum width

  • height (#to_s)

    the maximum height

Yields:

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

Returns:

  • (File, Tempfile)


44
45
46
47
48
49
50
51
# File 'lib/image_processing/mini_magick.rb', line 44

def resize_to_limit!(image, width, height)
  _with_minimagick(image) do |img|
    img.combine_options do |cmd|
      yield cmd if block_given?
      cmd.resize "#{width}x#{height}>"
    end
  end
end