Class: ODFWriter::Image

Inherits:
Field
  • Object
show all
Defined in:
lib/odf_writer/image.rb

Overview

Image: replace images

Constant Summary collapse

IMAGE_DIR_NAME =

constants

"Pictures"

Constants inherited from Field

Field::DELIMITERS

Instance Attribute Summary

Attributes inherited from Field

#name

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Field

#field, #initialize, #value

Constructor Details

This class inherits a constructor from ODFWriter::Field

Class Method Details

.unique_image_names(doc) ⇒ Object

def



90
91
92
93
94
95
96
97
# File 'lib/odf_writer/image.rb', line 90

def self.unique_image_names(doc)
  nodes   = doc.xpath("//draw:frame[@draw:name='#{@name}']")
  padding = Math.log10(nodes.length).to_i + 1 if nodes.present?
  nodes.each_with_index do |node, i|
    num = "%.#{padding}i" % i
    node.attribute('name').value = "IMAGE_#{num}_" + node.attribute('name').value
  end
end

Instance Method Details

#replace!(doc, manifest, template, item = nil) ⇒ Object

replace!



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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/odf_writer/image.rb', line 45

def replace!(doc, manifest, template, item=nil )

  image_data = value(item)
  
  if image_data = is_image?(image_data) 
    
    # find placeholder image
    nodes = find_image_nodes( doc )
    return if nodes.blank?
    
    # find manifest
    man = manifest.xpath("//manifest:manifest") rescue nil
    return if man.blank?
    
    # each placeholder image
    nodes.each do |node|
    
      # create unique filename for image in .odt file
      path = ::File.join(IMAGE_DIR_NAME, "#{SecureRandom.hex(20).upcase}#{::File.extname(image_data[:filename])}")
      mime = Rack::Mime.mime_type(File.extname(image_data[:filename]))
      
      # set path and mime type of placeholder image
      node.attribute('href').value = path
      if node.attribute('mime-type').present?
        node.attribute('mime-type').value = mime
      else
        node.set_attribute('mime-type', mime)
      end
      
      # set width and height of placeholder image
      parent = node.parent
      if parent.name == "frame"
        width  = parent.attribute('width').value
        height = parent.attribute('height').value
        parent.attribute('height').value = recalc_height(:x => image_data[:width], :y => image_data[:height], :newx => width, :newy => height)
      end
      
      # add image to .odt file
      add_image_file( image_data[:bytes], path, mime, doc, man, template )
      
    end
  end
  
end