Class: FoggyMirror::Vips
- Inherits:
-
Object
- Object
- FoggyMirror::Vips
- Defined in:
- lib/foggy-mirror/adapters/vips.rb
Instance Method Summary collapse
- #color_samples(res) ⇒ Object
-
#data_uri(res, format: '.gif', options: {}) ⇒ Object
format and options are passed to Vips’ #write_to_buffer.
- #dominant_color ⇒ Object
-
#initialize(path) ⇒ Vips
constructor
A new instance of Vips.
Constructor Details
#initialize(path) ⇒ Vips
Returns a new instance of Vips.
12 13 14 |
# File 'lib/foggy-mirror/adapters/vips.rb', line 12 def initialize(path) @path = path end |
Instance Method Details
#color_samples(res) ⇒ Object
20 21 22 |
# File 'lib/foggy-mirror/adapters/vips.rb', line 20 def color_samples(res) square_image(res).to_a.flatten(1) end |
#data_uri(res, format: '.gif', options: {}) ⇒ Object
format and options are passed to Vips’ #write_to_buffer.
We use .gif as the default format as it’s by far the most size-efficient format for very small images. E.g. a 5x5 GIF is around 171 bytes, while an equivalent PNG would be around 2780 bytes, and JPEG would be larger still.
31 32 33 34 35 36 37 38 39 40 41 42 43 |
# File 'lib/foggy-mirror/adapters/vips.rb', line 31 def data_uri(res, format: '.gif', options: {}) image_type = if format.downcase.match?(/\.jpe?g/) 'jpeg' else format.downcase.match(/\.(png|gif)/)[1] end raise(ArgumentError, 'Unsupported image format') unless image_type base64 = Base64.strict_encode64(square_image(res).write_to_buffer(format, **)).chomp "data:image/#{image_type};base64,#{base64}" end |
#dominant_color ⇒ Object
16 17 18 |
# File 'lib/foggy-mirror/adapters/vips.rb', line 16 def dominant_color color_avg(load_image(DEFAULT_RESOLUTION)) end |