Class: Refile::MiniMagick

Inherits:
Object
  • Object
show all
Defined in:
lib/refile/mini_magick.rb,
lib/refile/mini_magick/version.rb

Overview

Processes images via MiniMagick, resizing cropping and padding them.

Constant Summary collapse

VERSION =
"0.2.0"

Instance Method Summary collapse

Constructor Details

#initialize(method) ⇒ MiniMagick

Returns a new instance of MiniMagick.

Parameters:

  • method (Symbol) —

    The method to invoke on #call



9
10
11
# File 'lib/refile/mini_magick.rb', line 9

def initialize(method)
  @method = method
end

Instance Method Details

#call(file, *args, format: nil, &block) ⇒ File

Process the given file. The file will be processed via one of the instance methods of this class, depending on the method argument passed to the constructor on initialization.

If the format is given it will convert the image to the given file format.

Parameters:

  • file (Tempfile) —

    the file to manipulate

  • format (String) (defaults to: nil) —

    the file format to convert to

Returns:

  • (File) —

    the processed file



132
133
134
135
136
137
138
# File 'lib/refile/mini_magick.rb', line 132

def call(file, *args, format: nil, &block)
  img = ::MiniMagick::Image.new(file.path)
  img.format(format.to_s.downcase, nil) if format
  send(@method, img, *args, &block)

  ::File.open(img.path, "rb")
end

#convert(img, format) {|MiniMagick::Tool::Mogrify, MiniMagick::Tool::Convert| ... } ⇒ void

This method returns an undefined value.

Changes the image encoding format to the given format

Parameters:

  • img (MiniMagick::Image) —

    the image to convert

  • format (String) —

    the format to convert to

Yields:

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

See Also:



20
21
22
# File 'lib/refile/mini_magick.rb', line 20

def convert(img, format, &block)
  img.format(format.to_s.downcase, nil, &block)
end

#fill(img, width, height, gravity = "Center") {|MiniMagick::Tool::Mogrify, MiniMagick::Tool::Convert| ... } ⇒ void

This method returns an undefined value.

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:

  • img (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)

See Also:



75
76
77
78
79
80
81
82
83
84
# File 'lib/refile/mini_magick.rb', line 75

def fill(img, width, height, gravity = "Center")
  # We use `convert` to work around GraphicsMagick's absence of "gravity"
  ::MiniMagick::Tool::Convert.new do |cmd|
    yield cmd if block_given?
    cmd.resize "#{width}x#{height}^"
    cmd.gravity gravity
    cmd.extent "#{width}x#{height}"
    cmd.merge! [img.path, img.path]
  end
end

#fit(img, width, height) {|MiniMagick::Tool::Mogrify, MiniMagick::Tool::Convert| ... } ⇒ void

This method returns an undefined value.

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:

  • img (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)


52
53
54
55
56
57
# File 'lib/refile/mini_magick.rb', line 52

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

#limit(img, width, height) {|MiniMagick::Tool::Mogrify, MiniMagick::Tool::Convert| ... } ⇒ void

This method returns an undefined value.

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:

  • img (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)


35
36
37
38
39
40
# File 'lib/refile/mini_magick.rb', line 35

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

#pad(img, width, height, background = "transparent", gravity = "Center") {|MiniMagick::Tool::Mogrify, MiniMagick::Tool::Convert| ... } ⇒ void

This method returns an undefined value.

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:

  • img (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)

See Also:



107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/refile/mini_magick.rb', line 107

def pad(img, width, height, background = "transparent", gravity = "Center")
  # We use `convert` to work around GraphicsMagick's absence of "gravity"
  ::MiniMagick::Tool::Convert.new do |cmd|
    yield cmd if block_given?
    cmd.thumbnail "#{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}"
    cmd.merge! [img.path, img.path]
  end
end