diff --git a/composer.json b/composer.json index 3af7dd3..9d6f0d2 100644 --- a/composer.json +++ b/composer.json @@ -55,6 +55,7 @@ "yiisoft/yii2-codeception": "self.version", "yiisoft/yii2-debug": "self.version", "yiisoft/yii2-elasticsearch": "self.version", + "yiisoft/yii2-imagine": "self.version", "yiisoft/yii2-gii": "self.version", "yiisoft/yii2-jui": "self.version", "yiisoft/yii2-mongodb": "self.version", @@ -81,6 +82,7 @@ "ext-mongo": ">=1.3.0", "ext-pdo": "*", "ext-pdo_mysql": "*", + "imagine/imagine": "v0.5.0", "smarty/smarty": "*", "swiftmailer/swiftmailer": "*", "twig/twig": "*" @@ -91,6 +93,7 @@ "ext-mongo": "required by yii2-mongo extension", "ext-pdo": "required by yii2-sphinx extension", "ext-pdo_mysql": "required by yii2-sphinx extension", + "imagine/imagine": "required by yii2-imagine extension", "smarty/smarty": "required by yii2-smarty extension", "swiftmailer/swiftmailer": "required by yii2-swiftmailer extension", "twig/twig": "required by yii2-twig extension" @@ -103,6 +106,7 @@ "yii\\debug\\": "extensions/", "yii\\elasticsearch\\": "extensions/", "yii\\gii\\": "extensions/", + "yii\\imagine\\" : "extensions/", "yii\\jui\\": "extensions/", "yii\\mongodb\\": "extensions/", "yii\\redis\\": "extensions/", diff --git a/tests/unit/data/imagine/GothamRnd-Light.otf b/tests/unit/data/imagine/GothamRnd-Light.otf new file mode 100644 index 0000000..4181a78 Binary files /dev/null and b/tests/unit/data/imagine/GothamRnd-Light.otf differ diff --git a/tests/unit/data/imagine/large.jpg b/tests/unit/data/imagine/large.jpg new file mode 100644 index 0000000..81c47e5 Binary files /dev/null and b/tests/unit/data/imagine/large.jpg differ diff --git a/tests/unit/data/imagine/xparent.gif b/tests/unit/data/imagine/xparent.gif new file mode 100644 index 0000000..2e6fd66 Binary files /dev/null and b/tests/unit/data/imagine/xparent.gif differ diff --git a/tests/unit/extensions/imagine/AbstractImageTest.php b/tests/unit/extensions/imagine/AbstractImageTest.php new file mode 100644 index 0000000..d757b0a --- /dev/null +++ b/tests/unit/extensions/imagine/AbstractImageTest.php @@ -0,0 +1,118 @@ +imageFile = Yii::getAlias('@yiiunit/data/imagine/large') . '.jpg'; + $this->watermarkFile = Yii::getAlias('@yiiunit/data/imagine/xparent') . '.gif'; + $this->runtimeTextFile = Yii::getAlias('@yiiunit/runtime/image-text-test') . '.png'; + $this->runtimeWatermarkFile = Yii::getAlias('@yiiunit/runtime/image-watermark-test') . '.png'; + parent::setUp(); + } + + protected function tearDown() + { + @unlink($this->runtimeTextFile); + @unlink($this->runtimeWatermarkFile); + } + + public function testText() { + if(!$this->isFontTestSupported()) { + $this->markTestSkipped('Skipping ImageGdTest Gd not installed'); + } + + $fontFile = Yii::getAlias('@yiiunit/data/imagine/GothamRnd-Light') . '.otf'; + + $img = $this->image->text($this->imageFile, 'Yii-2 Image', [ + 'font' => $fontFile, + 'size' => 12, + 'color' => '000' + ]); + + $img->save($this->runtimeTextFile); + $this->assertTrue(file_exists($this->runtimeTextFile)); + + } + + public function testCrop() + { + $point = [20,20]; + $img = $this->image->crop($this->imageFile, 100, 100, $point); + + $this->assertEquals(100, $img->getSize()->getWidth()); + $this->assertEquals(100, $img->getSize()->getHeight()); + + $point = new Point(20, 20); + $img = $this->image->crop($this->imageFile, 100, 100, $point); + $this->assertEquals(100, $img->getSize()->getWidth()); + $this->assertEquals(100, $img->getSize()->getHeight()); + + } + + public function testWatermark() + { + $img = $this->image->watermark($this->imageFile, $this->watermarkFile); + $img->save($this->runtimeWatermarkFile); + $this->assertTrue(file_exists($this->runtimeWatermarkFile)); + } + + public function testFrame() + { + $frameSize = 5; + $original = $this->image->getImagine()->open($this->imageFile); + $originalSize = $original->getSize(); + $img = $this->image->frame($this->imageFile, $frameSize, '666', 0); + $size = $img->getSize(); + + $this->assertEquals($size->getWidth(), $originalSize->getWidth() + ($frameSize * 2)); + } + + public function testThumbnail() + { + $img = $this->image->thumbnail($this->imageFile, 120, 120); + + $this->assertEquals(120, $img->getSize()->getWidth()); + $this->assertEquals(120, $img->getSize()->getHeight()); + } + + /** + * @expectedException \yii\base\InvalidConfigException + */ + public function testShouldThrowExceptionOnDriverInvalidArgument() { + $this->image->setDriver('fake-driver'); + } + + /** + * @expectedException \InvalidArgumentException + */ + public function testShouldThrowExceptionOnCropInvalidArgument() { + $this->image->crop($this->imageFile, 100, 100, new \stdClass()); + } + + /** + * @expectedException \InvalidArgumentException + */ + public function testShouldThrowExceptionOnWatermarkInvalidArgument() { + $this->image->watermark($this->imageFile, $this->watermarkFile, new \stdClass()); + } + + + abstract protected function isFontTestSupported(); +} diff --git a/tests/unit/extensions/imagine/ImageGdTest.php b/tests/unit/extensions/imagine/ImageGdTest.php new file mode 100644 index 0000000..1fa73fb --- /dev/null +++ b/tests/unit/extensions/imagine/ImageGdTest.php @@ -0,0 +1,31 @@ +markTestSkipped('Skipping ImageGdTest, Gd not installed'); + } else { + $this->image = new Image(); + $this->image->setDriver(Image::DRIVER_GD2); + parent::setUp(); + } + } + + protected function isFontTestSupported() + { + $infos = gd_info(); + return isset($infos['FreeType Support']) ? $infos['FreeType Support'] : false; + } + +} \ No newline at end of file diff --git a/tests/unit/extensions/imagine/ImageGmagickTest.php b/tests/unit/extensions/imagine/ImageGmagickTest.php new file mode 100644 index 0000000..3a6daac --- /dev/null +++ b/tests/unit/extensions/imagine/ImageGmagickTest.php @@ -0,0 +1,30 @@ +markTestSkipped('Skipping ImageGmagickTest, Gmagick is not installed'); + } else { + $this->image = new Image(); + $this->image->setDriver(Image::DRIVER_GMAGICK); + parent::setUp(); + } + } + + protected function isFontTestSupported() + { + return true; + } + +} \ No newline at end of file diff --git a/tests/unit/extensions/imagine/ImageImagickTest.php b/tests/unit/extensions/imagine/ImageImagickTest.php new file mode 100644 index 0000000..26b0545 --- /dev/null +++ b/tests/unit/extensions/imagine/ImageImagickTest.php @@ -0,0 +1,30 @@ +markTestSkipped('Skipping ImageImagickTest, Imagick is not installed'); + } else { + $this->image = new Image(); + $this->image->setDriver(Image::DRIVER_IMAGICK); + parent::setUp(); + } + } + + protected function isFontTestSupported() + { + return true; + } + +} \ No newline at end of file