diff options
| author | Sebastiano <[email protected]> | 2023-01-17 12:14:09 +0100 |
|---|---|---|
| committer | Koza <[email protected]> | 2023-04-12 17:25:36 +0200 |
| commit | 9bd3367b35bcc85f74c749cfbab05e9c430ab064 (patch) | |
| tree | 0412e3f2220a4632a11e48a43fe89d17fd833e63 /lib/axlsx/drawing | |
| parent | e548f377932207130cec4ac257a3907385d547d5 (diff) | |
| download | caxlsx-9bd3367b35bcc85f74c749cfbab05e9c430ab064.tar.gz caxlsx-9bd3367b35bcc85f74c749cfbab05e9c430ab064.zip | |
feat: add support for remote images
Diffstat (limited to 'lib/axlsx/drawing')
| -rw-r--r-- | lib/axlsx/drawing/pic.rb | 33 |
1 files changed, 28 insertions, 5 deletions
diff --git a/lib/axlsx/drawing/pic.rb b/lib/axlsx/drawing/pic.rb index fb4d01fe..b6d085de 100644 --- a/lib/axlsx/drawing/pic.rb +++ b/lib/axlsx/drawing/pic.rb @@ -7,6 +7,7 @@ module Axlsx # Creates a new Pic(ture) object # @param [Anchor] anchor the anchor that holds this image + # @@option options [Boolean] :remote indicates if image_src is a remote URI # @option options [String] :name # @option options [String] :descr # @option options [String] :image_src @@ -18,6 +19,7 @@ module Axlsx @anchor = anchor @hyperlink = nil @anchor.drawing.worksheet.workbook.images << self + @remote = options[:remote] parse_options options start_at(*options[:start_at]) if options[:start_at] yield self if block_given? @@ -54,6 +56,10 @@ module Axlsx # @return [Integer] attr_reader :opacity + # Flag for remote picture (from URI) + # @return [Boolean] + attr_reader :remote + # sets or updates a hyperlink for this image. # @param [String] v The href value for the hyper link # @option options @see Hyperlink#initialize All options available to the Hyperlink class apply - however href will be overridden with the v parameter value. @@ -71,8 +77,10 @@ module Axlsx def image_src=(v) Axlsx::validate_string(v) - RestrictionValidator.validate 'Pic.image_src', ALLOWED_MIME_TYPES, MimeTypeUtils.get_mime_type(v) - raise ArgumentError, "File does not exist" unless File.exist?(v) + unless remote? + RestrictionValidator.validate 'Pic.image_src', ALLOWED_MIME_TYPES, MimeTypeUtils.get_mime_type(v) + raise ArgumentError, "File does not exist" unless File.exist?(v) + end @image_src = v end @@ -83,10 +91,17 @@ module Axlsx # @see descr def descr=(v) Axlsx::validate_string(v); @descr = v; end + # @see remote + def remote=(v) @remote = v; end + + def remote? + remote == true; + end + # The file name of image_src without any path information # @return [String] def file_name - File.basename(image_src) unless image_src.nil? + File.basename(image_src) unless remote? || image_src.nil? end # returns the extension of image_src without the preceeding '.' @@ -110,7 +125,11 @@ module Axlsx # The relationship object for this pic. # @return [Relationship] def relationship - Relationship.new(self, IMAGE_R, "../#{pn}") + if remote? + Relationship.new(self, IMAGE_R, "#{image_src}", target_mode: :External) + else + Relationship.new(self, IMAGE_R, "../#{pn}") + end end # providing access to the anchor's width attribute @@ -174,7 +193,11 @@ module Axlsx 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 remote? + str << ('<a:blip xmlns:r ="' << XML_NS_R << '" r:link="' << relationship.Id << '" >') + else + str << ('<a:blip xmlns:r ="' << XML_NS_R << '" r:embed="' << relationship.Id << '">') + end if opacity str << "<a:alphaModFix amt=\"#{opacity}\"/>" end |
