From 611ddaf9bfc82b88ea0ddf31d1eb0c9676c4f90e Mon Sep 17 00:00:00 2001 From: Andrew Ozz Date: Wed, 4 Nov 2015 21:43:44 +0000 Subject: [PATCH] Responsive images: do not generate `srcset` for GIFs that are inserted at full size. Prevents breaking animated GIFs. Props joemcgill. Fixes #34528. git-svn-id: https://develop.svn.wordpress.org/trunk@35524 602fd350-edb4-49c9-b593-d223f7449a82 --- src/wp-includes/media.php | 7 +++++++ tests/phpunit/tests/media.php | 39 +++++++++++++++++++++++++++++++++++ 2 files changed, 46 insertions(+) diff --git a/src/wp-includes/media.php b/src/wp-includes/media.php index ae36fcb8ef..562f2834dc 100644 --- a/src/wp-includes/media.php +++ b/src/wp-includes/media.php @@ -983,6 +983,13 @@ function wp_calculate_image_srcset( $image_src, $size_array, $image_meta, $attac return false; } + // Don't add srcset attributes to (animated) gifs that are inserted at full size. + if ( isset( $image_sizes['thumbnail']['mime-type'] ) && 'image/gif' === $image_sizes['thumbnail']['mime-type'] && + false !== strpos( $image_src, $image_meta['file'] ) ) { + + return false; + } + $image_basename = wp_basename( $image_meta['file'] ); $image_baseurl = _wp_upload_dir_baseurl(); diff --git a/tests/phpunit/tests/media.php b/tests/phpunit/tests/media.php index 627d251413..5734f3e685 100644 --- a/tests/phpunit/tests/media.php +++ b/tests/phpunit/tests/media.php @@ -963,4 +963,43 @@ EOF; // The content filter should return the image unchanged. $this->assertSame( $image_html, wp_make_content_images_responsive( $image_html ) ); } + + /** + * @ticket 33641 + * @ticket 34528 + */ + function test_wp_calculate_image_srcset_animated_gifs() { + // Mock meta for an animated gif. + $image_meta = array( + 'width' => 1200, + 'height' => 600, + 'file' => 'animated.gif', + 'sizes' => array( + 'thumbnail' => array( + 'file' => 'animated-150x150.gif', + 'width' => 150, + 'height' => 150, + 'mime-type' => 'image/gif' + ), + 'medium' => array( + 'file' => 'animated-300x150.gif', + 'width' => 300, + 'height' => 150, + 'mime-type' => 'image/gif' + ), + 'large' => array( + 'file' => 'animated-1024x512.gif', + 'width' => 1024, + 'height' => 512, + 'mime-type' => 'image/gif' + ), + ) + ); + + $image_src = 'http://' . WP_TESTS_DOMAIN . '/wp-content/uploads/' . $image_meta['file']; + // Test with soft resized size array. + $size_array = array(900, 450); + + $this->assertFalse( wp_calculate_image_srcset( $image_src, $size_array, $image_meta ) ); + } }