From 82af7aba8d1878316ebb0dd313de82490f21f855 Mon Sep 17 00:00:00 2001 From: Gary Pendergast Date: Wed, 21 Dec 2016 05:52:54 +0000 Subject: [PATCH] Media: Allow PDF fallbacks filter to process custom sizes. This fixes an oversight in [39246], which added a hook for filtering the array of sizes used for PDF thumbnails, but failed to provide a way for sizes added through `add_image_size()` to be processed. Merge of [39617] to the 4.7 branch. Props gitlost. Fixes #39231. See #38594. git-svn-id: https://develop.svn.wordpress.org/branches/4.7@39633 602fd350-edb4-49c9-b593-d223f7449a82 --- src/wp-admin/includes/image.php | 24 ++++++++++--- tests/phpunit/tests/image/functions.php | 45 +++++++++++++++++++++++++ 2 files changed, 64 insertions(+), 5 deletions(-) diff --git a/src/wp-admin/includes/image.php b/src/wp-admin/includes/image.php index 5f7b583d5d..4ae53b9c3a 100644 --- a/src/wp-admin/includes/image.php +++ b/src/wp-admin/includes/image.php @@ -221,14 +221,28 @@ function wp_generate_attachment_metadata( $attachment_id, $file ) { $fallback_sizes = apply_filters( 'fallback_intermediate_image_sizes', $fallback_sizes, $metadata ); $sizes = array(); + $_wp_additional_image_sizes = wp_get_additional_image_sizes(); foreach ( $fallback_sizes as $s ) { - $sizes[ $s ]['width'] = get_option( "{$s}_size_w" ); - $sizes[ $s ]['height'] = get_option( "{$s}_size_h" ); + if ( isset( $_wp_additional_image_sizes[ $s ]['width'] ) ) { + $sizes[ $s ]['width'] = intval( $_wp_additional_image_sizes[ $s ]['width'] ); + } else { + $sizes[ $s ]['width'] = get_option( "{$s}_size_w" ); + } - // Force thumbnails to be soft crops. - if ( ! 'thumbnail' === $s ) { - $sizes[ $s ]['crop'] = get_option( "{$s}_crop" ); + if ( isset( $_wp_additional_image_sizes[ $s ]['height'] ) ) { + $sizes[ $s ]['height'] = intval( $_wp_additional_image_sizes[ $s ]['height'] ); + } else { + $sizes[ $s ]['height'] = get_option( "{$s}_size_h" ); + } + + if ( isset( $_wp_additional_image_sizes[ $s ]['crop'] ) ) { + $sizes[ $s ]['crop'] = $_wp_additional_image_sizes[ $s ]['crop']; + } else { + // Force thumbnails to be soft crops. + if ( ! 'thumbnail' === $s ) { + $sizes[ $s ]['crop'] = get_option( "{$s}_crop" ); + } } } diff --git a/tests/phpunit/tests/image/functions.php b/tests/phpunit/tests/image/functions.php index f058560cdf..f17cbb923f 100644 --- a/tests/phpunit/tests/image/functions.php +++ b/tests/phpunit/tests/image/functions.php @@ -404,4 +404,49 @@ class Tests_Image_Functions extends WP_UnitTestCase { unlink( $test_file ); } + + /** + * @ticket 39231 + */ + public function test_fallback_intermediate_image_sizes() { + if ( ! wp_image_editor_supports( array( 'mime_type' => 'application/pdf' ) ) ) { + $this->markTestSkipped( 'Rendering PDFs is not supported on this system.' ); + } + + $orig_file = DIR_TESTDATA . '/images/wordpress-gsoc-flyer.pdf'; + $test_file = '/tmp/wordpress-gsoc-flyer.pdf'; + copy( $orig_file, $test_file ); + + $attachment_id = $this->factory->attachment->create_object( $test_file, 0, array( + 'post_mime_type' => 'application/pdf', + ) ); + + $this->assertNotEmpty( $attachment_id ); + + add_image_size( 'test-size', 100, 100 ); + add_filter( 'fallback_intermediate_image_sizes', array( $this, 'filter_fallback_intermediate_image_sizes' ), 10, 2 ); + + $expected = array( + 'file' => 'wordpress-gsoc-flyer-77x100.jpg', + 'width' => 77, + 'height' => 100, + 'mime-type' => 'image/jpeg', + ); + + $metadata = wp_generate_attachment_metadata( $attachment_id, $test_file ); + $this->assertTrue( isset( $metadata['sizes']['test-size'] ), 'The `test-size` was not added to the metadata.' ); + $this->assertSame( $metadata['sizes']['test-size'], $expected ); + + remove_image_size( 'test-size' ); + remove_filter( 'fallback_intermediate_image_sizes', array( $this, 'filter_fallback_intermediate_image_sizes' ), 10 ); + + unlink( $test_file ); + } + + function filter_fallback_intermediate_image_sizes( $fallback_sizes, $metadata ) { + // Add the 'test-size' to the list of fallback sizes. + $fallback_sizes[] = 'test-size'; + + return $fallback_sizes; + } }