From 9bd3367b35bcc85f74c749cfbab05e9c430ab064 Mon Sep 17 00:00:00 2001 From: Sebastiano Date: Tue, 17 Jan 2023 12:14:09 +0100 Subject: feat: add support for remote images --- lib/axlsx/drawing/pic.rb | 33 ++++++++++++++++++++++++++++----- lib/axlsx/package.rb | 2 +- 2 files changed, 29 insertions(+), 6 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 << '' str << '' - str << ('') + if remote? + str << ('') + else + str << ('') + end if opacity str << "" end diff --git a/lib/axlsx/package.rb b/lib/axlsx/package.rb index 2118d0ea..b61c3de1 100644 --- a/lib/axlsx/package.rb +++ b/lib/axlsx/package.rb @@ -247,7 +247,7 @@ module Axlsx end workbook.images.each do |image| - parts << { :entry => "xl/#{image.pn}", :path => image.image_src } + parts << { :entry => "xl/#{image.pn}", :path => image.image_src } unless image.remote? end if use_shared_strings -- cgit v1.2.3