Skip to content
This repository has been archived by the owner on Apr 30, 2021. It is now read-only.

Commit

Permalink
Merge pull request #6 from mcuadros/master
Browse files Browse the repository at this point in the history
Better abstraction, now is possible to inject the Imagick object
  • Loading branch information
Stig Lindqvist committed Dec 7, 2013
2 parents 97dc761 + a3a7ba1 commit db98b4c
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 43 deletions.
17 changes: 15 additions & 2 deletions src/stojg/crop/Crop.php
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
99 changes: 58 additions & 41 deletions tests/CropEntropyTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}

0 comments on commit db98b4c

Please # to comment.