Skip to content

Commit

Permalink
Always use jpg for social share images
Browse files Browse the repository at this point in the history
webp support among social appears to still be patchy.

REDMINE-20770
  • Loading branch information
tf committed Jun 21, 2024
1 parent e8d8e2c commit 23a7889
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 4 deletions.
7 changes: 5 additions & 2 deletions app/helpers/pageflow/social_share_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
13 changes: 12 additions & 1 deletion app/models/pageflow/image_file.rb
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ def attachment_styles(attachment)
format: panorama_format,
convert_options: '-quality 90 -interlace Plane'}
)
.merge(social_image_styles)
end

def url
Expand Down Expand Up @@ -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]}
Expand All @@ -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
25 changes: 25 additions & 0 deletions spec/helpers/pageflow/social_share_helper_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
24 changes: 23 additions & 1 deletion spec/models/pageflow/image_file_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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] = {
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 23a7889

Please sign in to comment.