Class: Refile::ImageProcessor
- Inherits:
-
Object
- Object
- Refile::ImageProcessor
- Defined in:
- lib/refile/image_processing.rb
Instance Method Summary collapse
- #call(file, *args, format: nil) ⇒ Object
- #convert(img, format) ⇒ Object
- #fill(img, width, height, gravity = "Center") ⇒ Object
- #fit(img, width, height) ⇒ Object
-
#initialize(method) ⇒ ImageProcessor
constructor
A new instance of ImageProcessor.
- #limit(img, width, height) ⇒ Object
- #pad(img, width, height, background = "transparent", gravity = "Center") ⇒ Object
Constructor Details
#initialize(method) ⇒ ImageProcessor
Returns a new instance of ImageProcessor.
6 7 8 |
# File 'lib/refile/image_processing.rb', line 6 def initialize(method) @method = method end |
Instance Method Details
#call(file, *args, format: nil) ⇒ Object
62 63 64 65 66 67 68 69 70 71 |
# File 'lib/refile/image_processing.rb', line 62 def call(file, *args, format: nil) path = file.download.path img = ::MiniMagick::Image.open(path) img.format(format.to_s.downcase) if format send(@method, img, *args) img.write(path) ::File.open(path, "rb") end |
#convert(img, format) ⇒ Object
10 11 12 |
# File 'lib/refile/image_processing.rb', line 10 def convert(img, format) img.format(format.to_s.downcase) end |
#fill(img, width, height, gravity = "Center") ⇒ Object
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
# File 'lib/refile/image_processing.rb', line 25 def fill(img, width, height, gravity = "Center") 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) ⇒ Object
18 19 20 |
# File 'lib/refile/image_processing.rb', line 18 def fit(img, width, height) img.resize "#{width}x#{height}" end |
#limit(img, width, height) ⇒ Object
14 15 16 |
# File 'lib/refile/image_processing.rb', line 14 def limit(img, width, height) img.resize "#{width}x#{height}>" end |
#pad(img, width, height, background = "transparent", gravity = "Center") ⇒ Object
49 50 51 52 53 54 55 56 57 58 59 60 |
# File 'lib/refile/image_processing.rb', line 49 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 |