Image.resize.phpt 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. <?php
  2. /**
  3. * Test: Nette\Utils\Image crop, resize & flip.
  4. * @phpExtension gd
  5. */
  6. declare(strict_types=1);
  7. use Nette\Utils\Image;
  8. use Tester\Assert;
  9. require __DIR__ . '/../bootstrap.php';
  10. $main = Image::fromFile(__DIR__ . '/fixtures.images/logo.gif');
  11. test('cropping...', function () use ($main) {
  12. $image = clone $main;
  13. Assert::same(176, $image->width);
  14. Assert::same(104, $image->height);
  15. $image->crop(10, 20, 50, 300);
  16. Assert::same(50, $image->width);
  17. Assert::same(84, $image->height);
  18. });
  19. test('resizing X', function () use ($main) {
  20. $image = clone $main;
  21. $image->resize(150, null);
  22. Assert::same(150, $image->width);
  23. Assert::same(89, $image->height);
  24. });
  25. test('resizing Y shrink', function () use ($main) {
  26. $image = clone $main;
  27. $image->resize(null, 150, Image::ShrinkOnly);
  28. Assert::same(176, $image->width);
  29. Assert::same(104, $image->height);
  30. });
  31. test('resizing X Y shrink', function () use ($main) {
  32. $image = clone $main;
  33. $image->resize(300, 150, Image::ShrinkOnly);
  34. Assert::same(176, $image->width);
  35. Assert::same(104, $image->height);
  36. });
  37. test('resizing X Y', function () use ($main) {
  38. $image = clone $main;
  39. $image->resize(300, 150);
  40. Assert::same(254, $image->width);
  41. Assert::same(150, $image->height);
  42. });
  43. test('resizing X Y stretch', function () use ($main) {
  44. $image = clone $main;
  45. $image->resize(300, 100, Image::Stretch);
  46. Assert::same(300, $image->width);
  47. Assert::same(100, $image->height);
  48. });
  49. test('resizing X Y shrink stretch', function () use ($main) {
  50. $image = clone $main;
  51. $image->resize(300, 100, Image::ShrinkOnly | Image::Stretch);
  52. Assert::same(176, $image->width);
  53. Assert::same(100, $image->height);
  54. });
  55. test('resizing X%', function () use ($main) {
  56. $image = clone $main;
  57. $image->resize('110%', null);
  58. Assert::same(194, $image->width);
  59. Assert::same(115, $image->height);
  60. });
  61. test('resizing X% Y%', function () use ($main) {
  62. $image = clone $main;
  63. $image->resize('110%', '90%');
  64. Assert::same(194, $image->width);
  65. Assert::same(94, $image->height);
  66. });
  67. test('flipping X', function () use ($main) {
  68. $image = clone $main;
  69. $image->resize(-150, null);
  70. Assert::same(150, $image->width);
  71. Assert::same(89, $image->height);
  72. });
  73. test('flipping Y shrink', function () use ($main) {
  74. $image = clone $main;
  75. $image->resize(null, -150, Image::ShrinkOnly);
  76. Assert::same(176, $image->width);
  77. Assert::same(104, $image->height);
  78. });
  79. test('flipping X Y shrink', function () use ($main) {
  80. $image = clone $main;
  81. $image->resize(-300, -150, Image::ShrinkOnly);
  82. Assert::same(176, $image->width);
  83. Assert::same(104, $image->height);
  84. });
  85. test('exact resize', function () use ($main) {
  86. $image = clone $main;
  87. $image->resize(300, 150, Image::Cover);
  88. Assert::same(300, $image->width);
  89. Assert::same(150, $image->height);
  90. });
  91. test('rotate', function () use ($main) {
  92. $image = clone $main;
  93. $image->rotate(90, Image::rgb(0, 0, 0));
  94. Assert::same(104, $image->width);
  95. Assert::same(176, $image->height);
  96. });
  97. test('alpha crop', function () use ($main) {
  98. $image = Image::fromFile(__DIR__ . '/fixtures.images/alpha1.png');
  99. $image->crop(1, 1, 8, 8);
  100. Assert::same(file_get_contents(__DIR__ . '/expected/Image.alpha.crop.png'), $image->toString($image::PNG));
  101. });
  102. test('alpha resize', function () use ($main) {
  103. $image = Image::fromFile(__DIR__ . '/fixtures.images/alpha1.png');
  104. $image->resize(20, 20);
  105. Assert::same(file_get_contents(__DIR__ . '/expected/Image.alpha.resize1.png'), $image->toString($image::PNG));
  106. });
  107. test('alpha flip', function () use ($main) {
  108. $image = Image::fromFile(__DIR__ . '/fixtures.images/alpha1.png');
  109. $image->resize(-10, -10);
  110. Assert::same(file_get_contents(__DIR__ . '/expected/Image.alpha.flip1.png'), $image->toString($image::PNG));
  111. });
  112. test('palette alpha resize', function () use ($main) {
  113. $image = Image::fromFile(__DIR__ . '/fixtures.images/alpha3.gif');
  114. $image->resize(20, 20);
  115. Assert::same(file_get_contents(__DIR__ . '/expected/Image.alpha.resize2.png'), $image->toString($image::PNG));
  116. });