From d3321a7c6f0ca3d63d58a0ae148dc4fa5e519d9c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A1gi-Kaz=C3=A1r=20M=C3=A1rk?= Date: Thu, 31 Jul 2014 19:23:57 +0200 Subject: [PATCH] Adds ImageManagerBasic --- src/Intervention/Image/ImageManagerBasic.php | 108 ++++++++++++++++++ src/Intervention/Image/ImageManagerStatic.php | 10 +- tests/ImageManagerStaticTest.php | 8 +- 3 files changed, 117 insertions(+), 9 deletions(-) create mode 100644 src/Intervention/Image/ImageManagerBasic.php diff --git a/src/Intervention/Image/ImageManagerBasic.php b/src/Intervention/Image/ImageManagerBasic.php new file mode 100644 index 00000000..9ec10470 --- /dev/null +++ b/src/Intervention/Image/ImageManagerBasic.php @@ -0,0 +1,108 @@ + 'gd', + ); + + /** + * Creates new instance of Image Manager + * + * @param array $config + */ + public function __construct(array $config = array()) + { + $this->config = $this->defaultConfig + $config; + } + + /** + * Creates a driver instance according to config settings + * + * @return Intervention\Image\AbstractDriver + */ + private function createDriver() + { + $drivername = ucfirst($this->config['driver']); + $driverclass = sprintf('Intervention\\Image\\%s\\Driver', $drivername); + + if (class_exists($driverclass)) { + return new $driverclass; + } + + throw new \Intervention\Image\Exception\NotSupportedException( + "Driver ({$drivername}) could not be instantiated." + ); + } + + /** + * Initiates an Image instance from different input types + * + * @param mixed $data + * + * @return Intervention\Image\Image + */ + public function make($data) + { + return $this->createDriver()->init($data); + } + + /** + * Creates an empty image canvas + * + * @param integer $width + * @param integer $height + * @param mixed $background + * + * @return Intervention\Image\Image + */ + public function canvas($width, $height, $background = null) + { + return $this->createDriver()->newImage($width, $height, $background); + } + + /** + * Create new cached image and run callback + * (requires additional package intervention/imagecache) + * + * @param Closure $callback + * @param integer $lifetime + * @param boolean $returnObj + * + * @return Image + */ + public function cache(Closure $callback, $lifetime = null, $returnObj = false) + { + if (class_exists('Intervention\\Image\\ImageCache')) { + // create imagecache + $imagecache = new ImageCache($this); + + // run callback + if (is_callable($callback)) { + $callback($imagecache); + } + + return $imagecache->get($lifetime, $returnObj); + } + + throw new \Intervention\Image\Exception\NotSupportedException( + "Please install package intervention/imagecache before running this function." + ); + } +} diff --git a/src/Intervention/Image/ImageManagerStatic.php b/src/Intervention/Image/ImageManagerStatic.php index 79e1aaa6..14e2f515 100644 --- a/src/Intervention/Image/ImageManagerStatic.php +++ b/src/Intervention/Image/ImageManagerStatic.php @@ -7,20 +7,20 @@ use Closure; class ImageManagerStatic { /** - * Instance of Intervention\Image\ImageManager + * Instance of Intervention\Image\ImageManagerBasic * - * @var ImageManager + * @var ImageManagerBasic */ public $manager; /** * Creates a new instance * - * @param ImageManager $manager + * @param ImageManagerBasic $manager */ - public function __construct(ImageManager $manager = null) + public function __construct(ImageManagerBasic $manager = null) { - $this->manager = $manager ? $manager : new ImageManager; + $this->manager = $manager ? $manager : new ImageManagerBasic; } /** diff --git a/tests/ImageManagerStaticTest.php b/tests/ImageManagerStaticTest.php index 2153fa5f..d42301de 100644 --- a/tests/ImageManagerStaticTest.php +++ b/tests/ImageManagerStaticTest.php @@ -8,10 +8,10 @@ class ImageManagerStaticTest extends PHPUnit_Framework_TestCase { Mockery::close(); } - + public function testNewInstance() { - $manager = Mockery::mock('Intervention\Image\ImageManager'); + $manager = Mockery::mock('Intervention\Image\ImageManagerBasic'); $managerStatic = new ImageManagerStatic($manager); $m = $managerStatic->newInstance(); $this->assertInstanceOf('Intervention\Image\ImageManagerStatic', $m); @@ -19,7 +19,7 @@ class ImageManagerStaticTest extends PHPUnit_Framework_TestCase public function testMake() { - $manager = Mockery::mock('Intervention\Image\ImageManager'); + $manager = Mockery::mock('Intervention\Image\ImageManagerBasic'); $managerStatic = new ImageManagerStatic($manager); $img = $managerStatic->make('tests/images/tile.png'); $this->assertInstanceOf('Intervention\Image\Image', $img); @@ -27,7 +27,7 @@ class ImageManagerStaticTest extends PHPUnit_Framework_TestCase public function testCanvas() { - $manager = Mockery::mock('Intervention\Image\ImageManager'); + $manager = Mockery::mock('Intervention\Image\ImageManagerBasic'); $managerStatic = new ImageManagerStatic($manager); $img = $managerStatic->canvas(100, 100); $this->assertInstanceOf('Intervention\Image\Image', $img);