Make WordPress Core

Ticket #39875: 39875.2.diff

File 39875.2.diff, 2.7 KB (added by desrosj, 7 years ago)
  • src/wp-admin/includes/image.php

     
    251251                        $editor = wp_get_image_editor( $file );
    252252
    253253                        if ( ! is_wp_error( $editor ) ) { // No support for this type of file
    254                                 $uploaded = $editor->save( $file, 'image/jpeg' );
     254                                /*
     255                                 * PDFs may have the same file filename as JPGs.
     256                                 * Use a random string for the PDF preview image to prevent the original JPG from being overwritten.
     257                                 */
     258                                $suffix = time() . rand( 100, 999 );
     259                                $dirname = pathinfo( $file, PATHINFO_DIRNAME );
     260                                $filename = pathinfo( $file, PATHINFO_FILENAME );
     261
     262                                while ( true ) {
     263                                        $new_filename = "{$filename}-{$suffix}-pdf";
     264                                        $preview_file = "{$new_filename}.jpg";
     265                                        $new_path = "{$dirname}/$preview_file";
     266
     267                                        if ( file_exists( $new_path ) ) {
     268                                                $suffix++;
     269                                        } else {
     270                                                $filename = $new_path;
     271                                                break;
     272                                        }
     273                                }
     274
     275                                $uploaded = $editor->save( $filename, 'image/jpeg' );
    255276                                unset( $editor );
    256277
    257278                                // Resize based on the full size image, rather than the source.
  • tests/phpunit/tests/image/functions.php

     
    400400                );
    401401
    402402                $metadata = wp_generate_attachment_metadata( $attachment_id, $test_file );
    403                 $this->assertSame( $expected, $metadata );
     403                $this->assertArrayHasKey( 'sizes', $metadata, 'attachment should have size data' );
     404                $this->assertEquals( count( $expected['sizes'] ), count( $metadata['sizes'] ) );
     405
     406                $this->assertContains( '-pdf', $metadata['sizes']['medium']['file'] );
     407                $this->assertEquals( $expected['sizes']['medium']['height'], $metadata['sizes']['medium']['height'] );
     408
     409                $this->assertContains( '-pdf', $metadata['sizes']['large']['file'] );
     410                $this->assertEquals( $expected['sizes']['large']['width'], $metadata['sizes']['large']['width'] );
    404411
    405412                unlink( $test_file );
    406413        }
     
    435442
    436443                $metadata = wp_generate_attachment_metadata( $attachment_id, $test_file );
    437444                $this->assertTrue( isset( $metadata['sizes']['test-size'] ), 'The `test-size` was not added to the metadata.' );
    438                 $this->assertSame( $metadata['sizes']['test-size'], $expected );
     445                $this->assertContains( '-pdf', $metadata['sizes']['test-size']['file'] );
     446                $this->assertEquals( $expected['height'], $metadata['sizes']['test-size']['height'] );
    439447
    440448                remove_image_size( 'test-size' );
    441449                remove_filter( 'fallback_intermediate_image_sizes', array( $this, 'filter_fallback_intermediate_image_sizes' ), 10 );