fix: keep every frame of animated GIF uploads (v2.21.1)

optimize_image() re-saved animated GIFs without save_all=True, so Pillow
wrote only the first frame. Save all frames, and skip resized variants for
animated GIFs (they would be stills; feeds and syndication fall back to the
original).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
PhilandClaude Opus 5.5 committed 2026-11-28 18:57:08 -07:00
1 parent c87de892cf
commit 18d667db74
5 files changed
+86 -23

No files matched your search

+35 -8
View File
@@ -47,6 +47,15 @@ def create_test_image(width=800, height=600, format='PNG'):
return buffer.getvalue()
def create_animated_gif(width=244, height=134):
"""A looping 3-frame GIF with a different duration per frame"""
frames = [Image.new('RGB', (width, height), color=c) for c in ('red', 'green', 'blue')]
buffer = io.BytesIO()
frames[0].save(buffer, format='GIF', save_all=True, append_images=frames[1:],
duration=[100, 200, 300], loop=0)
return buffer.getvalue()
def create_test_heic(width=800, height=600):
"""
Generate test HEIC image using pillow-heif
@@ -339,15 +348,20 @@ class TestImageOptimization:
assert optimized_bytes is not None
def test_gif_animation_preserved(self):
"""Test GIF animation preservation (per Q12)"""
# For v1.2.0: Just verify GIF is handled without error
# Full animation preservation is complex
gif_data = create_test_image(800, 600, 'GIF')
optimized, width, height, optimized_bytes = optimize_image(gif_data)
"""Every frame, its timing and the loop survive optimization (v2.21.1)"""
gif_data = create_animated_gif()
img, _, _, _ = validate_image(gif_data, 'anim.gif')
optimized, width, height, optimized_bytes = optimize_image(img, len(gif_data))
assert width > 0
assert height > 0
assert optimized_bytes is not None
assert (width, height) == (244, 134)
out = Image.open(io.BytesIO(optimized_bytes))
assert out.n_frames == 3
assert out.info['loop'] == 0
durations = []
for i in range(out.n_frames):
out.seek(i)
durations.append(out.info['duration'])
assert durations == [100, 200, 300]
class TestMediaSave:
@@ -371,6 +385,19 @@ class TestMediaSave:
media_path = Path(app.config['DATA_PATH']) / 'media' / media_info['path']
assert media_path.exists()
def test_save_animated_gif_stays_animated(self, app):
"""The stored GIF keeps every frame, and no still variants are made (v2.21.1)"""
gif_data = create_animated_gif(1400, 800) # wide enough for every variant size
with app.app_context():
media_info = save_media(gif_data, 'anim.gif')
media_dir = Path(app.config['DATA_PATH']) / 'media'
stored = Image.open(media_dir / media_info['path'])
assert stored.n_frames == 3
assert media_info['variants'] == []
assert not list((media_dir / media_info['path']).parent.glob('*_thumb.gif'))
def test_uuid_filename(self, app):
"""Test UUID-based filename generation (per Q5)"""
image_data = create_test_image(800, 600, 'PNG')