AbstractImageTest.php 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. <?php
  2. namespace yiiunit\imagine;
  3. use Yii;
  4. use yii\helpers\FileHelper;
  5. use yii\imagine\Image;
  6. use Imagine\Image\ImageInterface;
  7. use Imagine\Image\Point;
  8. abstract class AbstractImageTest extends TestCase
  9. {
  10. protected $imageFile;
  11. protected $watermarkFile;
  12. protected $runtimeTextFile;
  13. protected $runtimeWatermarkFile;
  14. /**
  15. * {@inheritdoc}
  16. */
  17. protected function setUp()
  18. {
  19. FileHelper::createDirectory(Yii::getAlias('@yiiunit/imagine/runtime'));
  20. $this->imageFile = Yii::getAlias('@yiiunit/imagine/data/large.jpg');
  21. $this->watermarkFile = Yii::getAlias('@yiiunit/imagine/data/xparent.gif');
  22. $this->runtimeTextFile = Yii::getAlias('@yiiunit/imagine/runtime/image-text-test.png');
  23. $this->runtimeWatermarkFile = Yii::getAlias('@yiiunit/imagine/runtime/image-watermark-test.png');
  24. parent::setUp();
  25. }
  26. /**
  27. * {@inheritdoc}
  28. */
  29. protected function tearDown()
  30. {
  31. @unlink($this->runtimeTextFile);
  32. @unlink($this->runtimeWatermarkFile);
  33. }
  34. public function testText()
  35. {
  36. if (!$this->isFontTestSupported()) {
  37. $this->markTestSkipped('Skipping ImageGdTest Gd not installed');
  38. }
  39. $fontFile = Yii::getAlias('@yiiunit/imagine/data/GothamRnd-Light.otf');
  40. $img = Image::text($this->imageFile, 'Yii-2 Image', $fontFile, [0, 0], [
  41. 'size' => 12,
  42. 'color' => '000'
  43. ]);
  44. $img->save($this->runtimeTextFile);
  45. $this->assertTrue(file_exists($this->runtimeTextFile));
  46. }
  47. public function testCrop()
  48. {
  49. $point = [20, 20];
  50. $img = Image::crop($this->imageFile, 100, 100, $point);
  51. $this->assertEquals(100, $img->getSize()->getWidth());
  52. $this->assertEquals(100, $img->getSize()->getHeight());
  53. }
  54. public function testWatermark()
  55. {
  56. $img = Image::watermark($this->imageFile, $this->watermarkFile);
  57. $img->save($this->runtimeWatermarkFile);
  58. $this->assertTrue(file_exists($this->runtimeWatermarkFile));
  59. }
  60. public function testFrame()
  61. {
  62. $frameSize = 5;
  63. $original = Image::getImagine()->open($this->imageFile);
  64. $originalSize = $original->getSize();
  65. $img = Image::frame($this->imageFile, $frameSize, '666', 0);
  66. $size = $img->getSize();
  67. $this->assertEquals($size->getWidth(), $originalSize->getWidth() + ($frameSize * 2));
  68. }
  69. public function testThumbnail()
  70. {
  71. // THUMBNAIL_OUTBOUND mode.
  72. $img = Image::thumbnail($this->imageFile, 120, 120);
  73. $this->assertEquals(120, $img->getSize()->getWidth());
  74. $this->assertEquals(120, $img->getSize()->getHeight());
  75. // THUMBNAIL_INSET mode. Missing thumbnail part is filled with background so dimensions are exactly
  76. // the ones specified.
  77. $img = Image::thumbnail($this->imageFile, 120, 120, ImageInterface::THUMBNAIL_INSET);
  78. $this->assertEquals(120, $img->getSize()->getWidth());
  79. $this->assertEquals(120, $img->getSize()->getHeight());
  80. // Height omitted and is calculated based on original image aspect ratio regardless of the mode.
  81. $img = Image::thumbnail($this->imageFile, 120, null);
  82. $this->assertEquals(120, $img->getSize()->getWidth());
  83. $this->assertEquals(62, $img->getSize()->getHeight());
  84. $img = Image::thumbnail($this->imageFile, 120, null, ImageInterface::THUMBNAIL_INSET);
  85. $this->assertEquals(120, $img->getSize()->getWidth());
  86. $this->assertEquals(62, $img->getSize()->getHeight());
  87. // Width omitted and is calculated based on original image aspect ratio regardless of the mode.
  88. $img = Image::thumbnail($this->imageFile, null, 120);
  89. $this->assertEquals(234, $img->getSize()->getWidth());
  90. $this->assertEquals(120, $img->getSize()->getHeight());
  91. $img = Image::thumbnail($this->imageFile, null, 120, ImageInterface::THUMBNAIL_INSET);
  92. $this->assertEquals(234, $img->getSize()->getWidth());
  93. $this->assertEquals(120, $img->getSize()->getHeight());
  94. }
  95. /**
  96. * @dataProvider providerResize
  97. */
  98. public function testResize($width, $height, $keepAspectRatio, $allowUpscaling, $newWidth, $newHeight)
  99. {
  100. $img = Image::resize($this->imageFile, $width, $height, $keepAspectRatio, $allowUpscaling);
  101. $this->assertEquals($newWidth, $img->getSize()->getWidth());
  102. $this->assertEquals($newHeight, $img->getSize()->getHeight());
  103. }
  104. public function providerResize()
  105. {
  106. // [width, height, keepAspectRatio, allowUpscaling, newWidth, newHeight]
  107. return [
  108. 'Height and width set. Image should keep aspect ratio.' =>
  109. [350, 350, true, false, 350, 180],
  110. 'Height and width set. Image should be resized to exact dimensions.' =>
  111. [350, 350, false, false, 350, 350],
  112. 'Height omitted and is calculated based on original image aspect ratio.' =>
  113. [350, null, true, false, 350, 180],
  114. 'Width omitted and is calculated based on original image aspect ratio.' =>
  115. [null, 180, true, false, 350, 180],
  116. 'Upscaling' =>
  117. [800, 800, true, true, 800, 411],
  118. ];
  119. }
  120. /**
  121. * @expectedException \yii\base\InvalidConfigException
  122. */
  123. public function testShouldThrowExceptionOnDriverInvalidArgument()
  124. {
  125. Image::setImagine(null);
  126. Image::$driver = 'fake-driver';
  127. Image::getImagine();
  128. }
  129. public function testIfAutoRotateThrowsException()
  130. {
  131. $img = Image::thumbnail($this->imageFile, 120, 120);
  132. $this->assertInstanceOf('\Imagine\Image\ImageInterface', Image::autorotate($img));
  133. }
  134. abstract protected function isFontTestSupported();
  135. }