mirror of
https://github.com/Intervention/image.git
synced 2025-01-17 20:28:21 +01:00
Change signature of ImageManager
This commit is contained in:
parent
03130eff4e
commit
7eb29db7b8
@ -20,7 +20,7 @@ operations.
|
|||||||
|
|
||||||
```php
|
```php
|
||||||
// create image manager with desired driver
|
// create image manager with desired driver
|
||||||
$manager = new ImageManager(['driver' => 'gd']);
|
$manager = new ImageManager(driver: 'gd');
|
||||||
|
|
||||||
// open an image file
|
// open an image file
|
||||||
$image = $manager->read('images/example.gif');
|
$image = $manager->read('images/example.gif');
|
||||||
|
@ -2,6 +2,7 @@
|
|||||||
|
|
||||||
namespace Intervention\Image;
|
namespace Intervention\Image;
|
||||||
|
|
||||||
|
use Intervention\Image\Exceptions\ConfigurationException;
|
||||||
use Intervention\Image\Interfaces\ImageInterface;
|
use Intervention\Image\Interfaces\ImageInterface;
|
||||||
use Intervention\Image\Traits\CanResolveDriverClass;
|
use Intervention\Image\Traits\CanResolveDriverClass;
|
||||||
|
|
||||||
@ -9,18 +10,55 @@ class ImageManager
|
|||||||
{
|
{
|
||||||
use CanResolveDriverClass;
|
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)) {
|
if (! in_array(strtolower($driver), self::AVAILABLE_DRIVERS)) {
|
||||||
throw new Exceptions\ConfigurationException(
|
throw new ConfigurationException(
|
||||||
'The following attributes are required to initialize ImageManager: ' .
|
'Driver ' . $driver . ' not available.'
|
||||||
implode(', ', self::$required_options)
|
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 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
|
* Create new image instance from scratch
|
||||||
*
|
*
|
||||||
@ -62,6 +100,6 @@ class ImageManager
|
|||||||
*/
|
*/
|
||||||
protected function getCurrentDriver(): string
|
protected function getCurrentDriver(): string
|
||||||
{
|
{
|
||||||
return strtolower($this->options['driver']);
|
return strtolower($this->driver);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -13,20 +13,32 @@ class ImageManagerTest extends TestCase
|
|||||||
{
|
{
|
||||||
public function testConstructor()
|
public function testConstructor()
|
||||||
{
|
{
|
||||||
$manager = new ImageManager(['driver' => 'gd']);
|
$manager = new ImageManager(driver: 'gd');
|
||||||
$this->assertInstanceOf(ImageManager::class, $manager);
|
$this->assertInstanceOf(ImageManager::class, $manager);
|
||||||
|
|
||||||
$this->expectException(ConfigurationException::class);
|
$this->expectException(ConfigurationException::class);
|
||||||
$manager = new ImageManager([]);
|
$manager = new ImageManager('foo');
|
||||||
|
}
|
||||||
|
|
||||||
$this->expectException(ConfigurationException::class);
|
public function testWithDriver(): void
|
||||||
$manager = new ImageManager(['foo' => 'bar']);
|
{
|
||||||
|
$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 */
|
/** @requires extension gd */
|
||||||
public function testCreateGd()
|
public function testCreateGd()
|
||||||
{
|
{
|
||||||
$manager = new ImageManager(['driver' => 'gd']);
|
$manager = new ImageManager('gd');
|
||||||
$image = $manager->create(5, 4);
|
$image = $manager->create(5, 4);
|
||||||
$this->assertInstanceOf(ImageInterface::class, $image);
|
$this->assertInstanceOf(ImageInterface::class, $image);
|
||||||
}
|
}
|
||||||
@ -34,7 +46,7 @@ class ImageManagerTest extends TestCase
|
|||||||
/** @requires extension gd */
|
/** @requires extension gd */
|
||||||
public function testReadGd()
|
public function testReadGd()
|
||||||
{
|
{
|
||||||
$manager = new ImageManager(['driver' => 'gd']);
|
$manager = new ImageManager('gd');
|
||||||
$image = $manager->read(__DIR__ . '/images/red.gif');
|
$image = $manager->read(__DIR__ . '/images/red.gif');
|
||||||
$this->assertInstanceOf(ImageInterface::class, $image);
|
$this->assertInstanceOf(ImageInterface::class, $image);
|
||||||
}
|
}
|
||||||
@ -42,7 +54,7 @@ class ImageManagerTest extends TestCase
|
|||||||
/** @requires extension imagick */
|
/** @requires extension imagick */
|
||||||
public function testCreateImagick()
|
public function testCreateImagick()
|
||||||
{
|
{
|
||||||
$manager = new ImageManager(['driver' => 'imagick']);
|
$manager = new ImageManager('imagick');
|
||||||
$image = $manager->create(5, 4);
|
$image = $manager->create(5, 4);
|
||||||
$this->assertInstanceOf(ImageInterface::class, $image);
|
$this->assertInstanceOf(ImageInterface::class, $image);
|
||||||
}
|
}
|
||||||
@ -50,7 +62,7 @@ class ImageManagerTest extends TestCase
|
|||||||
/** @requires extension imagick */
|
/** @requires extension imagick */
|
||||||
public function testReadImagick()
|
public function testReadImagick()
|
||||||
{
|
{
|
||||||
$manager = new ImageManager(['driver' => 'imagick']);
|
$manager = new ImageManager('imagick');
|
||||||
$image = $manager->read(__DIR__ . '/images/red.gif');
|
$image = $manager->read(__DIR__ . '/images/red.gif');
|
||||||
$this->assertInstanceOf(ImageInterface::class, $image);
|
$this->assertInstanceOf(ImageInterface::class, $image);
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user