From 79bc7874e566bfbb1dc0ac15e1f1e0dab33a8efe Mon Sep 17 00:00:00 2001 From: Oliver Vogel Date: Thu, 5 Jun 2014 17:25:48 +0200 Subject: [PATCH] renamed classes --- ...AbstractSource.php => AbstractDecoder.php} | 6 +- src/Intervention/Image/AbstractDriver.php | 8 +-- .../Image/Gd/Commands/ColorsCommand.php | 68 +++++++++++++++++++ .../Image/Gd/Commands/FillCommand.php | 4 +- .../Image/Gd/{Source.php => Decoder.php} | 2 +- src/Intervention/Image/Gd/Driver.php | 6 +- .../Image/Imagick/Commands/ColorsCommand.php | 11 +++ .../Image/Imagick/Commands/FillCommand.php | 4 +- .../Image/Imagick/Commands/InsertCommand.php | 2 - .../Image/Imagick/{Source.php => Decoder.php} | 2 +- src/Intervention/Image/Imagick/Driver.php | 6 +- ...SourceTest.php => AbstractDecoderTest.php} | 44 ++++++------ tests/DriverTest.php | 8 +-- 13 files changed, 124 insertions(+), 47 deletions(-) rename src/Intervention/Image/{AbstractSource.php => AbstractDecoder.php} (97%) create mode 100644 src/Intervention/Image/Gd/Commands/ColorsCommand.php rename src/Intervention/Image/Gd/{Source.php => Decoder.php} (98%) create mode 100644 src/Intervention/Image/Imagick/Commands/ColorsCommand.php rename src/Intervention/Image/Imagick/{Source.php => Decoder.php} (97%) rename tests/{AbstractSourceTest.php => AbstractDecoderTest.php} (54%) diff --git a/src/Intervention/Image/AbstractSource.php b/src/Intervention/Image/AbstractDecoder.php similarity index 97% rename from src/Intervention/Image/AbstractSource.php rename to src/Intervention/Image/AbstractDecoder.php index 82d92d3d..c1aff632 100644 --- a/src/Intervention/Image/AbstractSource.php +++ b/src/Intervention/Image/AbstractDecoder.php @@ -2,7 +2,7 @@ namespace Intervention\Image; -abstract class AbstractSource +abstract class AbstractDecoder { /** * Initiates new image from path in filesystem @@ -44,7 +44,7 @@ abstract class AbstractSource private $data; /** - * Creates new Source with data + * Creates new Decoder with data * * @param mixed $data */ @@ -180,7 +180,7 @@ abstract class AbstractSource } /** - * Source object transforms to string source data + * Decoder object transforms to string source data * * @return string */ diff --git a/src/Intervention/Image/AbstractDriver.php b/src/Intervention/Image/AbstractDriver.php index 2e8a0344..44e44773 100644 --- a/src/Intervention/Image/AbstractDriver.php +++ b/src/Intervention/Image/AbstractDriver.php @@ -5,11 +5,11 @@ namespace Intervention\Image; abstract class AbstractDriver { /** - * Source instance to init images from + * Decoder instance to init images from * - * @var Intervention\Image\AbstractSource + * @var Intervention\Image\AbstractDecoder */ - public $source; + public $decoder; /** * Image encoder instance @@ -61,7 +61,7 @@ abstract class AbstractDriver */ public function init($data) { - return $this->source->init($data); + return $this->decoder->init($data); } /** diff --git a/src/Intervention/Image/Gd/Commands/ColorsCommand.php b/src/Intervention/Image/Gd/Commands/ColorsCommand.php new file mode 100644 index 00000000..55650041 --- /dev/null +++ b/src/Intervention/Image/Gd/Commands/ColorsCommand.php @@ -0,0 +1,68 @@ +argument(0)->min(1)->value(5); + $accuracy = $this->argument(1)->between(1, 100)->value(100); + $format = $this->argument(2)->type('string')->value('hex'); + + $output = array(); + $colors = array(); + + $size = $image->getSize(); + $step_x = round($size->width / ($size->width / 100 * $accuracy)); + $step_y = round($size->height / ($size->height / 100 * $accuracy)); + + // read colors from image + for ($x=0; $x < $size->width; $x=$x+$step_x) { + for ($y=0; $y < $size->height; $y=$y+$step_y) { + $color = imagecolorat($image->getCore(), $x, $y); + if (isset($colors[$color])) { + $colors[$color]++; + } else { + $colors[$color] = 1; + } + } + } + + // sort colors by color-value + ksort($colors); + + // consolidate colors + $compare_color = null; + + foreach ($colors as $key => $value) { + $color = new Color($key); + if ( ! is_null($compare_color)) { + if ( ! $color->differs($compare_color, 80)) { + unset($colors[$key]); + } else { + $compare_color = $color; + } + } else { + $compare_color = $color; + } + } + + + // sort colors by appearance + arsort($colors); + + // slice array + $colors = array_slice($colors, 0, $number, true); + + // format colors + foreach ($colors as $color => $count) { + $color = new Color($color); + $output[] = $color->format($format); + } + + $this->setOutput($output); + } +} diff --git a/src/Intervention/Image/Gd/Commands/FillCommand.php b/src/Intervention/Image/Gd/Commands/FillCommand.php index e6cee133..fa045ea3 100644 --- a/src/Intervention/Image/Gd/Commands/FillCommand.php +++ b/src/Intervention/Image/Gd/Commands/FillCommand.php @@ -2,7 +2,7 @@ namespace Intervention\Image\Gd\Commands; -use Intervention\Image\Gd\Source; +use Intervention\Image\Gd\Decoder; use Intervention\Image\Gd\Color; class FillCommand extends \Intervention\Image\Commands\AbstractCommand @@ -26,7 +26,7 @@ class FillCommand extends \Intervention\Image\Commands\AbstractCommand try { // set image tile filling - $source = new Source; + $source = new Decoder; $tile = $source->init($filling); imagesettile($image->getCore(), $tile->getCore()); $filling = IMG_COLOR_TILED; diff --git a/src/Intervention/Image/Gd/Source.php b/src/Intervention/Image/Gd/Decoder.php similarity index 98% rename from src/Intervention/Image/Gd/Source.php rename to src/Intervention/Image/Gd/Decoder.php index bf5acf70..536200f0 100644 --- a/src/Intervention/Image/Gd/Source.php +++ b/src/Intervention/Image/Gd/Decoder.php @@ -5,7 +5,7 @@ namespace Intervention\Image\Gd; use \Intervention\Image\Image; use \Intervention\Image\Size; -class Source extends \Intervention\Image\AbstractSource +class Decoder extends \Intervention\Image\AbstractDecoder { /** * Initiates new image from path in filesystem diff --git a/src/Intervention/Image/Gd/Driver.php b/src/Intervention/Image/Gd/Driver.php index 76ca24bf..9512be81 100644 --- a/src/Intervention/Image/Gd/Driver.php +++ b/src/Intervention/Image/Gd/Driver.php @@ -9,10 +9,10 @@ class Driver extends \Intervention\Image\AbstractDriver /** * Creates new instance of driver * - * @param Intervention\Image\Gd\Source $source + * @param Intervention\Image\Gd\Decoder $decoder * @param Intervention\Image\Gd\Encoder $encoder */ - public function __construct(Source $source = null, Encoder $encoder = null) + public function __construct(Decoder $decoder = null, Encoder $encoder = null) { if ( ! $this->coreAvailable()) { throw new \Intervention\Image\Exception\NotSupportedException( @@ -20,7 +20,7 @@ class Driver extends \Intervention\Image\AbstractDriver ); } - $this->source = $source ? $source : new Source; + $this->decoder = $decoder ? $decoder : new Decoder; $this->encoder = $encoder ? $encoder : new Encoder; } diff --git a/src/Intervention/Image/Imagick/Commands/ColorsCommand.php b/src/Intervention/Image/Imagick/Commands/ColorsCommand.php new file mode 100644 index 00000000..25f07861 --- /dev/null +++ b/src/Intervention/Image/Imagick/Commands/ColorsCommand.php @@ -0,0 +1,11 @@ +init($filling); } catch (\Intervention\Image\Exception\NotReadableException $e) { diff --git a/src/Intervention/Image/Imagick/Commands/InsertCommand.php b/src/Intervention/Image/Imagick/Commands/InsertCommand.php index 83bd22dc..24d535bc 100644 --- a/src/Intervention/Image/Imagick/Commands/InsertCommand.php +++ b/src/Intervention/Image/Imagick/Commands/InsertCommand.php @@ -2,8 +2,6 @@ namespace Intervention\Image\Imagick\Commands; -use \Intervention\Image\Imagick\Source; - class InsertCommand extends \Intervention\Image\Commands\AbstractCommand { /** diff --git a/src/Intervention/Image/Imagick/Source.php b/src/Intervention/Image/Imagick/Decoder.php similarity index 97% rename from src/Intervention/Image/Imagick/Source.php rename to src/Intervention/Image/Imagick/Decoder.php index cf53d641..dab86296 100644 --- a/src/Intervention/Image/Imagick/Source.php +++ b/src/Intervention/Image/Imagick/Decoder.php @@ -5,7 +5,7 @@ namespace Intervention\Image\Imagick; use \Intervention\Image\Image; use \Intervention\Image\Size; -class Source extends \Intervention\Image\AbstractSource +class Decoder extends \Intervention\Image\AbstractDecoder { /** * Initiates new image from path in filesystem diff --git a/src/Intervention/Image/Imagick/Driver.php b/src/Intervention/Image/Imagick/Driver.php index 2758b4ee..ef4a8fd7 100644 --- a/src/Intervention/Image/Imagick/Driver.php +++ b/src/Intervention/Image/Imagick/Driver.php @@ -9,10 +9,10 @@ class Driver extends \Intervention\Image\AbstractDriver /** * Creates new instance of driver * - * @param Intervention\Image\Imagick\Source $source + * @param Intervention\Image\Imagick\Decoder $decoder * @param Intervention\Image\Imagick\Encoder $encoder */ - public function __construct(Source $source = null, Encoder $encoder = null) + public function __construct(Decoder $decoder = null, Encoder $encoder = null) { if ( ! $this->coreAvailable()) { throw new \Intervention\Image\Exception\NotSupportedException( @@ -20,7 +20,7 @@ class Driver extends \Intervention\Image\AbstractDriver ); } - $this->source = $source ? $source : new Source; + $this->decoder = $decoder ? $decoder : new Decoder; $this->encoder = $encoder ? $encoder : new Encoder; } diff --git a/tests/AbstractSourceTest.php b/tests/AbstractDecoderTest.php similarity index 54% rename from tests/AbstractSourceTest.php rename to tests/AbstractDecoderTest.php index 814a2b5e..2ec8ae9a 100644 --- a/tests/AbstractSourceTest.php +++ b/tests/AbstractDecoderTest.php @@ -1,8 +1,8 @@ getTestSource(new \Imagick); + $source = $this->getTestDecoder(new \Imagick); $this->assertTrue($source->isImagick()); - $source = $this->getTestSource(new StdClass); + $source = $this->getTestDecoder(new StdClass); $this->assertFalse($source->isImagick()); - $source = $this->getTestSource(null); + $source = $this->getTestDecoder(null); $this->assertFalse($source->isImagick()); } public function testIsGdResource() { $resource = imagecreatefromjpeg(__DIR__.'/images/test.jpg'); - $source = $this->getTestSource($resource); + $source = $this->getTestDecoder($resource); $this->assertTrue($source->isGdResource()); - $source = $this->getTestSource(tmpfile()); + $source = $this->getTestDecoder(tmpfile()); $this->assertFalse($source->isGdResource()); - $source = $this->getTestSource(null); + $source = $this->getTestDecoder(null); $this->assertFalse($source->isGdResource()); } public function testIsFilepath() { - $source = $this->getTestSource(__DIR__.'/AbstractSourceTest.php'); + $source = $this->getTestDecoder(__DIR__.'/AbstractDecoderTest.php'); $this->assertTrue($source->isFilepath()); - $source = $this->getTestSource(null); + $source = $this->getTestDecoder(null); $this->assertFalse($source->isFilepath()); - $source = $this->getTestSource(array()); + $source = $this->getTestDecoder(array()); $this->assertFalse($source->isFilepath()); - $source = $this->getTestSource(new StdClass); + $source = $this->getTestDecoder(new StdClass); $this->assertFalse($source->isFilepath()); } public function testIsUrl() { - $source = $this->getTestSource('http://foo.bar'); + $source = $this->getTestDecoder('http://foo.bar'); $this->assertTrue($source->isUrl()); - $source = $this->getTestSource(null); + $source = $this->getTestDecoder(null); $this->assertFalse($source->isUrl()); } public function testIsBinary() { - $source = $this->getTestSource(file_get_contents(__DIR__.'/images/test.jpg')); + $source = $this->getTestDecoder(file_get_contents(__DIR__.'/images/test.jpg')); $this->assertTrue($source->isBinary()); - $source = $this->getTestSource(null); + $source = $this->getTestDecoder(null); $this->assertFalse($source->isBinary()); - $source = $this->getTestSource(1); + $source = $this->getTestDecoder(1); $this->assertFalse($source->isBinary()); - $source = $this->getTestSource(0); + $source = $this->getTestDecoder(0); $this->assertFalse($source->isBinary()); - $source = $this->getTestSource(array(1,2,3)); + $source = $this->getTestDecoder(array(1,2,3)); $this->assertFalse($source->isBinary()); - $source = $this->getTestSource(new StdClass); + $source = $this->getTestDecoder(new StdClass); $this->assertFalse($source->isBinary()); } - public function getTestSource($data) + public function getTestDecoder($data) { - return $this->getMockForAbstractClass('\Intervention\Image\AbstractSource', array($data)); + return $this->getMockForAbstractClass('\Intervention\Image\AbstractDecoder', array($data)); } } diff --git a/tests/DriverTest.php b/tests/DriverTest.php index 6f9454ad..0d8184ae 100644 --- a/tests/DriverTest.php +++ b/tests/DriverTest.php @@ -13,7 +13,7 @@ class DriverTest extends PHPUnit_Framework_TestCase public function testNewImageGd() { $driver = new GdDriver( - Mockery::mock('\Intervention\Image\Gd\Source'), + Mockery::mock('\Intervention\Image\Gd\Decoder'), Mockery::mock('\Intervention\Image\Gd\Encoder') ); @@ -26,7 +26,7 @@ class DriverTest extends PHPUnit_Framework_TestCase public function testNewImageImagick() { $driver = new ImagickDriver( - Mockery::mock('\Intervention\Image\Imagick\Source'), + Mockery::mock('\Intervention\Image\Imagick\Decoder'), Mockery::mock('\Intervention\Image\Imagick\Encoder') ); @@ -39,7 +39,7 @@ class DriverTest extends PHPUnit_Framework_TestCase public function testParseColorGd() { $driver = new GdDriver( - Mockery::mock('\Intervention\Image\Gd\Source'), + Mockery::mock('\Intervention\Image\Gd\Decoder'), Mockery::mock('\Intervention\Image\Gd\Encoder') ); @@ -50,7 +50,7 @@ class DriverTest extends PHPUnit_Framework_TestCase public function testParseColorImagick() { $driver = new ImagickDriver( - Mockery::mock('\Intervention\Image\Imagick\Source'), + Mockery::mock('\Intervention\Image\Imagick\Decoder'), Mockery::mock('\Intervention\Image\Imagick\Encoder') );