Class: Axlsx::Pic

Inherits:
Object
  • Object
show all
Includes:
OptionsParser
Defined in:
lib/axlsx/drawing/pic.rb

Overview

a Pic object represents an image in your worksheet Worksheet#add_image is the recommended way to manage images in your sheets

Constant Summary collapse

ALLOWED_MIME_TYPES =

allowed mime types

%w(image/jpeg image/png image/gif)

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from OptionsParser

#parse_options

Constructor Details

#initialize(anchor, options = {}) {|_self| ... } ⇒ Pic

Creates a new Pic(ture) object

Parameters:

  • anchor (Anchor)

    the anchor that holds this image

  • options (Hash) (defaults to: {})

    a customizable set of options

Options Hash (options):

  • :name (String)
  • :descr (String)
  • :image_src (String)
  • :start_at (Array)
  • :width (Integer)
  • :height (Integer)
  • :opacity (Float)
    • set the picture opacity, accepts a value between 0.0 and 1.0

Yields:

  • (_self)

Yield Parameters:

  • _self (Axlsx::Pic)

    the object that the method was called on



19
20
21
22
23
24
25
26
27
28
# File 'lib/axlsx/drawing/pic.rb', line 19

def initialize(anchor, options={})
  @anchor = anchor
  @hyperlink = nil
  @anchor.drawing.worksheet.workbook.images << self
  parse_options options
  start_at(*options[:start_at]) if options[:start_at]
  yield self if block_given?
  @picture_locking = PictureLocking.new(options)
  @opacity = (options[:opacity] * 100000).round if options[:opacity]
end

Instance Attribute Details

#anchorOneCellAnchor (readonly)

The anchor for this image

Returns:



48
49
50
# File 'lib/axlsx/drawing/pic.rb', line 48

def anchor
  @anchor
end

#descrString

A description of the picture

Returns:

  • (String)


39
40
41
# File 'lib/axlsx/drawing/pic.rb', line 39

def descr
  @descr
end

Returns the value of attribute hyperlink.



53
54
55
# File 'lib/axlsx/drawing/pic.rb', line 53

def hyperlink
  @hyperlink
end

#image_srcString

The path to the image you want to include Only local images are supported at this time.

Returns:

  • (String)


44
45
46
# File 'lib/axlsx/drawing/pic.rb', line 44

def image_src
  @image_src
end

#nameString

The name to use for this picture

Returns:

  • (String)


35
36
37
# File 'lib/axlsx/drawing/pic.rb', line 35

def name
  @name
end

#opacityInteger (readonly)

Picture opacity

Returns:

  • (Integer)


57
58
59
# File 'lib/axlsx/drawing/pic.rb', line 57

def opacity
  @opacity
end

#picture_lockingObject (readonly)

The picture locking attributes for this picture



51
52
53
# File 'lib/axlsx/drawing/pic.rb', line 51

def picture_locking
  @picture_locking
end

Instance Method Details

#end_at(x, y = nil) ⇒ Marker

noop if not using a two cell anchor

Parameters:

  • x (Integer)

    The column

  • y (Integer) (defaults to: nil)

    The row

Returns:



159
160
161
162
163
# File 'lib/axlsx/drawing/pic.rb', line 159

def end_at(x, y=nil)
  use_two_cell_anchor unless @anchor.is_a?(TwoCellAnchor)
  @anchor.end_at x, y
  @anchor.to
end

#extnameString

returns the extension of image_src without the preceeding '.'

Returns:

  • (String)


95
96
97
# File 'lib/axlsx/drawing/pic.rb', line 95

def extname
  File.extname(image_src).delete('.') unless image_src.nil?
end

#file_nameString

The file name of image_src without any path information

Returns:

  • (String)


89
90
91
# File 'lib/axlsx/drawing/pic.rb', line 89

def file_name
  File.basename(image_src) unless image_src.nil?
end

#heightObject

Note:

this is a noop if you are using a TwoCellAnchor

providing access to update the anchor's height attribute



133
134
135
# File 'lib/axlsx/drawing/pic.rb', line 133

def height
  @anchor.height
end

#height=(v) ⇒ Object

Note:

This is a noop if you are using a TwoCellAnchor

See Also:



139
140
141
142
# File 'lib/axlsx/drawing/pic.rb', line 139

def height=(v)
  use_one_cell_anchor unless @anchor.is_a?(OneCellAnchor)
  @anchor.height = v
end

#indexIndex

The index of this image in the workbooks images collections

Returns:

  • (Index)


101
102
103
# File 'lib/axlsx/drawing/pic.rb', line 101

def index
  @anchor.drawing.worksheet.workbook.images.index(self)
end

#pnString

The part name for this image used in serialization and relationship building

Returns:

  • (String)


107
108
109
# File 'lib/axlsx/drawing/pic.rb', line 107

def pn
  "#{IMAGE_PN % [(index+1), extname]}"
end

#relationshipRelationship

The relationship object for this pic.

Returns:



113
114
115
# File 'lib/axlsx/drawing/pic.rb', line 113

def relationship
  Relationship.new(self, IMAGE_R, "../#{pn}")
end

#start_at(x, y = nil) ⇒ Marker

This is a short cut method to set the start anchor position If you need finer granularity in positioning use graphic_frame.anchor.from.colOff / rowOff

Parameters:

  • x (Integer)

    The column

  • y (Integer) (defaults to: nil)

    The row

Returns:



150
151
152
153
# File 'lib/axlsx/drawing/pic.rb', line 150

def start_at(x, y=nil)
  @anchor.start_at x, y
  @anchor.from
end

#to_xml_string(str = '') ⇒ String

Serializes the object

Parameters:

  • str (String) (defaults to: '')

Returns:

  • (String)


168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
# File 'lib/axlsx/drawing/pic.rb', line 168

def to_xml_string(str = '')
  str << '<xdr:pic>'
  str << '<xdr:nvPicPr>'
  str << ('<xdr:cNvPr id="2" name="' << name.to_s << '" descr="' << descr.to_s << '">')
  hyperlink.to_xml_string(str) if hyperlink.is_a?(Hyperlink)
  str << '</xdr:cNvPr><xdr:cNvPicPr>'
  picture_locking.to_xml_string(str)
  str << '</xdr:cNvPicPr></xdr:nvPicPr>'
  str << '<xdr:blipFill>'
  str << ('<a:blip xmlns:r ="' << XML_NS_R << '" r:embed="' << relationship.Id << '">')
  if opacity
    str << "<a:alphaModFix amt=\"#{opacity}\"/>"
  end
  str << '</a:blip>'
  str << '<a:stretch><a:fillRect/></a:stretch></xdr:blipFill><xdr:spPr>'
  str << '<a:xfrm><a:off x="0" y="0"/><a:ext cx="2336800" cy="2161540"/></a:xfrm>'
  str << '<a:prstGeom prst="rect"><a:avLst/></a:prstGeom></xdr:spPr></xdr:pic>'
end

#widthObject

providing access to the anchor's width attribute



119
120
121
122
# File 'lib/axlsx/drawing/pic.rb', line 119

def width
  return unless @anchor.is_a?(OneCellAnchor)
  @anchor.width
end

#width=(v) ⇒ Object

See Also:



125
126
127
128
# File 'lib/axlsx/drawing/pic.rb', line 125

def width=(v)
  use_one_cell_anchor unless @anchor.is_a?(OneCellAnchor)
  @anchor.width = v
end