Skip to content

Commit

Permalink
Merge pull request #2027 from tf/webp-no-size-crop
Browse files Browse the repository at this point in the history
Relax size option when cropping in webp processor
  • Loading branch information
tf authored Nov 15, 2023
2 parents 79d55d4 + 4f39e91 commit 846064c
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 8 deletions.
2 changes: 1 addition & 1 deletion lib/pageflow/paperclip_processors/webp.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ def make
thumbnail = Vips::Image.thumbnail(
ANIMATED_FORMATS.include?(@current_format) ? "#{source.path}[n=-1]" : source.path,
width,
size: :down,
size: @should_crop ? :both : :down,
height: height,
crop: crop
)
Expand Down
29 changes: 22 additions & 7 deletions spec/pageflow/paperclip_processors/webp_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ module PaperclipProcessors
describe Webp do
describe '#make' do
it 'shrinks jpg correctly' do
processor = Webp.new(fixture_jpg, geometry: '600x300>')
processor = Webp.new(fixture_1200x800_jpg, geometry: '600x300>')

result = processor.make
new_image = Vips::Image.new_from_file(result.path)
Expand All @@ -16,7 +16,7 @@ module PaperclipProcessors
end

it 'does not shrink if already smaller' do
processor = Webp.new(fixture_jpg, geometry: '2000x2000>')
processor = Webp.new(fixture_1200x800_jpg, geometry: '2000x2000>')

result = processor.make
new_image = Vips::Image.new_from_file(result.path)
Expand All @@ -26,8 +26,8 @@ module PaperclipProcessors
expect(new_image.height).to eq(800)
end

it 'crops them correctly' do
processor = Webp.new(fixture_jpg, geometry: '600x300#')
it 'supports cropping' do
processor = Webp.new(fixture_1200x800_jpg, geometry: '600x300#')

result = processor.make
new_image = Vips::Image.new_from_file(result.path)
Expand All @@ -37,8 +37,19 @@ module PaperclipProcessors
expect(new_image.height).to eq(300)
end

it 'can crop images smaller than crop area' do
processor = Webp.new(fixture_7x15_png, geometry: '40x40#')

result = processor.make
new_image = Vips::Image.new_from_file(result.path)

expect(result.path).to end_with('.webp')
expect(new_image.width).to eq(40)
expect(new_image.height).to eq(40)
end

it 'handles gif files' do
processor = Webp.new(fixture_gif, geometry: '600x300>')
processor = Webp.new(fixture_1x1_gif, geometry: '600x300>')

result = processor.make
new_image = Vips::Image.new_from_file(result.path)
Expand All @@ -48,11 +59,15 @@ module PaperclipProcessors
expect(new_image.height).to eq(1)
end

def fixture_jpg
def fixture_7x15_png
File.open(Engine.root.join('spec', 'fixtures', 'image.png'))
end

def fixture_1200x800_jpg
File.open(Engine.root.join('spec', 'fixtures', 'image.jpg'))
end

def fixture_gif
def fixture_1x1_gif
File.open(Engine.root.join('spec', 'fixtures', 'image.gif'))
end
end
Expand Down

0 comments on commit 846064c

Please sign in to comment.