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)`.



212
213
214
215
216
217
218
# File 'lib/image_processing/mini_magick.rb', line 212

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)
  file.rewind
  tempfile
end

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

Adjusts the image so that its orientation is suitable for viewing.

Parameters:

  • image (MiniMagick::Image)

    the image to convert

Yields:

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

Returns:

  • (File, Tempfile)

See Also:



40
41
42
43
44
45
46
47
# File 'lib/image_processing/mini_magick.rb', line 40

def auto_orient!(image)
  with_minimagick(image) do |img|
    img.combine_options do |cmd|
      yield cmd if block_given?
      cmd.auto_orient
    end
  end
end

.convert!(image, format, page = nil) {|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:



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

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

.crop!(image, width, height, x_offset = 0, y_offset = 0, gravity: "NorthWest") {|MiniMagick::Tool::Mogrify, MiniMagick::Tool::Convert| ... } ⇒ File, Tempfile

Crops the image to be the defined area.

Parameters:

  • width (#to_s)

    the width of the cropped image

  • height (#to_s)

    the height of the cropped image

  • x_offset (#to_s) (defaults to: 0)

    the x coordinate where to start cropping

  • y_offset (#to_s) (defaults to: 0)

    the y coordinate where to start cropping

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

    which part of the image to focus on

Yields:

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

Returns:

  • (File, Tempfile)

See Also:



189
190
191
192
193
194
195
196
197
# File 'lib/image_processing/mini_magick.rb', line 189

def crop!(image, width, height, x_offset = 0, y_offset = 0, gravity: "NorthWest")
  with_minimagick(image) do |img|
    img.combine_options do |cmd|
      yield cmd if block_given?
      cmd.gravity gravity
      cmd.crop "#{width}x#{height}+#{x_offset}+#{y_offset}"
    end
  end
end

.nondestructive_alias(name, original) ⇒ Object



11
12
13
14
15
16
# 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
  module_function name
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:



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

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:



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

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



107
108
109
110
111
112
113
114
115
116
# File 'lib/image_processing/mini_magick.rb', line 107

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)


81
82
83
84
85
86
87
88
# File 'lib/image_processing/mini_magick.rb', line 81

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)


61
62
63
64
65
66
67
68
# File 'lib/image_processing/mini_magick.rb', line 61

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

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


202
203
204
205
206
207
208
# File 'lib/image_processing/mini_magick.rb', line 202

def with_minimagick(image)
  image = ::MiniMagick::Image.new(image.path, image)
  yield image
  tempfile = image.instance_variable_get("@tempfile")
  tempfile.open if tempfile.is_a?(Tempfile) # for aws-sdk
  tempfile
end