diff --git a/lib/cooked_post_processor.rb b/lib/cooked_post_processor.rb index 8ce16d2c5aa..62f268bcaa6 100644 --- a/lib/cooked_post_processor.rb +++ b/lib/cooked_post_processor.rb @@ -70,11 +70,22 @@ class CookedPostProcessor end def limit_size!(img) - w, h = get_size_from_image_sizes(img["src"], @opts[:image_sizes]) || get_size(img["src"]) + # retrieve the size from + # 1) the width/height attributes + # 2) the dimension from the preview (image_sizes) + # 3) the dimension of the original image (HTTP request) + w, h = get_size_from_attributes(img) || + get_size_from_image_sizes(img["src"], @opts[:image_sizes]) || + get_size(img["src"]) # limit the size of the thumbnail img["width"], img["height"] = ImageSizer.resize(w, h) end + def get_size_from_attributes(img) + w, h = img["width"].to_i, img["height"].to_i + return [w, h] if w > 0 && h > 0 + end + def get_size_from_image_sizes(src, image_sizes) return unless image_sizes.present? image_sizes.each do |image_size| diff --git a/spec/components/cooked_post_processor_spec.rb b/spec/components/cooked_post_processor_spec.rb index 08a070214ca..02cc21f28f5 100644 --- a/spec/components/cooked_post_processor_spec.rb +++ b/spec/components/cooked_post_processor_spec.rb @@ -42,12 +42,20 @@ describe CookedPostProcessor do context "with image_sizes" do - let(:post) { build(:post_with_image_url) } + let(:post) { build(:post_with_image_urls) } let(:cpp) { CookedPostProcessor.new(post, image_sizes: {"http://foo.bar/image.png" => {"width" => 111, "height" => 222}}) } + before { cpp.post_process_images } + + it "adds the width from the image sizes provided when no dimension is provided" do + cpp.html.should =~ /src="http:\/\/foo.bar\/image.png" width="111" height="222"/ + end + it "adds the width from the image sizes provided" do - cpp.post_process_images - cpp.html.should =~ /width=\"111\"/ + cpp.html.should =~ /src="http:\/\/domain.com\/picture.jpg" width="50" height="42"/ + end + + it "should be dirty" do cpp.should be_dirty end diff --git a/spec/fabricators/post_fabricator.rb b/spec/fabricators/post_fabricator.rb index c34e1c5f534..c8e116e62f4 100644 --- a/spec/fabricators/post_fabricator.rb +++ b/spec/fabricators/post_fabricator.rb @@ -58,8 +58,11 @@ Fabricator(:post_with_unsized_images, from: :post) do ' end -Fabricator(:post_with_image_url, from: :post) do - cooked '' +Fabricator(:post_with_image_urls, from: :post) do + cooked ' + + +' end Fabricator(:post_with_large_image, from: :post) do