From 23a78892d41c136f26c91b8279748756be597f40 Mon Sep 17 00:00:00 2001 From: Tim Fischbach Date: Fri, 21 Jun 2024 15:39:22 +0200 Subject: [PATCH] Always use jpg for social share images webp support among social appears to still be patchy. REDMINE-20770 --- app/helpers/pageflow/social_share_helper.rb | 7 ++++-- app/models/pageflow/image_file.rb | 13 +++++++++- .../pageflow/social_share_helper_spec.rb | 25 +++++++++++++++++++ spec/models/pageflow/image_file_spec.rb | 24 +++++++++++++++++- 4 files changed, 65 insertions(+), 4 deletions(-) diff --git a/app/helpers/pageflow/social_share_helper.rb b/app/helpers/pageflow/social_share_helper.rb index 928e5758ef..82934710bb 100644 --- a/app/helpers/pageflow/social_share_helper.rb +++ b/app/helpers/pageflow/social_share_helper.rb @@ -58,8 +58,11 @@ def social_share_entry_image_tags(entry) image_file = find_file_in_entry(ImageFile, entry.share_image_id, entry) if image_file - image_url = image_file.thumbnail_url(:medium) - share_images.push(image_url: image_url, width: image_file.width, height: image_file.height) + image_url = image_file.thumbnail_url( + image_file.output_present?(:social) ? :social : :medium + ) + + share_images.push(image_url:, width: image_file.width, height: image_file.height) else entry.pages.each do |page| break if share_images.size >= 4 diff --git a/app/models/pageflow/image_file.rb b/app/models/pageflow/image_file.rb index d6f396eb50..15e7f45292 100644 --- a/app/models/pageflow/image_file.rb +++ b/app/models/pageflow/image_file.rb @@ -59,6 +59,7 @@ def attachment_styles(attachment) format: panorama_format, convert_options: '-quality 90 -interlace Plane'} ) + .merge(social_image_styles) end def url @@ -88,6 +89,16 @@ def save_image_dimensions rescue Paperclip::Errors::NotIdentifiedByImageMagickError end + def social_image_styles + if output_present?(:social) + {social: {geometry: '1024x1024>', + format: :jpg, + convert_options: '-quality 70 -interlace Plane'}} + else + {} + end + end + def style_defaults if output_present?(:webp) {format: :webp, processors: [:pageflow_webp]} @@ -97,7 +108,7 @@ def style_defaults end def set_output_presences - self.output_presences = {webp: true} if entry&.feature_state('webp_images') + self.output_presences = {webp: true, social: true} if entry&.feature_state('webp_images') end end end diff --git a/spec/helpers/pageflow/social_share_helper_spec.rb b/spec/helpers/pageflow/social_share_helper_spec.rb index 0a33d095de..4502c240be 100644 --- a/spec/helpers/pageflow/social_share_helper_spec.rb +++ b/spec/helpers/pageflow/social_share_helper_spec.rb @@ -158,6 +158,31 @@ module Pageflow expect(html).to have_css("meta[content=\"#{image_file.thumbnail_url(:medium)}\"][name=\"twitter:image:src\"]", visible: false, count: 1) end + it 'uses jpg image if available' do + entry = PublishedEntry.new(create(:entry, :published)) + image_file = create_used_file( + :image_file, + entry:, + width: 1200, + height: 600, + output_presences: {webp: true, social: true} + ) + entry.revision.share_image_id = image_file.perma_id + + html = helper.social_share_entry_image_tags(entry) + + expect(html).to have_css( + "meta[content=\"#{image_file.thumbnail_url(:social)}\"][property=\"og:image\"]", + visible: false, + count: 1 + ) + expect(html).to have_css( + "meta[content=\"#{image_file.thumbnail_url(:social)}\"][name=\"twitter:image:src\"]", + visible: false, + count: 1 + ) + end + it 'renders up to three open graph image meta tags for page thumbnails' do entry = PublishedEntry.new(create(:entry, :published)) image_file1 = create_used_file(:image_file, entry: entry, width: 1200, height: 600) diff --git a/spec/models/pageflow/image_file_spec.rb b/spec/models/pageflow/image_file_spec.rb index 12fdb9cc29..7c810d4439 100644 --- a/spec/models/pageflow/image_file_spec.rb +++ b/spec/models/pageflow/image_file_spec.rb @@ -71,6 +71,13 @@ module Pageflow expect(styles[:panorama_medium].processor_options[:geometry]).to eq('100%') end + it 'does generate social style by default' do + image_file = build(:image_file, :uploading, width: 2000, height: 1000) + styles = image_file.attachment.styles + + expect(styles).not_to have_key(:social) + end + describe 'with webp outputs' do it 'turns jpg file into webp files' do Pageflow.config.thumbnail_styles[:square] = { @@ -94,6 +101,21 @@ module Pageflow expect(styles[:medium][:processors]).to eq([:pageflow_webp]) end end + + describe 'with social output' do + it 'keeps social image as jpg for compatibility reasons' do + image_file = build(:image_file, + :uploading, + file_name: 'image.jpg', + output_presences: { + social: true + }) + + styles = image_file.attachment_styles(image_file.attachment) + + expect(styles[:social][:format]).to eq(:jpg) + end + end end describe 'basename' do @@ -128,7 +150,7 @@ module Pageflow image_file.attachment.reprocess! - expect(image_file.reload.present_outputs).to eq([:webp]) + expect(image_file.reload.present_outputs).to eq([:webp, :social]) end it 'does not fail if entry is missing' do