ImageTest.php 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  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 getWidthProvider()
  8. {
  9. return [
  10. [__DIR__ . '/data/color.png', 4],
  11. [__DIR__ . '/data/chainring.jpg', 1336],
  12. ];
  13. }
  14. /**
  15. * @dataProvider getWidthProvider
  16. */
  17. public function testGetWidth($path, $width)
  18. {
  19. $img = Image::fromFile($path);
  20. $this->assertSame($width, $img->getWidth());
  21. $this->assertSame($width, $img->width);
  22. }
  23. public function getColorAtProvider()
  24. {
  25. return [
  26. [__DIR__ . '/data/color.png', 0, 0, new colors\RGBA(0, 1, 0, 1 )],
  27. [__DIR__ . '/data/color.png', 1, 0, new colors\RGBA(0, 0, 1, 1 )],
  28. [__DIR__ . '/data/color.png', 1, 1, new colors\RGBA(0, 0, 0, 1 )],
  29. [__DIR__ . '/data/color.png', 0, 1, new colors\RGBA(1, 0, 0, 1 )],
  30. [__DIR__ . '/data/color.png', 0, 2, new colors\RGBA(1, 0.2, 0, 1 )],
  31. [__DIR__ . '/data/color.png', 1, 2, new colors\RGBA(0, 1, 0.2, 1 )],
  32. [__DIR__ . '/data/color.png', 2, 2, new colors\RGBA(0.2, 0, 1, 1 )],
  33. [__DIR__ . '/data/color.png', 3, 2, new colors\RGBA(1, 1, 1, 1 )],
  34. [__DIR__ . '/data/color.png', 2, 0, new colors\RGBA(0.2, 0.4, 1, 1 )],
  35. [__DIR__ . '/data/color.png', 2, 1, new colors\RGBA(0.2, 0.4, 1, 102/127)],
  36. [__DIR__ . '/data/color.png', 3, 1, new colors\RGBA(1, 0.8, 0.2, 102/127)],
  37. [__DIR__ . '/data/color.png', 3, 0, new colors\RGBA(0, 0, 0, 0 )],
  38. ];
  39. }
  40. /**
  41. * @dataProvider getColorAtProvider
  42. */
  43. public function testGetColorAt($path, $x, $y, $e)
  44. {
  45. $img = Image::fromFile($path);
  46. $r = $img->getColorAt($x, $y);
  47. $this->assertTrue($e->equals($r), print_r($r->tuple, true));
  48. }
  49. public function rotateProvider()
  50. {
  51. return [
  52. [
  53. __DIR__ . '/data/color.png',
  54. 0,
  55. [
  56. [0, 0, new colors\RGBA(0, 1, 0, 1)],
  57. [1, 0, new colors\RGBA(0, 0, 1, 1)],
  58. [3, 0, new colors\RGBA(0, 0, 0, 0)],
  59. [0, 1, new colors\RGBA(1, 0, 0, 1)],
  60. [1, 1, new colors\RGBA(0, 0, 0, 1)],
  61. [3, 2, new colors\RGBA(1, 1, 1, 1)],
  62. ],
  63. ],
  64. [
  65. __DIR__ . '/data/color.png',
  66. 90,
  67. [
  68. [0, 3, new colors\RGBA(0, 1, 0, 1)],
  69. [0, 2, new colors\RGBA(0, 0, 1, 1)],
  70. [0, 0, new colors\RGBA(0, 0, 0, 0)],
  71. [1, 3, new colors\RGBA(1, 0, 0, 1)],
  72. [1, 2, new colors\RGBA(0, 0, 0, 1)],
  73. [2, 0, new colors\RGBA(1, 1, 1, 1)],
  74. ],
  75. ],
  76. [
  77. __DIR__ . '/data/color.png',
  78. 180,
  79. [
  80. [3, 2, new colors\RGBA(0, 1, 0, 1)],
  81. [2, 2, new colors\RGBA(0, 0, 1, 1)],
  82. [0, 2, new colors\RGBA(0, 0, 0, 0)],
  83. [3, 1, new colors\RGBA(1, 0, 0, 1)],
  84. [2, 1, new colors\RGBA(0, 0, 0, 1)],
  85. [0, 0, new colors\RGBA(1, 1, 1, 1)],
  86. ],
  87. ],
  88. [
  89. __DIR__ . '/data/color.png',
  90. 270,
  91. [
  92. [2, 0, new colors\RGBA(0, 1, 0, 1)],
  93. [2, 1, new colors\RGBA(0, 0, 1, 1)],
  94. [2, 3, new colors\RGBA(0, 0, 0, 0)],
  95. [1, 0, new colors\RGBA(1, 0, 0, 1)],
  96. [1, 1, new colors\RGBA(0, 0, 0, 1)],
  97. [0, 3, new colors\RGBA(1, 1, 1, 1)],
  98. ],
  99. ],
  100. [
  101. __DIR__ . '/data/color.png',
  102. 360,
  103. [
  104. [0, 0, new colors\RGBA(0, 1, 0, 1)],
  105. [1, 0, new colors\RGBA(0, 0, 1, 1)],
  106. [3, 0, new colors\RGBA(0, 0, 0, 0)],
  107. [0, 1, new colors\RGBA(1, 0, 0, 1)],
  108. [1, 1, new colors\RGBA(0, 0, 0, 1)],
  109. [3, 2, new colors\RGBA(1, 1, 1, 1)],
  110. ],
  111. ],
  112. [
  113. __DIR__ . '/data/color.png',
  114. 360 + 90,
  115. [
  116. [0, 3, new colors\RGBA(0, 1, 0, 1)],
  117. [0, 2, new colors\RGBA(0, 0, 1, 1)],
  118. [0, 0, new colors\RGBA(0, 0, 0, 0)],
  119. [1, 3, new colors\RGBA(1, 0, 0, 1)],
  120. [1, 2, new colors\RGBA(0, 0, 0, 1)],
  121. [2, 0, new colors\RGBA(1, 1, 1, 1)],
  122. ],
  123. ],
  124. ];
  125. }
  126. /**
  127. * @dataProvider rotateProvider
  128. */
  129. public function testRotate($source_path, $angle, $expected_pixels)
  130. {
  131. $img = Image::fromFile($source_path);
  132. $img->rotate($angle);
  133. foreach($expected_pixels as $px) {
  134. $this->assertTrue($px[2]->equals($img->getColorAt($px[0], $px[1])));
  135. }
  136. }
  137. public function rotateLeftProvider()
  138. {
  139. return [
  140. [
  141. __DIR__ . '/data/color.png',
  142. [
  143. [0, 3, new colors\RGBA(0, 1, 0, 1)],
  144. [0, 2, new colors\RGBA(0, 0, 1, 1)],
  145. [0, 0, new colors\RGBA(0, 0, 0, 0)],
  146. [1, 3, new colors\RGBA(1, 0, 0, 1)],
  147. [1, 2, new colors\RGBA(0, 0, 0, 1)],
  148. [2, 0, new colors\RGBA(1, 1, 1, 1)],
  149. ],
  150. ],
  151. ];
  152. }
  153. /**
  154. * @dataProvider rotateLeftProvider
  155. */
  156. public function testRotateLeft($source_path, $expected_pixels)
  157. {
  158. $img = Image::fromFile($source_path);
  159. $img->rotateLeft();
  160. foreach($expected_pixels as $px) {
  161. $this->assertTrue($px[2]->equals($img->getColorAt($px[0], $px[1])));
  162. }
  163. }
  164. public function rotateRightProvider()
  165. {
  166. return [
  167. [
  168. __DIR__ . '/data/color.png',
  169. [
  170. [2, 0, new colors\RGBA(0, 1, 0, 1)],
  171. [2, 1, new colors\RGBA(0, 0, 1, 1)],
  172. [2, 3, new colors\RGBA(0, 0, 0, 0)],
  173. [1, 0, new colors\RGBA(1, 0, 0, 1)],
  174. [1, 1, new colors\RGBA(0, 0, 0, 1)],
  175. [0, 3, new colors\RGBA(1, 1, 1, 1)],
  176. ],
  177. ],
  178. ];
  179. }
  180. /**
  181. * @dataProvider rotateRightProvider
  182. */
  183. public function testRotateRight($source_path, $expected_pixels)
  184. {
  185. $img = Image::fromFile($source_path);
  186. $img->rotateRight();
  187. foreach($expected_pixels as $px) {
  188. $this->assertTrue($px[2]->equals($img->getColorAt($px[0], $px[1])));
  189. }
  190. }
  191. public function saveJpegProvider()
  192. {
  193. return [
  194. [__DIR__ . '/data/chainring.jpg'],
  195. [__DIR__ . '/data/color.png'],
  196. ];
  197. }
  198. /**
  199. * @dataProvider saveJpegProvider
  200. */
  201. public function testSaveJpeg($source_path)
  202. {
  203. $img = Image::fromFile($source_path);
  204. $tmp_path = tempnam(sys_get_temp_dir(), 'image');
  205. $img->saveJpeg($tmp_path);
  206. $this->assertSame(IMAGETYPE_JPEG, exif_imagetype($tmp_path));
  207. $this->assertSame($img->width, Image::fromFile($tmp_path)->width);
  208. unlink($tmp_path);
  209. }
  210. }