From 3a46cba4305020d1e96fada784517aa83452e3f9 Mon Sep 17 00:00:00 2001 From: Mike Schroder Date: Wed, 7 Mar 2018 01:18:08 +0000 Subject: [PATCH] Media: Correctly allow changing PDF thumbnail crop value. Corrects logic that keeping plugins from setting crop value of intermediate image sizes for rendered PDFs. Adds test. Props leemon, SergeyBiryukov, chetan200891, birgire. Fixes #43226. git-svn-id: https://develop.svn.wordpress.org/trunk@42792 602fd350-edb4-49c9-b593-d223f7449a82 --- src/wp-admin/includes/image.php | 2 +- tests/phpunit/tests/image/functions.php | 63 +++++++++++++++++++++++++ 2 files changed, 64 insertions(+), 1 deletion(-) diff --git a/src/wp-admin/includes/image.php b/src/wp-admin/includes/image.php index 573f107605..d6e6fbd563 100644 --- a/src/wp-admin/includes/image.php +++ b/src/wp-admin/includes/image.php @@ -253,7 +253,7 @@ function wp_generate_attachment_metadata( $attachment_id, $file ) { $sizes[ $s ]['crop'] = $_wp_additional_image_sizes[ $s ]['crop']; } else { // Force thumbnails to be soft crops. - if ( ! 'thumbnail' === $s ) { + 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 915b695b42..913fefd93d 100644 --- a/tests/phpunit/tests/image/functions.php +++ b/tests/phpunit/tests/image/functions.php @@ -443,6 +443,69 @@ class Tests_Image_Functions extends WP_UnitTestCase { } } + /** + * Crop setting for PDF. + * + * @ticket 43226 + */ + public function test_crop_setting_for_pdf() { + + if ( ! wp_image_editor_supports( array( 'mime_type' => 'application/pdf' ) ) ) { + $this->markTestSkipped( 'Rendering PDFs is not supported on this system.' ); + } + + update_option( 'medium_crop', 1 ); + + $orig_file = DIR_TESTDATA . '/images/wordpress-gsoc-flyer.pdf'; + $test_file = get_temp_dir() . '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 ); + + $expected = array( + 'sizes' => array( + 'thumbnail' => array( + 'file' => 'wordpress-gsoc-flyer-pdf-116x150.jpg', + 'width' => 116, + 'height' => 150, + 'mime-type' => 'image/jpeg', + ), + 'medium' => array( + 'file' => 'wordpress-gsoc-flyer-pdf-300x300.jpg', + 'width' => 300, + 'height' => 300, + 'mime-type' => 'image/jpeg', + ), + 'large' => array( + 'file' => 'wordpress-gsoc-flyer-pdf-791x1024.jpg', + 'width' => 791, + 'height' => 1024, + 'mime-type' => 'image/jpeg', + ), + 'full' => array( + 'file' => 'wordpress-gsoc-flyer-pdf.jpg', + 'width' => 1088, + 'height' => 1408, + 'mime-type' => 'image/jpeg', + ), + ), + ); + + $metadata = wp_generate_attachment_metadata( $attachment_id, $test_file ); + $this->assertSame( $expected, $metadata ); + + unlink( $test_file ); + foreach ( $metadata['sizes'] as $size ) { + unlink( get_temp_dir() . $size['file'] ); + } + } + /** * @ticket 39231 */