ImageTest.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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. [__DIR__ . '/data/chainring.jpg', 90, __DIR__ . '/data/chainring-rotated-left.jpg'],
  37. [__DIR__ . '/data/chainring.jpg', 270, __DIR__ . '/data/chainring-rotated-right.jpg'],
  38. ];
  39. }
  40. /**
  41. * @dataProvider rotateProvider
  42. */
  43. public function testRotate($source_path, $angle, $expected_path)
  44. {
  45. $img = Image::fromFile($source_path);
  46. $tmp_path = tempnam(sys_get_temp_dir(), 'image');
  47. $img->rotate($angle);
  48. $img->saveJpeg($tmp_path);
  49. $this->assertFileEquals($expected_path, $tmp_path);
  50. unlink($tmp_path);
  51. }
  52. public function rotateLeftProvider()
  53. {
  54. return [
  55. [__DIR__ . '/data/chainring.jpg', __DIR__ . '/data/chainring-rotated-left.jpg'],
  56. ];
  57. }
  58. /**
  59. * @dataProvider rotateLeftProvider
  60. */
  61. public function testRotateLeft($source_path, $expected_path)
  62. {
  63. $img = Image::fromFile($source_path);
  64. $tmp_path = tempnam(sys_get_temp_dir(), 'image');
  65. $img->rotateLeft();
  66. $img->saveJpeg($tmp_path);
  67. $this->assertFileEquals($expected_path, $tmp_path);
  68. unlink($tmp_path);
  69. }
  70. public function rotateRightProvider()
  71. {
  72. return [
  73. [__DIR__ . '/data/chainring.jpg', __DIR__ . '/data/chainring-rotated-right.jpg'],
  74. ];
  75. }
  76. /**
  77. * @dataProvider rotateRightProvider
  78. */
  79. public function testRotateRight($source_path, $expected_path)
  80. {
  81. $img = Image::fromFile($source_path);
  82. $tmp_path = tempnam(sys_get_temp_dir(), 'image');
  83. $img->rotateRight();
  84. $img->saveJpeg($tmp_path);
  85. $this->assertFileEquals($expected_path, $tmp_path);
  86. unlink($tmp_path);
  87. }
  88. public function testSaveJpeg()
  89. {
  90. $img = Image::fromFile(__DIR__ . '/data/chainring.jpg');
  91. $tmp_path = tempnam(sys_get_temp_dir(), 'image');
  92. $img->saveJpeg($tmp_path);
  93. $this->assertFileEquals(__DIR__ . '/data/chainring-saved.jpg', $tmp_path);
  94. unlink($tmp_path);
  95. }
  96. }