Class: DynamicImage::Format

Inherits:
Object
  • Object
show all
Defined in:
lib/dynamic_image/format.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, options) ⇒ Format

Returns a new instance of Format.



8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/dynamic_image/format.rb', line 8

def initialize(name, options)
  options = default_options.merge(options)

  @name = name
  @animated = options[:animated]
  @content_types = Array(options[:content_type])
  @extensions = Array(options[:extension])
  @magic_bytes = options[:magic_bytes].map do |s|
    s.dup.force_encoding("binary")
  end
  @signature = options[:signature]
  @save_options = options[:save_options]
end

Instance Attribute Details

#animatedObject (readonly)

Returns the value of attribute animated.



5
6
7
# File 'lib/dynamic_image/format.rb', line 5

def animated
  @animated
end

#content_typesObject (readonly)

Returns the value of attribute content_types.



5
6
7
# File 'lib/dynamic_image/format.rb', line 5

def content_types
  @content_types
end

#extensionsObject (readonly)

Returns the value of attribute extensions.



5
6
7
# File 'lib/dynamic_image/format.rb', line 5

def extensions
  @extensions
end

#magic_bytesObject (readonly)

Returns the value of attribute magic_bytes.



5
6
7
# File 'lib/dynamic_image/format.rb', line 5

def magic_bytes
  @magic_bytes
end

#nameObject (readonly)

Returns the value of attribute name.



5
6
7
# File 'lib/dynamic_image/format.rb', line 5

def name
  @name
end

#save_optionsObject (readonly)

Returns the value of attribute save_options.



5
6
7
# File 'lib/dynamic_image/format.rb', line 5

def save_options
  @save_options
end

#signatureObject (readonly)

Returns the value of attribute signature.



5
6
7
# File 'lib/dynamic_image/format.rb', line 5

def signature
  @signature
end

Class Method Details

.content_type(type) ⇒ Object



41
42
43
# File 'lib/dynamic_image/format.rb', line 41

def content_type(type)
  formats.filter { |f| f.content_types.include?(type) }.first
end

.content_typesObject



45
46
47
# File 'lib/dynamic_image/format.rb', line 45

def content_types
  formats.flat_map(&:content_types)
end

.find(name) ⇒ Object



49
50
51
52
53
# File 'lib/dynamic_image/format.rb', line 49

def find(name)
  key = name.to_s.upcase
  key = "JPEG" if key == "JPG"
  registered_formats[key]
end

.formatsObject



55
56
57
# File 'lib/dynamic_image/format.rb', line 55

def formats
  registered_formats.map { |_, f| f }
end

.register(name, **opts) ⇒ Object



59
60
61
# File 'lib/dynamic_image/format.rb', line 59

def register(name, **opts)
  registered_formats[name] = new(name, opts)
end

.sniff(bytes) ⇒ Object



63
64
65
66
67
# File 'lib/dynamic_image/format.rb', line 63

def sniff(bytes)
  return unless bytes

  formats.find { |format| format.matches?(bytes) }
end

Instance Method Details

#animated?Boolean

Returns:

  • (Boolean)


22
23
24
# File 'lib/dynamic_image/format.rb', line 22

def animated?
  animated
end

#content_typeObject



32
33
34
# File 'lib/dynamic_image/format.rb', line 32

def content_type
  content_types.first
end

#default_optionsObject



76
77
78
79
# File 'lib/dynamic_image/format.rb', line 76

def default_options
  { animated: false, content_type: [], extension: [], magic_bytes: [],
    signature: nil, save_options: {} }
end

#extensionObject



36
37
38
# File 'lib/dynamic_image/format.rb', line 36

def extension
  extensions.first
end

#matches?(bytes) ⇒ Boolean

Returns:

  • (Boolean)


26
27
28
29
30
# File 'lib/dynamic_image/format.rb', line 26

def matches?(bytes)
  return false unless magic_bytes.any? { |b| bytes.start_with?(b) }

  signature.nil? || signature.call(bytes)
end