Class: RubyDocx::Elements::Image

Inherits:
Element
  • Object
show all
Defined in:
lib/ruby_docx/elements/image.rb

Instance Attribute Summary collapse

Attributes inherited from Element

#doc, #grid, #node

Instance Method Summary collapse

Methods inherited from Element

#elements, #initialize, #inspect, #to_s, #to_xml

Constructor Details

This class inherits a constructor from RubyDocx::Elements::Element

Instance Attribute Details

#heightObject (readonly)

Returns the value of attribute height.



7
8
9
# File 'lib/ruby_docx/elements/image.rb', line 7

def height
  @height
end

Returns the value of attribute link.



6
7
8
# File 'lib/ruby_docx/elements/image.rb', line 6

def link
  @link
end

#pathObject

Returns the value of attribute path.



6
7
8
# File 'lib/ruby_docx/elements/image.rb', line 6

def path
  @path
end

#widthObject (readonly)

Returns the value of attribute width.



7
8
9
# File 'lib/ruby_docx/elements/image.rb', line 7

def width
  @width
end

#zipObject

Returns the value of attribute zip.



6
7
8
# File 'lib/ruby_docx/elements/image.rb', line 6

def zip
  @zip
end

Instance Method Details

#base64_dataObject



54
55
56
# File 'lib/ruby_docx/elements/image.rb', line 54

def base64_data
  "data:#{self.mime};base64,#{Base64.strict_encode64(self.data)}"
end

#dataObject



9
10
11
12
13
14
15
16
# File 'lib/ruby_docx/elements/image.rb', line 9

def data
  if self.path
    @zip.read("word/#{@path}")
  else

  end

end

#mimeObject



50
51
52
# File 'lib/ruby_docx/elements/image.rb', line 50

def mime
  MimeMagic.by_magic(self.data).type
end

#relation_idObject



18
19
20
21
22
23
24
25
# File 'lib/ruby_docx/elements/image.rb', line 18

def relation_id
  element = @node.xpath(".//v:imagedata").first
  if element && element.attributes.keys.index("id")
    element.attributes["id"].value
  else
    nil
  end
end

#replace(lnk) ⇒ Object



27
28
29
# File 'lib/ruby_docx/elements/image.rb', line 27

def replace(lnk)
  @link = lnk
end

#save(path) ⇒ Object



67
68
69
70
71
72
73
# File 'lib/ruby_docx/elements/image.rb', line 67

def save(path)
  if @zip && @path
    file = File.new(path, "wb")
    file.write(self.data)
    file.close
  end
end

#styleObject



31
32
33
34
35
36
37
38
39
# File 'lib/ruby_docx/elements/image.rb', line 31

def style
  element = @node.xpath(".//v:shape").first

  if element && element.attributes.keys.index("style")
    element.attributes["style"].value
  else
    nil
  end
end

#titleObject



41
42
43
44
45
46
47
48
# File 'lib/ruby_docx/elements/image.rb', line 41

def title
  element = @node.xpath(".//v:imagedata").first
  if element && element.attributes.keys.index("title")
    element.attributes["title"].value
  else
    nil
  end
end

#to_htmlObject



58
59
60
61
62
63
64
65
# File 'lib/ruby_docx/elements/image.rb', line 58

def to_html
  if @link.to_s.size > 0
    "<img src='#{@link}' alt=\"#{self.title}\" style=\"#{self.style}\" />"
  else
    "<img src='#{self.base64_data}' alt=\"#{self.title}\" style=\"#{self.style}\" />"
  end

end