ImageTest.php 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. <?php
  2. namespace fphammerle\helpers\tests;
  3. use fphammerle\helpers\Image;
  4. class ImageTest extends \PHPUnit_Framework_TestCase
  5. {
  6. public function rotateProvider()
  7. {
  8. return [
  9. [__DIR__ . '/data/chainring.jpg', 90, __DIR__ . '/data/chainring-rotated-left.jpg'],
  10. [__DIR__ . '/data/chainring.jpg', 270, __DIR__ . '/data/chainring-rotated-right.jpg'],
  11. ];
  12. }
  13. /**
  14. * @dataProvider rotateProvider
  15. */
  16. public function testRotate($source_path, $angle, $expected_path)
  17. {
  18. $img = Image::fromFile($source_path);
  19. $tmp_path = tempnam(sys_get_temp_dir(), 'image');
  20. $img->rotate($angle);
  21. $img->saveJpeg($tmp_path);
  22. $this->assertFileEquals($expected_path, $tmp_path);
  23. unlink($tmp_path);
  24. }
  25. public function rotateLeftProvider()
  26. {
  27. return [
  28. [__DIR__ . '/data/chainring.jpg', __DIR__ . '/data/chainring-rotated-left.jpg'],
  29. ];
  30. }
  31. /**
  32. * @dataProvider rotateLeftProvider
  33. */
  34. public function testRotateLeft($source_path, $expected_path)
  35. {
  36. $img = Image::fromFile($source_path);
  37. $tmp_path = tempnam(sys_get_temp_dir(), 'image');
  38. $img->rotateLeft();
  39. $img->saveJpeg($tmp_path);
  40. $this->assertFileEquals($expected_path, $tmp_path);
  41. unlink($tmp_path);
  42. }
  43. public function rotateRightProvider()
  44. {
  45. return [
  46. [__DIR__ . '/data/chainring.jpg', __DIR__ . '/data/chainring-rotated-right.jpg'],
  47. ];
  48. }
  49. /**
  50. * @dataProvider rotateRightProvider
  51. */
  52. public function testRotateRight($source_path, $expected_path)
  53. {
  54. $img = Image::fromFile($source_path);
  55. $tmp_path = tempnam(sys_get_temp_dir(), 'image');
  56. $img->rotateRight();
  57. $img->saveJpeg($tmp_path);
  58. $this->assertFileEquals($expected_path, $tmp_path);
  59. unlink($tmp_path);
  60. }
  61. public function testSaveJpeg()
  62. {
  63. $img = Image::fromFile(__DIR__ . '/data/chainring.jpg');
  64. $tmp_path = tempnam(sys_get_temp_dir(), 'image');
  65. $img->saveJpeg($tmp_path);
  66. $this->assertFileEquals(__DIR__ . '/data/chainring-saved.jpg', $tmp_path);
  67. unlink($tmp_path);
  68. }
  69. }