Ticket #39875: 39875.3.patch
File 39875.3.patch, 5.8 KB (added by , 8 years ago) |
---|
-
src/wp-admin/includes/image.php
251 251 $editor = wp_get_image_editor( $file ); 252 252 253 253 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 JPEGs. 256 * Ensure the PDF preview image does not overwrite any JPEG images that already exist. 257 */ 258 $dirname = dirname( $file ) . '/'; 259 $ext = '.' . pathinfo( $file, PATHINFO_EXTENSION ); 260 $preview_file = $dirname . wp_unique_filename( $dirname, wp_basename( $file, $ext ) . '-pdf.jpg' ); 261 262 $uploaded = $editor->save( $preview_file, 'image/jpeg' ); 255 263 unset( $editor ); 256 264 257 265 // Resize based on the full size image, rather than the source. -
tests/phpunit/tests/image/functions.php
18 18 require_once( ABSPATH . WPINC . '/class-wp-image-editor-imagick.php' ); 19 19 20 20 include_once( DIR_TESTDATA . '/../includes/mock-image-editor.php' ); 21 22 // Ensure no legacy / failed tests detritus. 23 $folder = '/tmp/wordpress-gsoc-flyer*.{jpg,pdf}'; 24 25 foreach ( glob( $folder, GLOB_BRACE ) as $file ) { 26 unlink( $file ); 27 } 21 28 } 22 29 23 30 /** … … 373 380 $expected = array( 374 381 'sizes' => array( 375 382 'thumbnail' => array( 376 'file' => "wordpress-gsoc-flyer- 116x150.jpg",383 'file' => "wordpress-gsoc-flyer-pdf-116x150.jpg", 377 384 'width' => 116, 378 385 'height' => 150, 379 386 'mime-type' => "image/jpeg", 380 387 ), 381 388 'medium' => array( 382 'file' => "wordpress-gsoc-flyer- 232x300.jpg",389 'file' => "wordpress-gsoc-flyer-pdf-232x300.jpg", 383 390 'width' => 232, 384 391 'height' => 300, 385 392 'mime-type' => "image/jpeg", 386 393 ), 387 394 'large' => array( 388 'file' => "wordpress-gsoc-flyer- 791x1024.jpg",395 'file' => "wordpress-gsoc-flyer-pdf-791x1024.jpg", 389 396 'width' => 791, 390 397 'height' => 1024, 391 398 'mime-type' => "image/jpeg", 392 399 ), 393 400 'full' => array( 394 'file' => "wordpress-gsoc-flyer .jpg",401 'file' => "wordpress-gsoc-flyer-pdf.jpg", 395 402 'width' => 1088, 396 403 'height' => 1408, 397 404 'mime-type' => "image/jpeg", … … 403 410 $this->assertSame( $expected, $metadata ); 404 411 405 412 unlink( $test_file ); 413 foreach ( $metadata['sizes'] as $size ) { 414 unlink ( '/tmp/' . $size['file'] ); 415 } 406 416 } 407 417 408 418 /** … … 427 437 add_filter( 'fallback_intermediate_image_sizes', array( $this, 'filter_fallback_intermediate_image_sizes' ), 10, 2 ); 428 438 429 439 $expected = array( 430 'file' => 'wordpress-gsoc-flyer- 77x100.jpg',440 'file' => 'wordpress-gsoc-flyer-pdf-77x100.jpg', 431 441 'width' => 77, 432 442 'height' => 100, 433 443 'mime-type' => 'image/jpeg', … … 441 451 remove_filter( 'fallback_intermediate_image_sizes', array( $this, 'filter_fallback_intermediate_image_sizes' ), 10 ); 442 452 443 453 unlink( $test_file ); 454 foreach ( $metadata['sizes'] as $size ) { 455 unlink ( '/tmp/' . $size['file'] ); 456 } 444 457 } 445 458 446 459 function filter_fallback_intermediate_image_sizes( $fallback_sizes, $metadata ) { … … 449 462 450 463 return $fallback_sizes; 451 464 } 465 466 /** 467 * Test PDF preview doesn't overwrite existing JPEG. 468 * @ticket 39875 469 */ 470 public function test_pdf_preview_doesnt_overwrite_existing_jpeg() { 471 // Dummy JPEGs. 472 $jpg1_path = '/tmp/test.jpg'; // Straight. 473 file_put_contents( $jpg1_path, 'asdf' ); 474 $jpg2_path = '/tmp/test-pdf.jpg'; // With PDF marker. 475 file_put_contents( $jpg2_path, 'fdsa' ); 476 477 // PDF with same name as JPEG. 478 $pdf_path = '/tmp/test.pdf'; 479 copy( DIR_TESTDATA . '/images/wordpress-gsoc-flyer.pdf', $pdf_path ); 480 481 $attachment_id = $this->factory->attachment->create_object( $pdf_path, 0, array( 482 'post_mime_type' => 'application/pdf', 483 ) ); 484 485 $this->assertNotEmpty( $attachment_id ); 486 487 $metadata = wp_generate_attachment_metadata( $attachment_id, $pdf_path ); 488 489 $this->assertTrue( is_array( $metadata ) ); 490 $this->assertNotEmpty( $metadata['sizes']['full']['file'] ); 491 492 $preview_path = '/tmp/' . $metadata['sizes']['full']['file']; 493 494 $this->assertTrue( is_file( $preview_path ) ); 495 $this->assertNotEquals( $pdf_path, $preview_path ); 496 $this->assertNotEquals( $jpg1_path, $preview_path ); 497 $this->assertSame( 'asdf', file_get_contents( $jpg1_path ) ); 498 $this->assertNotEquals( $jpg2_path, $preview_path ); 499 $this->assertSame( 'fdsa', file_get_contents( $jpg2_path ) ); 500 501 // Cleanup. 502 unlink( $jpg1_path ); 503 unlink( $jpg2_path ); 504 unlink( $pdf_path ); 505 foreach ( $metadata['sizes'] as $size ) { 506 unlink ( '/tmp/' . $size['file'] ); 507 } 508 } 509 510 /** 511 * Test PDF preview low level renaming. 512 * @ticket 39875 513 */ 514 public function test_pdf_preview_renaming() { 515 // Standard case. 516 $this->assertSame( '/tmp/test-pdf.jpg', self::pdf_preview_renaming( '/tmp/test.pdf' ) ); 517 518 // Unlikely edge cases, but useful to document. 519 520 $this->assertSame( '/tmp/test-pdf.jpg', self::pdf_preview_renaming( '/tmp/test.' ) ); 521 522 $this->assertSame( '/tmp/pdf-pdf.jpg', self::pdf_preview_renaming( '/tmp/.pdf' ) ); 523 524 $this->assertSame( '/tmp/ext-pdf.jpg', self::pdf_preview_renaming( '/tmp/.ext' ) ); 525 526 $this->assertSame( '/tmp/pdf.jpg', self::pdf_preview_renaming( '/tmp/.' ) ); 527 } 528 529 protected static function pdf_preview_renaming( $file ) { 530 $dirname = dirname( $file ) . '/'; 531 $ext = '.' . pathinfo( $file, PATHINFO_EXTENSION ); 532 $preview_file = $dirname . wp_unique_filename( $dirname, wp_basename( $file, $ext ) . '-pdf.jpg' ); 533 return $preview_file; 534 } 452 535 }