Class: OggAlbumTagger::Picture

Inherits:
Object
  • Object
show all
Defined in:
lib/ogg_album_tagger/picture.rb

Class Method Summary collapse

Class Method Details

.generate_metadata_block_picture(image, desc = '') ⇒ Object

Embed a picture so that it can be included as a tag.

See xiph.org/flac/format.html#metadata_block_picture Note: the type of the picture is currently fixed to “Front cover”, as it is the most common type.



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/ogg_album_tagger/picture.rb', line 13

def self.(image, desc = '')
  begin
    image = File.expand_path(image)
    img = Exiftool.new(image)
    content = IO.binread(image)
  rescue Exiftool::ExiftoolNotInstalled
    raise OggAlbumTagger::SystemError, "exiftool (the executable, not the gem) is not in your path, please install it."
  rescue
    raise OggAlbumTagger::ArgumentError, "\"#{image}\" cannot be read."
  end

  meta = img.results[0].to_hash
  mime = meta[:mime_type]

  raise OggAlbumTagger::ArgumentError, 'Unsupported image type. Use JPEG or PNG.' unless ['image/png', 'image/jpeg'].include?(mime)

  pack = [
    3, # Front cover
    mime.length,
    mime,
    desc.bytesize,
    desc,
    meta[:image_width],
    meta[:image_height],
    meta[:color_components] * meta[:bits_per_sample],
    0, # palette
    content.length,
    content
  ].pack(sprintf("L>L>A%dL>a%dL>L>L>L>L>a*", mime.length, desc.bytesize))
  Base64.strict_encode64(pack)
end