A platform for community discussion. Free, open, simple.
FEATURE: add support for responsive images in posts
Samad0e7687428e
diff --git a/config/site_settings.yml b/config/site_settings.ymlindex 354bde003dc..1187c06beb6 100644--- a/config/site_settings.yml+++ b/config/site_settings.yml@@ -914,6 +914,9 @@ files:default: 'gz'type: listlist_type: compact+ responsive_post_image_sizes:+ default: "1|1.5|2"+ hidden: truecrawl_images:default: truemax_image_width:diff --git a/lib/cooked_post_processor.rb b/lib/cooked_post_processor.rbindex c64edce2854..8653f422382 100644--- a/lib/cooked_post_processor.rb+++ b/lib/cooked_post_processor.rb@@ -231,6 +231,10 @@ class CookedPostProcessorendend+ def add_to_size_cache(url, w, h)+ @size_cache[url] = [w, h]+ end+def get_size(url)return @size_cache[url] if @size_cache.has_key?(url)@@ -285,6 +289,15 @@ class CookedPostProcessorif upload = Upload.get_from_url(src)upload.create_thumbnail!(width, height, crop)++ each_responsive_ratio do |ratio|+ resized_w = (width * ratio).to_i+ resized_h = (height * ratio).to_i++ if upload.width && resized_w <= upload.width+ upload.create_thumbnail!(resized_w, resized_h, crop)+ end+ endendadd_lightbox!(img, original_width, original_height, upload)@@ -299,6 +312,15 @@ class CookedPostProcessorfalseend+ def each_responsive_ratio+ SiteSetting+ .responsive_post_image_sizes+ .split('|')+ .map(&:to_f)+ .sort+ .each { |r| yield r if r > 1 }+ end+def add_lightbox!(img, original_width, original_height, upload = nil)# first, create a div to hold our lightboxlightbox = create_node("div", "lightbox-wrapper")@@ -320,13 +342,31 @@ class CookedPostProcessorif uploadthumbnail = upload.thumbnail(w, h)-- img["src"] =- if thumbnail && thumbnail.filesize.to_i < upload.filesize- upload.thumbnail(w, h).url- else- upload.url+ if thumbnail && thumbnail.filesize.to_i < upload.filesize+ img["src"] = upload.thumbnail(w, h).url++ srcset = +""++ each_responsive_ratio do |ratio|+ resized_w = (w * ratio).to_i+ resized_h = (h * ratio).to_i++ if upload.width && resized_w > upload.width+ cooked_url = UrlHelper.cook_url(upload.url)+ srcset << ", #{cooked_url} #{ratio}x"+ else+ if t = upload.thumbnail(resized_w, resized_h)+ cooked_url = UrlHelper.cook_url(t.url)+ srcset << ", #{cooked_url} #{ratio}x"+ end+ endend++ img["srcset"] = "#{img["src"]}#{srcset}" if srcset.length > 0++ else+ img["src"] = upload.url+ endend# then, some overlay informationsdiff --git a/spec/components/cooked_post_processor_spec.rb b/spec/components/cooked_post_processor_spec.rbindex 8bf669506f3..748c258c0f5 100644--- a/spec/components/cooked_post_processor_spec.rb+++ b/spec/components/cooked_post_processor_spec.rb@@ -53,6 +53,51 @@ describe CookedPostProcessor docontext ".post_process_images" do+ before do+ SiteSetting.responsive_post_image_sizes = ""+ end++ context "responsive images" do+ it "includes responsive images on demand" do++ SiteSetting.responsive_post_image_sizes = "1|1.5|3"++ upload = Fabricate(:upload, width: 2000, height: 1500, filesize: 10000)+ post = Fabricate(:post, raw: "hello <img src='#{upload.url}'>")++ # fake some optimized images+ OptimizedImage.create!(+ url: 'http://a.b.c/666x500.jpg',+ width: 666,+ height: 500,+ upload_id: upload.id,+ sha1: SecureRandom.hex,+ extension: '.jpg',+ filesize: 500+ )++ # fake 3x optimized image, we lose 2 pixels here over original due to rounding on downsize+ OptimizedImage.create!(+ url: 'http://a.b.c/1998x1500.jpg',+ width: 1998,+ height: 1500,+ upload_id: upload.id,+ sha1: SecureRandom.hex,+ extension: '.jpg',+ filesize: 800+ )++ cpp = CookedPostProcessor.new(post)++ cpp.add_to_size_cache(upload.url, 2000, 1500)+ cpp.post_process_images++ # 1.5x is skipped cause we have a missing thumb+ expect(cpp.html).to include('srcset="http://a.b.c/666x500.jpg, http://a.b.c/1998x1500.jpg 3.0x"')++ end+ end+shared_examples "leave dimensions alone" doit "doesn't use them" doexpect(cpp.html).to match(/src="http:\/\/foo.bar\/image.png" width="" height=""/)