diff --git a/src/stojg/crop/Crop.php b/src/stojg/crop/Crop.php index 9379447..c16181a 100644 --- a/src/stojg/crop/Crop.php +++ b/src/stojg/crop/Crop.php @@ -55,9 +55,22 @@ public static function mark() * @param string $imagePath - The path to an image to load. Paths can include wildcards for file names, * or can be URLs. */ - public function __construct($imagePath) + public function __construct($imagePath = null) { - $this->originalImage = new \Imagick($imagePath); + if ($imagePath) { + $this->setImage(new \Imagick($imagePath)); + } + } + + /** + * Sets the object Image to be croped + * + * @param \Imagick $image + * @return null + */ + public function setImage(\Imagick $image) + { + $this->originalImage = $image; // set base image dimensions $this->setBaseDimensions( diff --git a/tests/CropEntropyTest.php b/tests/CropEntropyTest.php index 1d39bc3..316a5a5 100644 --- a/tests/CropEntropyTest.php +++ b/tests/CropEntropyTest.php @@ -4,48 +4,65 @@ require_once 'src/stojg/crop/CropCenter.php'; require_once 'src/stojg/crop/CropEntropy.php'; +use stojg\crop\CropEntropy; + class ClassEntropyTest extends PHPUnit_Framework_TestCase { - - /** - * - * @var string - */ - protected $tempDir = ''; - - public function setUp() { - if (!extension_loaded('imagick')) { + + const EXAMPLE_IMAGE = '/images/side.png'; + + /** + * + * @var string + */ + protected $tempDir = ''; + + public function setUp() { + if (!extension_loaded('imagick')) { $this->markTestSkipped('The imagick extension is not available.'); - return; + return; + } + $this->tempDir = sys_get_temp_dir().DIRECTORY_SEPARATOR.'croptest'; + + if(file_exists($this->tempDir)) { + $this->cleanup(); + } + + if(!mkdir($this->tempDir)) { + $this->markTestSkipped('Can\'t create temp directory '. $this->tempDir .' skipping test'); + } + } + + /** + * + */ + public function tearDown() { + $this->cleanup(); + } + + public function testEntropy() { + $center = new CropEntropy(__DIR__ . self::EXAMPLE_IMAGE); + $croppedImage = $center->resizeAndCrop(200, 200); + $croppedImage->writeimage($this->tempDir.'/entropy-test.png'); + } + + public function testEntropyWithPreviusImagick() { + $image = new Imagick(__DIR__ . self::EXAMPLE_IMAGE); + + $center = new CropEntropy(); + $center->setImage($image); + + $croppedImage = $center->resizeAndCrop(200, 200); + + $this->assertSame($image, $croppedImage); + + $croppedImage->writeimage($this->tempDir.'/entropy-test.png'); + } + + private function cleanup() { + $testFiles = glob($this->tempDir.DIRECTORY_SEPARATOR.'*'); + foreach($testFiles as $file) { + unlink($file); } - $this->tempDir = sys_get_temp_dir().DIRECTORY_SEPARATOR.'croptest'; - - if(file_exists($this->tempDir)) { - $this->cleanup(); - } - - if(!mkdir($this->tempDir)) { - $this->markTestSkipped('Can\'t create temp directory '. $this->tempDir .' skipping test'); - } - } - - /** - * - */ - public function tearDown() { - $this->cleanup(); - } - - public function testEntropy() { - $center = new \stojg\crop\CropEntropy(__DIR__.'/images/side.png'); - $croppedImage = $center->resizeAndCrop(200, 200); - $croppedImage->writeimage($this->tempDir.'/entropy-test.png'); - } - - private function cleanup() { - $testFiles = glob($this->tempDir.DIRECTORY_SEPARATOR.'*'); - foreach($testFiles as $file) { - unlink($file); - } - rmdir($this->tempDir); - } + rmdir($this->tempDir); + } }