Make WordPress Core

Ticket #39875: 39875.patch

File 39875.patch, 5.0 KB (added by gitlost, 8 years ago)

Special case pdf preview to avoid overwriting exising JPEGs.

  • src/wp-includes/class-wp-image-editor-imagick.php

     
    624624        protected function _save( $image, $filename = null, $mime_type = null ) {
    625625                list( $filename, $extension, $mime_type ) = $this->get_output_format( $filename, $mime_type );
    626626
    627                 if ( ! $filename )
     627                if ( ! $filename ) {
    628628                        $filename = $this->generate_filename( null, null, $extension );
     629                } else {
     630                        // If PDF preview.
     631                        if ( 'image/jpeg' === $mime_type && 'pdf' === strtolower( pathinfo( $this->file, PATHINFO_EXTENSION ) ) ) {
     632                                // Make sure not to overwrite existing JPEG with same name.
     633                                $dirname = dirname( $filename );
     634                                $filename = $dirname . '/' . wp_unique_filename( $dirname, wp_basename( $filename ) );
     635                        }
     636                }
    629637
    630638                try {
    631639                        // Store initial Format
  • tests/phpunit/tests/image/editor_imagick.php

     
    542542
    543543                $this->assertTrue( $result );
    544544        }
     545
     546        /**
     547         * Test PDF preview doesn't overwrite existing JPEG.
     548         * @ticket 39875
     549         */
     550        public function test_pdf_preview_doesnt_overwrite_existing_jpeg() {
     551                // Dummy JPEG.
     552                $jpg_path = '/tmp/test.jpg';
     553                file_put_contents( $jpg_path, 'asdf' );
     554
     555                // PDF with same name as JPEG.
     556                $pdf_path = '/tmp/test.pdf';
     557                copy( DIR_TESTDATA . '/images/wordpress-gsoc-flyer.pdf', $pdf_path );
     558
     559                // Load PDF.
     560                $image_editor = new WP_Image_Editor_Imagick( $pdf_path );
     561                $output = $image_editor->load();
     562                $this->assertTrue( $output );
     563
     564                // Create PDF preview.
     565                $output = $image_editor->save( $pdf_path, 'image/jpeg' );
     566
     567                $this->assertNotEmpty( $output );
     568                $this->assertTrue( is_array( $output ) );
     569                $this->assertNotEquals( $jpg_path, $output['path'] );
     570                $this->assertNotEquals( $pdf_path, $output['path'] );
     571                $this->assertSame( 'asdf', file_get_contents( $jpg_path ) );
     572                $this->assertTrue( is_file( $output['path'] ) );
     573                $this->assertSame( wp_basename( $output['path'] ), $output['file'] );
     574                $this->assertSame( 'image/jpeg', $output['mime-type'] );
     575
     576                // Cleanup.
     577                unlink( $jpg_path );
     578                unlink( $pdf_path );
     579                unlink( $output['path'] );
     580        }
    545581}
  • tests/phpunit/tests/image/functions.php

     
    1818                require_once( ABSPATH . WPINC . '/class-wp-image-editor-imagick.php' );
    1919
    2020                include_once( DIR_TESTDATA . '/../includes/mock-image-editor.php' );
     21
     22                $folder = '/tmp/wordpress-gsoc-flyer*.jpg';
     23
     24                foreach ( glob( $folder ) as $file ) {
     25                        unlink( $file );
     26                }
    2127        }
    2228
    2329        /**
     
    403409                $this->assertSame( $expected, $metadata );
    404410
    405411                unlink( $test_file );
     412                foreach ( $expected['sizes'] as $size ) {
     413                        unlink ( '/tmp/' . $size['file'] );
     414                }
    406415        }
    407416
    408417        /**
     
    427436                add_filter( 'fallback_intermediate_image_sizes', array( $this, 'filter_fallback_intermediate_image_sizes' ), 10, 2 );
    428437
    429438                $expected = array(
    430                         'file'      => 'wordpress-gsoc-flyer-77x100.jpg',
    431                         'width'     => 77,
    432                         'height'    => 100,
    433                         'mime-type' => 'image/jpeg',
    434439                );
     440                $expected = array(
     441                        'sizes' => array(
     442                                'thumbnail' => array(
     443                                        'file'      => "wordpress-gsoc-flyer-116x150.jpg",
     444                                        'width'     => 116,
     445                                        'height'    => 150,
     446                                        'mime-type' => "image/jpeg",
     447                                ),
     448                                'medium'    => array(
     449                                        'file'      => "wordpress-gsoc-flyer-232x300.jpg",
     450                                        'width'     => 232,
     451                                        'height'    => 300,
     452                                        'mime-type' => "image/jpeg",
     453                                ),
     454                                'large'     => array(
     455                                        'file'      => "wordpress-gsoc-flyer-791x1024.jpg",
     456                                        'width'     => 791,
     457                                        'height'    => 1024,
     458                                        'mime-type' => "image/jpeg",
     459                                ),
     460                                'test-size'      => array(
     461                                        'file'      => 'wordpress-gsoc-flyer-77x100.jpg',
     462                                        'width'     => 77,
     463                                        'height'    => 100,
     464                                        'mime-type' => 'image/jpeg',
     465                                ),
     466                                'full'      => array(
     467                                        'file'      => "wordpress-gsoc-flyer.jpg",
     468                                        'width'     => 1088,
     469                                        'height'    => 1408,
     470                                        'mime-type' => "image/jpeg",
     471                                ),
     472                        ),
     473                );
    435474
    436475                $metadata = wp_generate_attachment_metadata( $attachment_id, $test_file );
    437476                $this->assertTrue( isset( $metadata['sizes']['test-size'] ), 'The `test-size` was not added to the metadata.' );
    438                 $this->assertSame( $metadata['sizes']['test-size'], $expected );
     477                $this->assertSame( $expected, $metadata );
    439478
    440479                remove_image_size( 'test-size' );
    441480                remove_filter( 'fallback_intermediate_image_sizes', array( $this, 'filter_fallback_intermediate_image_sizes' ), 10 );
    442481
    443482                unlink( $test_file );
     483                foreach ( $expected['sizes'] as $size ) {
     484                        unlink ( '/tmp/' . $size['file'] );
     485                }
    444486        }
    445487
    446488        function filter_fallback_intermediate_image_sizes( $fallback_sizes, $metadata ) {