Class: Axlsx::Pic
- Inherits:
-
Object
- Object
- Axlsx::Pic
- 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).freeze
Instance Attribute Summary collapse
-
#anchor ⇒ OneCellAnchor
readonly
The anchor for this image.
-
#descr ⇒ String
A description of the picture.
-
#hyperlink ⇒ Object
Returns the value of attribute hyperlink.
-
#image_src ⇒ String
The path to the image you want to include Only local images are supported at this time.
-
#name ⇒ String
The name to use for this picture.
-
#opacity ⇒ Integer
readonly
Picture opacity.
-
#picture_locking ⇒ Object
readonly
The picture locking attributes for this picture.
-
#remote ⇒ Boolean
Flag for remote picture (from URI).
Instance Method Summary collapse
-
#end_at(x, y = nil) ⇒ Marker
noop if not using a two cell anchor.
-
#extname ⇒ String
returns the extension of image_src without the preceeding '.'.
-
#file_name ⇒ String
The file name of image_src without any path information.
-
#height ⇒ Object
providing access to update the anchor's height attribute.
- #height=(v) ⇒ Object
-
#index ⇒ Index
The index of this image in the workbooks images collections.
-
#initialize(anchor, options = {}) {|_self| ... } ⇒ Pic
constructor
Creates a new Pic(ture) object.
-
#pn ⇒ String
The part name for this image used in serialization and relationship building.
-
#relationship ⇒ Relationship
The relationship object for this pic.
- #remote? ⇒ Boolean
-
#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.
-
#to_xml_string(str = +'')) ⇒ String
Serializes the object.
-
#width ⇒ Object
providing access to the anchor's width attribute.
- #width=(v) ⇒ Object
Methods included from OptionsParser
Constructor Details
#initialize(anchor, options = {}) {|_self| ... } ⇒ Pic
Creates a new Pic(ture) object
20 21 22 23 24 25 26 27 28 29 30 |
# File 'lib/axlsx/drawing/pic.rb', line 20 def initialize(anchor, = {}) @anchor = anchor @hyperlink = nil @anchor.drawing.worksheet.workbook.images << self @remote = [:remote] start_at(*[:start_at]) if [:start_at] yield self if block_given? @picture_locking = PictureLocking.new() @opacity = ([:opacity] * 100000).round if [:opacity] end |
Instance Attribute Details
#anchor ⇒ OneCellAnchor (readonly)
The anchor for this image
50 51 52 |
# File 'lib/axlsx/drawing/pic.rb', line 50 def anchor @anchor end |
#descr ⇒ String
A description of the picture
41 42 43 |
# File 'lib/axlsx/drawing/pic.rb', line 41 def descr @descr end |
#hyperlink ⇒ Object
Returns the value of attribute hyperlink.
55 56 57 |
# File 'lib/axlsx/drawing/pic.rb', line 55 def hyperlink @hyperlink end |
#image_src ⇒ String
The path to the image you want to include Only local images are supported at this time.
46 47 48 |
# File 'lib/axlsx/drawing/pic.rb', line 46 def image_src @image_src end |
#name ⇒ String
The name to use for this picture
37 38 39 |
# File 'lib/axlsx/drawing/pic.rb', line 37 def name @name end |
#opacity ⇒ Integer (readonly)
Picture opacity
59 60 61 |
# File 'lib/axlsx/drawing/pic.rb', line 59 def opacity @opacity end |
#picture_locking ⇒ Object (readonly)
The picture locking attributes for this picture
53 54 55 |
# File 'lib/axlsx/drawing/pic.rb', line 53 def picture_locking @picture_locking end |
#remote ⇒ Boolean
Flag for remote picture (from URI)
63 64 65 |
# File 'lib/axlsx/drawing/pic.rb', line 63 def remote @remote end |
Instance Method Details
#end_at(x, y = nil) ⇒ Marker
noop if not using a two cell anchor
192 193 194 195 196 |
# File 'lib/axlsx/drawing/pic.rb', line 192 def end_at(x, y = nil) use_two_cell_anchor unless @anchor.is_a?(TwoCellAnchor) @anchor.end_at x, y @anchor.to end |
#extname ⇒ String
returns the extension of image_src without the preceeding '.'
123 124 125 |
# File 'lib/axlsx/drawing/pic.rb', line 123 def extname File.extname(image_src).delete('.') unless image_src.nil? end |
#file_name ⇒ String
The file name of image_src without any path information
117 118 119 |
# File 'lib/axlsx/drawing/pic.rb', line 117 def file_name File.basename(image_src) unless remote? || image_src.nil? end |
#height ⇒ Object
this is a noop if you are using a TwoCellAnchor
providing access to update the anchor's height attribute
166 167 168 |
# File 'lib/axlsx/drawing/pic.rb', line 166 def height @anchor.height end |
#height=(v) ⇒ Object
This is a noop if you are using a TwoCellAnchor
172 173 174 175 |
# File 'lib/axlsx/drawing/pic.rb', line 172 def height=(v) use_one_cell_anchor unless @anchor.is_a?(OneCellAnchor) @anchor.height = v end |
#index ⇒ Index
The index of this image in the workbooks images collections
129 130 131 |
# File 'lib/axlsx/drawing/pic.rb', line 129 def index @anchor.drawing.worksheet.workbook.images.index(self) end |
#pn ⇒ String
The part name for this image used in serialization and relationship building
135 136 137 |
# File 'lib/axlsx/drawing/pic.rb', line 135 def pn format(IMAGE_PN, index + 1, extname) end |
#relationship ⇒ Relationship
The relationship object for this pic.
141 142 143 144 145 146 147 |
# File 'lib/axlsx/drawing/pic.rb', line 141 def relationship if remote? Relationship.new(self, IMAGE_R, image_src.to_s, target_mode: :External) else Relationship.new(self, IMAGE_R, "../#{pn}") end end |
#remote? ⇒ Boolean
111 112 113 |
# File 'lib/axlsx/drawing/pic.rb', line 111 def remote? remote == 1 || remote.to_s == 'true' 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
183 184 185 186 |
# File 'lib/axlsx/drawing/pic.rb', line 183 def start_at(x, y = nil) @anchor.start_at x, y @anchor.from end |
#to_xml_string(str = +'')) ⇒ String
Serializes the object
201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 |
# File 'lib/axlsx/drawing/pic.rb', line 201 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 << relationship_xml_portion 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 |
#width ⇒ Object
providing access to the anchor's width attribute
151 152 153 154 155 |
# File 'lib/axlsx/drawing/pic.rb', line 151 def width return unless @anchor.is_a?(OneCellAnchor) @anchor.width end |
#width=(v) ⇒ Object
158 159 160 161 |
# File 'lib/axlsx/drawing/pic.rb', line 158 def width=(v) use_one_cell_anchor unless @anchor.is_a?(OneCellAnchor) @anchor.width = v end |