diff --git a/readme.md b/readme.md index 598b4784..e7591058 100644 --- a/readme.md +++ b/readme.md @@ -20,7 +20,7 @@ operations. ```php // create image manager with desired driver -$manager = new ImageManager(['driver' => 'gd']); +$manager = new ImageManager(driver: 'gd'); // open an image file $image = $manager->read('images/example.gif'); diff --git a/src/ImageManager.php b/src/ImageManager.php index b73c3880..9a682328 100644 --- a/src/ImageManager.php +++ b/src/ImageManager.php @@ -2,6 +2,7 @@ namespace Intervention\Image; +use Intervention\Image\Exceptions\ConfigurationException; use Intervention\Image\Interfaces\ImageInterface; use Intervention\Image\Traits\CanResolveDriverClass; @@ -9,18 +10,55 @@ class ImageManager { use CanResolveDriverClass; - private static $required_options = ['driver']; + protected const AVAILABLE_DRIVERS = ['gd', 'imagick']; - public function __construct(protected array $options = ['driver' => 'gd']) + /** + * Create new ImageManager instance + * + * @param string $driver + * @return void + * @throws ConfigurationException + */ + public function __construct(protected string $driver = 'gd') { - if (count(array_intersect(array_keys($options), self::$required_options)) != count(self::$required_options)) { - throw new Exceptions\ConfigurationException( - 'The following attributes are required to initialize ImageManager: ' . - implode(', ', self::$required_options) + if (! in_array(strtolower($driver), self::AVAILABLE_DRIVERS)) { + throw new ConfigurationException( + 'Driver ' . $driver . ' not available.' ); } } + /** + * Static constructor to create ImageManager with given driver + * + * @param string $driver + * @return ImageManager + */ + public static function withDriver(string $driver): self + { + return new self($driver); + } + + /** + * Static helper to create ImageManager with GD driver + * + * @return ImageManager + */ + public static function gd(): self + { + return new self('gd'); + } + + /** + * Static constructor to create ImageManager with Imagick driver + * + * @return ImageManager + */ + public static function imagick(): self + { + return new self('imagick'); + } + /** * Create new image instance from scratch * @@ -62,6 +100,6 @@ class ImageManager */ protected function getCurrentDriver(): string { - return strtolower($this->options['driver']); + return strtolower($this->driver); } } diff --git a/tests/ImageManagerTest.php b/tests/ImageManagerTest.php index 565eb5f7..aafecad3 100644 --- a/tests/ImageManagerTest.php +++ b/tests/ImageManagerTest.php @@ -13,20 +13,32 @@ class ImageManagerTest extends TestCase { public function testConstructor() { - $manager = new ImageManager(['driver' => 'gd']); + $manager = new ImageManager(driver: 'gd'); $this->assertInstanceOf(ImageManager::class, $manager); $this->expectException(ConfigurationException::class); - $manager = new ImageManager([]); + $manager = new ImageManager('foo'); + } - $this->expectException(ConfigurationException::class); - $manager = new ImageManager(['foo' => 'bar']); + public function testWithDriver(): void + { + $manager = ImageManager::withDriver('gd'); + $this->assertInstanceOf(ImageManager::class, $manager); + } + + public function testDriverStatics(): void + { + $manager = ImageManager::gd(); + $this->assertInstanceOf(ImageManager::class, $manager); + + $manager = ImageManager::imagick(); + $this->assertInstanceOf(ImageManager::class, $manager); } /** @requires extension gd */ public function testCreateGd() { - $manager = new ImageManager(['driver' => 'gd']); + $manager = new ImageManager('gd'); $image = $manager->create(5, 4); $this->assertInstanceOf(ImageInterface::class, $image); } @@ -34,7 +46,7 @@ class ImageManagerTest extends TestCase /** @requires extension gd */ public function testReadGd() { - $manager = new ImageManager(['driver' => 'gd']); + $manager = new ImageManager('gd'); $image = $manager->read(__DIR__ . '/images/red.gif'); $this->assertInstanceOf(ImageInterface::class, $image); } @@ -42,7 +54,7 @@ class ImageManagerTest extends TestCase /** @requires extension imagick */ public function testCreateImagick() { - $manager = new ImageManager(['driver' => 'imagick']); + $manager = new ImageManager('imagick'); $image = $manager->create(5, 4); $this->assertInstanceOf(ImageInterface::class, $image); } @@ -50,7 +62,7 @@ class ImageManagerTest extends TestCase /** @requires extension imagick */ public function testReadImagick() { - $manager = new ImageManager(['driver' => 'imagick']); + $manager = new ImageManager('imagick'); $image = $manager->read(__DIR__ . '/images/red.gif'); $this->assertInstanceOf(ImageInterface::class, $image); }