1
0
mirror of https://github.com/Intervention/image.git synced 2025-01-17 04:08:14 +01:00

Change signature of ImageManager

This commit is contained in:
Oliver Vogel 2023-11-03 15:40:49 +01:00
parent 03130eff4e
commit 7eb29db7b8
3 changed files with 66 additions and 16 deletions

View File

@ -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');

View File

@ -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);
}
}

View File

@ -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);
}