Class: MountableImageServer::ImageProcessor
- Inherits:
-
Object
- Object
- MountableImageServer::ImageProcessor
- Includes:
- Skeptick
- Defined in:
- lib/mountable_image_server/image_processor.rb
Constant Summary collapse
- VALID_PARAMETERS =
Accepted parameters and values: ‘fit` - Approach how to interpret width and height, use ’crop’ (cropping) or ‘clip’ (resizing) ‘w` - Width of image in pixels `h` - Height of image in pixels `q` - Quality of image, use value between 0 (worst) and 100 (best) `darken` - Blends image with black color, use value between 0 (no black) and 100 (completely black) `fm` - Format of image, use ’jpg’, ‘png’, ‘gif’, …
[:fit, :w, :h, :q, :darken, :fm]
- PARAMETER_VALUE_PATTERNS =
{ fit: /^(clip|crop)$/, h: /^\d+$/, w: /^\d+$/, q: /^([0-9]|[1-9][0-9]|100)$/, darken: /^([0-9]|[1-9][0-9]|100)$/, fm: /^(jpg|png|gif)$/, }
Instance Method Summary collapse
-
#initialize(path, parameters) ⇒ ImageProcessor
constructor
A new instance of ImageProcessor.
- #run(&block) ⇒ Object
Constructor Details
#initialize(path, parameters) ⇒ ImageProcessor
Returns a new instance of ImageProcessor.
27 28 29 30 31 |
# File 'lib/mountable_image_server/image_processor.rb', line 27 def initialize(path, parameters) @path = path @file_format = path.extname.downcase.scan(/[^\.]+/).last @parameters = sanitize_parameters(parameters) end |
Instance Method Details
#run(&block) ⇒ Object
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 |
# File 'lib/mountable_image_server/image_processor.rb', line 33 def run(&block) yield(Pathname(path)) and return unless parameters.any? parameters[:fit] ||= 'clip' if parameters[:fm] && parameters[:fm] == file_format parameters.delete(:fm) end if parameters[:fm] extension = ".#{parameters[:fm]}" else extension = ".#{file_format}" end if parameters[:fm] && parameters[:fm] != file_format && file_format == 'gif' input_path = "#{path}[0]" else input_path = path end operations_queue = [ format_operations, resize_operations, crop_operations, quality_operations, darken_operations, ].reduce([], :+) Tempfile.create(['processed-image', extension]) do |file| command = convert(input_path, to: file.path) do operations_queue.each do |operation| set *operation end end command.run yield(Pathname(file.path)) end end |