Class: Refile::ImageProcessor
- Inherits:
-
Object
- Object
- Refile::ImageProcessor
- Defined in:
- lib/refile/image_processing.rb
Overview
Processes images via MiniMagick, resizing cropping and padding them.
Instance Method Summary collapse
-
#call(file, *args, format: nil) ⇒ File
Process the given file.
-
#convert(img, format)
Changes the image encoding format to the given format.
-
#fill(img, width, height, gravity = "Center")
Resize the image so that it is at least as large in both dimensions as specified, then crops any excess outside the specified dimensions.
-
#fit(img, width, height)
Resize the image to fit within the specified dimensions while retaining the original aspect ratio.
-
#initialize(method) ⇒ ImageProcessor
constructor
A new instance of ImageProcessor.
-
#limit(img, width, height)
Resize the image to fit within the specified dimensions while retaining the original aspect ratio.
-
#pad(img, width, height, background = "transparent", gravity = "Center")
resize the image to fit within the specified dimensions while retaining the original aspect ratio in the same way as #fill.
Constructor Details
#initialize(method) ⇒ ImageProcessor
Returns a new instance of ImageProcessor.
8 9 10 |
# File 'lib/refile/image_processing.rb', line 8 def initialize(method) @method = method end |
Instance Method Details
#call(file, *args, format: nil) ⇒ 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.
131 132 133 134 135 136 137 |
# File 'lib/refile/image_processing.rb', line 131 def call(file, *args, format: nil) img = ::MiniMagick::Image.new(file.path) img.format(format.to_s.downcase, nil) if format send(@method, img, *args) ::File.open(img.path, "rb") end |
#convert(img, format)
This method returns an undefined value.
Changes the image encoding format to the given format
18 19 20 |
# File 'lib/refile/image_processing.rb', line 18 def convert(img, format) img.format(format.to_s.downcase, nil) end |
#fill(img, width, height, gravity = "Center")
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.
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 |
# File 'lib/refile/image_processing.rb', line 64 def fill(img, width, height, gravity = "Center") # FIXME: test and rewrite to simpler implementation! width = width.to_i height = height.to_i cols, rows = img[:dimensions] img. do |cmd| if width != cols || height != rows scale_x = width / cols.to_f scale_y = height / rows.to_f if scale_x >= scale_y cols = (scale_x * (cols + 0.5)).round rows = (scale_x * (rows + 0.5)).round cmd.resize "#{cols}" else cols = (scale_y * (cols + 0.5)).round rows = (scale_y * (rows + 0.5)).round cmd.resize "x#{rows}" end end cmd.gravity gravity cmd.background "rgba(255,255,255,0.0)" cmd.extent "#{width}x#{height}" if cols != width || rows != height end end |
#fit(img, width, height)
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.
45 46 47 |
# File 'lib/refile/image_processing.rb', line 45 def fit(img, width, height) img.resize "#{width}x#{height}" end |
#limit(img, width, height)
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.
32 33 34 |
# File 'lib/refile/image_processing.rb', line 32 def limit(img, width, height) img.resize "#{width}x#{height}>" end |
#pad(img, width, height, background = "transparent", gravity = "Center")
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.
109 110 111 112 113 114 115 116 117 118 119 120 |
# File 'lib/refile/image_processing.rb', line 109 def pad(img, width, height, background = "transparent", gravity = "Center") img. do |cmd| 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}" end end |