ImageTest.php 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. <?php
  2. namespace fphammerle\helpers\tests;
  3. use fphammerle\helpers\Image;
  4. use fphammerle\helpers\colors;
  5. class ImageTest extends \PHPUnit_Framework_TestCase
  6. {
  7. public function getColorAtProvider()
  8. {
  9. return [
  10. [__DIR__ . '/data/color.png', 0, 0, new colors\RGBA(0, 1, 0, 1 )],
  11. [__DIR__ . '/data/color.png', 1, 0, new colors\RGBA(0, 0, 1, 1 )],
  12. [__DIR__ . '/data/color.png', 1, 1, new colors\RGBA(0, 0, 0, 1 )],
  13. [__DIR__ . '/data/color.png', 0, 1, new colors\RGBA(1, 0, 0, 1 )],
  14. [__DIR__ . '/data/color.png', 0, 2, new colors\RGBA(1, 0.2, 0, 1 )],
  15. [__DIR__ . '/data/color.png', 1, 2, new colors\RGBA(0, 1, 0.2, 1 )],
  16. [__DIR__ . '/data/color.png', 2, 2, new colors\RGBA(0.2, 0, 1, 1 )],
  17. [__DIR__ . '/data/color.png', 3, 2, new colors\RGBA(1, 1, 1, 1 )],
  18. [__DIR__ . '/data/color.png', 2, 0, new colors\RGBA(0.2, 0.4, 1, 1 )],
  19. [__DIR__ . '/data/color.png', 2, 1, new colors\RGBA(0.2, 0.4, 1, 102/127)],
  20. [__DIR__ . '/data/color.png', 3, 1, new colors\RGBA(1, 0.8, 0.2, 102/127)],
  21. [__DIR__ . '/data/color.png', 3, 0, new colors\RGBA(0, 0, 0, 0 )],
  22. ];
  23. }
  24. /**
  25. * @dataProvider getColorAtProvider
  26. */
  27. public function testGetColorAt($path, $x, $y, $e)
  28. {
  29. $img = Image::fromFile($path);
  30. $r = $img->getColorAt($x, $y);
  31. $this->assertTrue($e->equals($r), print_r($r->tuple, true));
  32. }
  33. public function rotateProvider()
  34. {
  35. return [
  36. [
  37. __DIR__ . '/data/color.png',
  38. 0,
  39. [
  40. [0, 0, new colors\RGBA(0, 1, 0, 1)],
  41. [1, 0, new colors\RGBA(0, 0, 1, 1)],
  42. [3, 0, new colors\RGBA(0, 0, 0, 0)],
  43. [0, 1, new colors\RGBA(1, 0, 0, 1)],
  44. [1, 1, new colors\RGBA(0, 0, 0, 1)],
  45. [3, 2, new colors\RGBA(1, 1, 1, 1)],
  46. ],
  47. ],
  48. [
  49. __DIR__ . '/data/color.png',
  50. 90,
  51. [
  52. [0, 3, new colors\RGBA(0, 1, 0, 1)],
  53. [0, 2, new colors\RGBA(0, 0, 1, 1)],
  54. [0, 0, new colors\RGBA(0, 0, 0, 0)],
  55. [1, 3, new colors\RGBA(1, 0, 0, 1)],
  56. [1, 2, new colors\RGBA(0, 0, 0, 1)],
  57. [2, 0, new colors\RGBA(1, 1, 1, 1)],
  58. ],
  59. ],
  60. [
  61. __DIR__ . '/data/color.png',
  62. 180,
  63. [
  64. [3, 2, new colors\RGBA(0, 1, 0, 1)],
  65. [2, 2, new colors\RGBA(0, 0, 1, 1)],
  66. [0, 2, new colors\RGBA(0, 0, 0, 0)],
  67. [3, 1, new colors\RGBA(1, 0, 0, 1)],
  68. [2, 1, new colors\RGBA(0, 0, 0, 1)],
  69. [0, 0, new colors\RGBA(1, 1, 1, 1)],
  70. ],
  71. ],
  72. [
  73. __DIR__ . '/data/color.png',
  74. 270,
  75. [
  76. [2, 0, new colors\RGBA(0, 1, 0, 1)],
  77. [2, 1, new colors\RGBA(0, 0, 1, 1)],
  78. [2, 3, new colors\RGBA(0, 0, 0, 0)],
  79. [1, 0, new colors\RGBA(1, 0, 0, 1)],
  80. [1, 1, new colors\RGBA(0, 0, 0, 1)],
  81. [0, 3, new colors\RGBA(1, 1, 1, 1)],
  82. ],
  83. ],
  84. [
  85. __DIR__ . '/data/color.png',
  86. 360,
  87. [
  88. [0, 0, new colors\RGBA(0, 1, 0, 1)],
  89. [1, 0, new colors\RGBA(0, 0, 1, 1)],
  90. [3, 0, new colors\RGBA(0, 0, 0, 0)],
  91. [0, 1, new colors\RGBA(1, 0, 0, 1)],
  92. [1, 1, new colors\RGBA(0, 0, 0, 1)],
  93. [3, 2, new colors\RGBA(1, 1, 1, 1)],
  94. ],
  95. ],
  96. [
  97. __DIR__ . '/data/color.png',
  98. 360 + 90,
  99. [
  100. [0, 3, new colors\RGBA(0, 1, 0, 1)],
  101. [0, 2, new colors\RGBA(0, 0, 1, 1)],
  102. [0, 0, new colors\RGBA(0, 0, 0, 0)],
  103. [1, 3, new colors\RGBA(1, 0, 0, 1)],
  104. [1, 2, new colors\RGBA(0, 0, 0, 1)],
  105. [2, 0, new colors\RGBA(1, 1, 1, 1)],
  106. ],
  107. ],
  108. ];
  109. }
  110. /**
  111. * @dataProvider rotateProvider
  112. */
  113. public function testRotate($source_path, $angle, $expected_pixels)
  114. {
  115. $img = Image::fromFile($source_path);
  116. $img->rotate($angle);
  117. foreach($expected_pixels as $px) {
  118. $this->assertTrue($px[2]->equals($img->getColorAt($px[0], $px[1])));
  119. }
  120. }
  121. public function rotateLeftProvider()
  122. {
  123. return [
  124. [__DIR__ . '/data/chainring.jpg', __DIR__ . '/data/chainring-rotated-left.jpg'],
  125. ];
  126. }
  127. /**
  128. * @dataProvider rotateLeftProvider
  129. */
  130. public function testRotateLeft($source_path, $expected_path)
  131. {
  132. $img = Image::fromFile($source_path);
  133. $tmp_path = tempnam(sys_get_temp_dir(), 'image');
  134. $img->rotateLeft();
  135. $img->saveJpeg($tmp_path);
  136. $this->assertFileEquals($expected_path, $tmp_path);
  137. unlink($tmp_path);
  138. }
  139. public function rotateRightProvider()
  140. {
  141. return [
  142. [__DIR__ . '/data/chainring.jpg', __DIR__ . '/data/chainring-rotated-right.jpg'],
  143. ];
  144. }
  145. /**
  146. * @dataProvider rotateRightProvider
  147. */
  148. public function testRotateRight($source_path, $expected_path)
  149. {
  150. $img = Image::fromFile($source_path);
  151. $tmp_path = tempnam(sys_get_temp_dir(), 'image');
  152. $img->rotateRight();
  153. $img->saveJpeg($tmp_path);
  154. $this->assertFileEquals($expected_path, $tmp_path);
  155. unlink($tmp_path);
  156. }
  157. public function testSaveJpeg()
  158. {
  159. $img = Image::fromFile(__DIR__ . '/data/chainring.jpg');
  160. $tmp_path = tempnam(sys_get_temp_dir(), 'image');
  161. $img->saveJpeg($tmp_path);
  162. $this->assertFileEquals(__DIR__ . '/data/chainring-saved.jpg', $tmp_path);
  163. unlink($tmp_path);
  164. }
  165. }