editor_imagick.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. <?php
  2. /**
  3. * Test the WP_Image_Editor_Imagick class
  4. * @group image
  5. * @group media
  6. * @group wp-image-editor-imagick
  7. */
  8. class Tests_Image_Editor_Imagick extends WP_Image_UnitTestCase {
  9. public $editor_engine = 'WP_Image_Editor_Imagick';
  10. public function setup() {
  11. require_once( ABSPATH . WPINC . '/class-wp-image-editor.php' );
  12. require_once( ABSPATH . WPINC . '/class-wp-image-editor-imagick.php' );
  13. $editor = new WP_Image_Editor_Imagick( null );
  14. if ( ! $editor->test() )
  15. $this->markTestSkipped( 'Image Magick not available' );
  16. parent::setUp();
  17. }
  18. /**
  19. * Check support for Image Magick compatible mime types.
  20. *
  21. */
  22. public function test_supports_mime_type() {
  23. $imagick_image_editor = new WP_Image_Editor_Imagick( null );
  24. $this->assertTrue( $imagick_image_editor->supports_mime_type( 'image/jpeg' ), 'Does not support image/jpeg' );
  25. $this->assertTrue( $imagick_image_editor->supports_mime_type( 'image/png' ), 'Does not support image/png' );
  26. $this->assertTrue( $imagick_image_editor->supports_mime_type( 'image/gif' ), 'Does not support image/gif' );
  27. }
  28. /**
  29. * Test resizing an image, not using crop
  30. *
  31. */
  32. public function test_resize() {
  33. $file = DIR_TESTDATA . '/images/gradient-square.jpg';
  34. $imagick_image_editor = new WP_Image_Editor_Imagick( $file );
  35. $imagick_image_editor->load();
  36. $imagick_image_editor->resize( 100, 50 );
  37. $this->assertEquals( array( 'width' => 50, 'height' => 50 ), $imagick_image_editor->get_size() );
  38. }
  39. /**
  40. * Test resizing an image including cropping
  41. *
  42. */
  43. public function test_resize_and_crop() {
  44. $file = DIR_TESTDATA . '/images/gradient-square.jpg';
  45. $imagick_image_editor = new WP_Image_Editor_Imagick( $file );
  46. $imagick_image_editor->load();
  47. $imagick_image_editor->resize( 100, 50, true );
  48. $this->assertEquals( array( 'width' => 100, 'height' => 50 ), $imagick_image_editor->get_size() );
  49. }
  50. /**
  51. * Test cropping an image
  52. */
  53. public function test_crop() {
  54. $file = DIR_TESTDATA . '/images/gradient-square.jpg';
  55. $imagick_image_editor = new WP_Image_Editor_Imagick( $file );
  56. $imagick_image_editor->load();
  57. $imagick_image_editor->crop( 0, 0, 50, 50 );
  58. $this->assertEquals( array( 'width' => 50, 'height' => 50 ), $imagick_image_editor->get_size() );
  59. }
  60. /**
  61. * Test rotating an image 180 deg
  62. */
  63. public function test_rotate() {
  64. $file = DIR_TESTDATA . '/images/gradient-square.jpg';
  65. $imagick_image_editor = new WP_Image_Editor_Imagick( $file );
  66. $imagick_image_editor->load();
  67. $property = new ReflectionProperty( $imagick_image_editor, 'image' );
  68. $property->setAccessible( true );
  69. $color_top_left = $property->getValue( $imagick_image_editor )->getImagePixelColor( 1, 1 )->getColor();
  70. $imagick_image_editor->rotate( 180 );
  71. $this->assertEquals( $color_top_left, $property->getValue( $imagick_image_editor )->getImagePixelColor( 99, 99 )->getColor() );
  72. }
  73. /**
  74. * Test flipping an image
  75. */
  76. public function test_flip() {
  77. $file = DIR_TESTDATA . '/images/gradient-square.jpg';
  78. $imagick_image_editor = new WP_Image_Editor_Imagick( $file );
  79. $imagick_image_editor->load();
  80. $property = new ReflectionProperty( $imagick_image_editor, 'image' );
  81. $property->setAccessible( true );
  82. $color_top_left = $property->getValue( $imagick_image_editor )->getImagePixelColor( 1, 1 )->getColor();
  83. $imagick_image_editor->flip( true, false );
  84. $this->assertEquals( $color_top_left, $property->getValue( $imagick_image_editor )->getImagePixelColor( 0, 99 )->getColor() );
  85. }
  86. /**
  87. * Test the image created with WP_Image_Edior_Imagick preserves alpha when resizing
  88. *
  89. * @ticket 24871
  90. */
  91. public function test_image_preserves_alpha_on_resize() {
  92. $file = DIR_TESTDATA . '/images/transparent.png';
  93. $editor = wp_get_image_editor( $file );
  94. $editor->load();
  95. $editor->resize(5,5);
  96. $save_to_file = tempnam( get_temp_dir(), '' ) . '.png';
  97. $editor->save( $save_to_file );
  98. $this->assertImageAlphaAtPoint( $save_to_file, array( 0,0 ), 127 );
  99. }
  100. /**
  101. * Test the image created with WP_Image_Edior_Imagick preserves alpha with no resizing etc
  102. *
  103. * @ticket 24871
  104. */
  105. public function test_image_preserves_alpha() {
  106. $file = DIR_TESTDATA . '/images/transparent.png';
  107. $editor = wp_get_image_editor( $file );
  108. $editor->load();
  109. $save_to_file = tempnam( get_temp_dir(), '' ) . '.png';
  110. $editor->save( $save_to_file );
  111. $this->assertImageAlphaAtPoint( $save_to_file, array( 0,0 ), 127 );
  112. }
  113. }