mirror of
https://github.com/discourse/discourse.git
synced 2024-11-25 09:42:07 +08:00
FIX: Optimize quoted images (#8427)
Only images that were part of a lightbox used to be optimized. This patch ensures that quoted images are also optimized.
This commit is contained in:
parent
8237e0e001
commit
1e0c2235a3
|
@ -208,9 +208,7 @@ class CookedPostProcessor
|
|||
# minus emojis
|
||||
@doc.css("img.emoji") -
|
||||
# minus oneboxed images
|
||||
oneboxed_images -
|
||||
# minus images inside quotes
|
||||
@doc.css(".quote img")
|
||||
oneboxed_images
|
||||
end
|
||||
|
||||
def extract_images_for_post
|
||||
|
@ -348,7 +346,8 @@ class CookedPostProcessor
|
|||
end
|
||||
end
|
||||
|
||||
add_lightbox!(img, original_width, original_height, upload, cropped: crop)
|
||||
add_lightbox!(img, original_width, original_height, upload, cropped: crop) if img.ancestors('.quote').blank?
|
||||
optimize_image!(img, upload, cropped: crop) if upload
|
||||
end
|
||||
|
||||
def loading_image(upload)
|
||||
|
@ -373,6 +372,38 @@ class CookedPostProcessor
|
|||
.each { |r| yield r if r > 1 }
|
||||
end
|
||||
|
||||
def optimize_image!(img, upload, cropped: false)
|
||||
w, h = img["width"].to_i, img["height"].to_i
|
||||
|
||||
thumbnail = upload.thumbnail(w, h)
|
||||
if thumbnail && thumbnail.filesize.to_i < upload.filesize
|
||||
img["src"] = thumbnail.url
|
||||
|
||||
srcset = +""
|
||||
|
||||
each_responsive_ratio do |ratio|
|
||||
resized_w = (w * ratio).to_i
|
||||
resized_h = (h * ratio).to_i
|
||||
|
||||
if !cropped && upload.width && resized_w > upload.width
|
||||
cooked_url = UrlHelper.cook_url(upload.url, secure: upload.secure?)
|
||||
srcset << ", #{cooked_url} #{ratio.to_s.sub(/\.0$/, "")}x"
|
||||
elsif t = upload.thumbnail(resized_w, resized_h)
|
||||
cooked_url = UrlHelper.cook_url(t.url, secure: upload.secure?)
|
||||
srcset << ", #{cooked_url} #{ratio.to_s.sub(/\.0$/, "")}x"
|
||||
end
|
||||
|
||||
img["srcset"] = "#{UrlHelper.cook_url(img["src"], secure: upload.secure?)}#{srcset}" if srcset.present?
|
||||
end
|
||||
else
|
||||
img["src"] = upload.url
|
||||
end
|
||||
|
||||
if small_upload = loading_image(upload)
|
||||
img["data-small-upload"] = small_upload.url
|
||||
end
|
||||
end
|
||||
|
||||
def add_lightbox!(img, original_width, original_height, upload, cropped: false)
|
||||
# first, create a div to hold our lightbox
|
||||
lightbox = create_node("div", LIGHTBOX_WRAPPER_CSS_CLASS)
|
||||
|
@ -389,39 +420,6 @@ class CookedPostProcessor
|
|||
|
||||
a.add_child(img)
|
||||
|
||||
# replace the image by its thumbnail
|
||||
w, h = img["width"].to_i, img["height"].to_i
|
||||
|
||||
if upload
|
||||
thumbnail = upload.thumbnail(w, h)
|
||||
if thumbnail && thumbnail.filesize.to_i < upload.filesize
|
||||
img["src"] = thumbnail.url
|
||||
|
||||
srcset = +""
|
||||
|
||||
each_responsive_ratio do |ratio|
|
||||
resized_w = (w * ratio).to_i
|
||||
resized_h = (h * ratio).to_i
|
||||
|
||||
if !cropped && upload.width && resized_w > upload.width
|
||||
cooked_url = UrlHelper.cook_url(upload.url, secure: upload.secure?)
|
||||
srcset << ", #{cooked_url} #{ratio.to_s.sub(/\.0$/, "")}x"
|
||||
elsif t = upload.thumbnail(resized_w, resized_h)
|
||||
cooked_url = UrlHelper.cook_url(t.url, secure: upload.secure?)
|
||||
srcset << ", #{cooked_url} #{ratio.to_s.sub(/\.0$/, "")}x"
|
||||
end
|
||||
|
||||
img["srcset"] = "#{UrlHelper.cook_url(img["src"], secure: upload.secure?)}#{srcset}" if srcset.present?
|
||||
end
|
||||
else
|
||||
img["src"] = upload.url
|
||||
end
|
||||
|
||||
if small_upload = loading_image(upload)
|
||||
img["data-small-upload"] = small_upload.url
|
||||
end
|
||||
end
|
||||
|
||||
# then, some overlay informations
|
||||
meta = create_node("div", "meta")
|
||||
img.add_next_sibling(meta)
|
||||
|
|
|
@ -872,6 +872,40 @@ describe CookedPostProcessor do
|
|||
|
||||
end
|
||||
|
||||
context "#convert_to_link" do
|
||||
fab!(:thumbnail) { Fabricate(:optimized_image, upload: upload, width: 512, height: 384) }
|
||||
|
||||
before do
|
||||
CookedPostProcessor.any_instance.stubs(:get_size).with(upload.url).returns([1024, 768])
|
||||
end
|
||||
|
||||
it "adds lightbox and optimizes images" do
|
||||
post = Fabricate(:post, raw: "![image|1024x768, 50%](#{upload.short_url})")
|
||||
|
||||
cpp = CookedPostProcessor.new(post, disable_loading_image: true)
|
||||
cpp.post_process
|
||||
|
||||
doc = Nokogiri::HTML::fragment(cpp.html)
|
||||
expect(doc.css('.lightbox-wrapper').size).to eq(1)
|
||||
expect(doc.css('img').first['srcset']).to_not eq(nil)
|
||||
end
|
||||
|
||||
it "optimizes images in quotes" do
|
||||
post = Fabricate(:post, raw: <<~MD)
|
||||
[quote]
|
||||
![image|1024x768, 50%](#{upload.short_url})
|
||||
[/quote]
|
||||
MD
|
||||
|
||||
cpp = CookedPostProcessor.new(post, disable_loading_image: true)
|
||||
cpp.post_process
|
||||
|
||||
doc = Nokogiri::HTML::fragment(cpp.html)
|
||||
expect(doc.css('.lightbox-wrapper').size).to eq(0)
|
||||
expect(doc.css('img').first['srcset']).to_not eq(nil)
|
||||
end
|
||||
end
|
||||
|
||||
context "#post_process_oneboxes" do
|
||||
let(:post) { build(:post_with_youtube, id: 123) }
|
||||
let(:cpp) { CookedPostProcessor.new(post, invalidate_oneboxes: true) }
|
||||
|
|
Loading…
Reference in New Issue
Block a user