Skip to content

Commit 2258013

Browse files
authored
Merge pull request #2118 from tf/jpg-social
Always use jpg for social share images
2 parents e8d8e2c + 23a7889 commit 2258013

File tree

4 files changed

+65
-4
lines changed

4 files changed

+65
-4
lines changed

app/helpers/pageflow/social_share_helper.rb

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,8 +58,11 @@ def social_share_entry_image_tags(entry)
5858
image_file = find_file_in_entry(ImageFile, entry.share_image_id, entry)
5959

6060
if image_file
61-
image_url = image_file.thumbnail_url(:medium)
62-
share_images.push(image_url: image_url, width: image_file.width, height: image_file.height)
61+
image_url = image_file.thumbnail_url(
62+
image_file.output_present?(:social) ? :social : :medium
63+
)
64+
65+
share_images.push(image_url:, width: image_file.width, height: image_file.height)
6366
else
6467
entry.pages.each do |page|
6568
break if share_images.size >= 4

app/models/pageflow/image_file.rb

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ def attachment_styles(attachment)
5959
format: panorama_format,
6060
convert_options: '-quality 90 -interlace Plane'}
6161
)
62+
.merge(social_image_styles)
6263
end
6364

6465
def url
@@ -88,6 +89,16 @@ def save_image_dimensions
8889
rescue Paperclip::Errors::NotIdentifiedByImageMagickError
8990
end
9091

92+
def social_image_styles
93+
if output_present?(:social)
94+
{social: {geometry: '1024x1024>',
95+
format: :jpg,
96+
convert_options: '-quality 70 -interlace Plane'}}
97+
else
98+
{}
99+
end
100+
end
101+
91102
def style_defaults
92103
if output_present?(:webp)
93104
{format: :webp, processors: [:pageflow_webp]}
@@ -97,7 +108,7 @@ def style_defaults
97108
end
98109

99110
def set_output_presences
100-
self.output_presences = {webp: true} if entry&.feature_state('webp_images')
111+
self.output_presences = {webp: true, social: true} if entry&.feature_state('webp_images')
101112
end
102113
end
103114
end

spec/helpers/pageflow/social_share_helper_spec.rb

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,31 @@ module Pageflow
158158
expect(html).to have_css("meta[content=\"#{image_file.thumbnail_url(:medium)}\"][name=\"twitter:image:src\"]", visible: false, count: 1)
159159
end
160160

161+
it 'uses jpg image if available' do
162+
entry = PublishedEntry.new(create(:entry, :published))
163+
image_file = create_used_file(
164+
:image_file,
165+
entry:,
166+
width: 1200,
167+
height: 600,
168+
output_presences: {webp: true, social: true}
169+
)
170+
entry.revision.share_image_id = image_file.perma_id
171+
172+
html = helper.social_share_entry_image_tags(entry)
173+
174+
expect(html).to have_css(
175+
"meta[content=\"#{image_file.thumbnail_url(:social)}\"][property=\"og:image\"]",
176+
visible: false,
177+
count: 1
178+
)
179+
expect(html).to have_css(
180+
"meta[content=\"#{image_file.thumbnail_url(:social)}\"][name=\"twitter:image:src\"]",
181+
visible: false,
182+
count: 1
183+
)
184+
end
185+
161186
it 'renders up to three open graph image meta tags for page thumbnails' do
162187
entry = PublishedEntry.new(create(:entry, :published))
163188
image_file1 = create_used_file(:image_file, entry: entry, width: 1200, height: 600)

spec/models/pageflow/image_file_spec.rb

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,13 @@ module Pageflow
7171
expect(styles[:panorama_medium].processor_options[:geometry]).to eq('100%')
7272
end
7373

74+
it 'does generate social style by default' do
75+
image_file = build(:image_file, :uploading, width: 2000, height: 1000)
76+
styles = image_file.attachment.styles
77+
78+
expect(styles).not_to have_key(:social)
79+
end
80+
7481
describe 'with webp outputs' do
7582
it 'turns jpg file into webp files' do
7683
Pageflow.config.thumbnail_styles[:square] = {
@@ -94,6 +101,21 @@ module Pageflow
94101
expect(styles[:medium][:processors]).to eq([:pageflow_webp])
95102
end
96103
end
104+
105+
describe 'with social output' do
106+
it 'keeps social image as jpg for compatibility reasons' do
107+
image_file = build(:image_file,
108+
:uploading,
109+
file_name: 'image.jpg',
110+
output_presences: {
111+
social: true
112+
})
113+
114+
styles = image_file.attachment_styles(image_file.attachment)
115+
116+
expect(styles[:social][:format]).to eq(:jpg)
117+
end
118+
end
97119
end
98120

99121
describe 'basename' do
@@ -128,7 +150,7 @@ module Pageflow
128150

129151
image_file.attachment.reprocess!
130152

131-
expect(image_file.reload.present_outputs).to eq([:webp])
153+
expect(image_file.reload.present_outputs).to eq([:webp, :social])
132154
end
133155

134156
it 'does not fail if entry is missing' do

0 commit comments

Comments
 (0)