diff --git a/app/models/upload.rb b/app/models/upload.rb index be740f9e687..bb6ba200174 100644 --- a/app/models/upload.rb +++ b/app/models/upload.rb @@ -224,7 +224,11 @@ class Upload < ActiveRecord::Base end begin - w, h = FastImage.new(path, raise_on_failure: true).size + if extension == 'svg' + w, h = Discourse::Utils.execute_command("identify", "-format", "%w %h", path).split(' ') rescue [0, 0] + else + w, h = FastImage.new(path, raise_on_failure: true).size + end self.width = w || 0 self.height = h || 0 diff --git a/lib/upload_creator.rb b/lib/upload_creator.rb index 06ea0606614..b67c7a6ee53 100644 --- a/lib/upload_creator.rb +++ b/lib/upload_creator.rb @@ -124,8 +124,14 @@ class UploadCreator @upload.extension = image_type || File.extname(@filename)[1..10] if is_image - @upload.thumbnail_width, @upload.thumbnail_height = ImageSizer.resize(*@image_info.size) - @upload.width, @upload.height = @image_info.size + if @image_info.type.to_s == 'svg' + w, h = Discourse::Utils.execute_command("identify", "-format", "%w %h", @file.path).split(' ') rescue [0, 0] + else + w, h = @image_info.size + end + + @upload.thumbnail_width, @upload.thumbnail_height = ImageSizer.resize(w, h) + @upload.width, @upload.height = w, h @upload.animated = animated? end diff --git a/spec/fixtures/images/pencil.svg b/spec/fixtures/images/pencil.svg new file mode 100644 index 00000000000..a16d2a9b299 --- /dev/null +++ b/spec/fixtures/images/pencil.svg @@ -0,0 +1,245 @@ + + + + + + + + + + + + + + image/svg+xml + + + + + + + + + + + + + + + + + + + + + + + + + + + + + Alfredit Designs + + + + diff --git a/spec/lib/upload_creator_spec.rb b/spec/lib/upload_creator_spec.rb index 8d5cee79520..8072e61e95a 100644 --- a/spec/lib/upload_creator_spec.rb +++ b/spec/lib/upload_creator_spec.rb @@ -535,6 +535,20 @@ RSpec.describe UploadCreator do end end + describe "svg sizing" do + let(:svg_filename) { "pencil.svg" } + let(:svg_file) { file_from_fixtures(svg_filename) } + + it "should handle units in width and height" do + upload = UploadCreator.new(svg_file, svg_filename, + force_optimize: true, + ).create_for(user.id) + + expect(upload.width).to be > 100 + expect(upload.height).to be > 100 + end + end + describe '#should_downsize?' do context "GIF image" do let(:gif_file) { file_from_fixtures("animated.gif") }