-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Expand file tree
/
Copy pathImageManager.php
More file actions
143 lines (127 loc) · 4.11 KB
/
ImageManager.php
File metadata and controls
143 lines (127 loc) · 4.11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
<?php
declare(strict_types=1);
namespace Intervention\Image;
use Intervention\Image\Interfaces\DriverInterface;
use Intervention\Image\Interfaces\ImageInterface;
use Intervention\Image\Drivers\Gd\Driver as GdDriver;
use Intervention\Image\Drivers\Imagick\Driver as ImagickDriver;
use Intervention\Image\Exceptions\DriverException;
use Intervention\Image\Exceptions\InputException;
use Intervention\Image\Interfaces\DecoderInterface;
use Intervention\Image\Interfaces\ImageManagerInterface;
final class ImageManager implements ImageManagerInterface
{
private DriverInterface $driver;
/**
* @link https://image.intervention.io/v3/basics/configuration-drivers#create-a-new-image-manager-instance
*
* @throws DriverException
* @throws InputException
*/
public function __construct(string|DriverInterface $driver, mixed ...$options)
{
$this->driver = $this->resolveDriver($driver, ...$options);
}
/**
* Create image manager with given driver
*
* @link https://image.intervention.io/v3/basics/configuration-drivers#static-constructor
*
* @throws DriverException
* @throws InputException
*/
public static function withDriver(string|DriverInterface $driver, mixed ...$options): self
{
return new self(self::resolveDriver($driver, ...$options));
}
/**
* Create image manager with GD driver
*
* @link https://image.intervention.io/v3/basics/configuration-drivers#static-gd-driver-constructor
*
* @throws DriverException
* @throws InputException
*/
public static function gd(mixed ...$options): self
{
return self::withDriver(new GdDriver(), ...$options);
}
/**
* Create image manager with Imagick driver
*
* @link https://image.intervention.io/v3/basics/configuration-drivers#static-imagick-driver-constructor
*
* @throws DriverException
* @throws InputException
*/
public static function imagick(mixed ...$options): self
{
return self::withDriver(new ImagickDriver(), ...$options);
}
/**
* {@inheritdoc}
*
* @see ImageManagerInterface::create()
*/
public function create(int $width, int $height): ImageInterface
{
return $this->driver->createImage($width, $height);
}
/**
* {@inheritdoc}
*
* @see ImageManagerInterface::read()
*/
public function read(mixed $input, string|array|DecoderInterface $decoders = []): ImageInterface
{
return $this->driver->handleInput(
$input,
match (true) {
is_string($decoders), is_a($decoders, DecoderInterface::class) => [$decoders],
default => $decoders,
}
);
}
/**
* {@inheritdoc}
*
* @see ImageManagerInterface::animate()
*/
public function animate(callable $init): ImageInterface
{
return $this->driver->createAnimation($init);
}
/**
* {@inheritdoc}
*
* @see ImageManagerInterface::driver()
*/
public function driver(): DriverInterface
{
return $this->driver;
}
/**
* Return driver object from given input which might be driver classname or instance of DriverInterface
*
* @throws DriverException
* @throws InputException
*/
private static function resolveDriver(string|DriverInterface $driver, mixed ...$options): DriverInterface
{
$driver = match (true) {
$driver instanceof DriverInterface => $driver,
class_exists($driver) => new $driver(),
default => throw new DriverException(
'Unable to resolve driver. Argment must be either an instance of ' .
DriverInterface::class . '::class or a qualified namespaced name of the driver class.',
),
};
if (!$driver instanceof DriverInterface) {
throw new DriverException(
'Unable to resolve driver. Driver object must implement ' . DriverInterface::class . '.',
);
}
$driver->config()->setOptions(...$options);
return $driver;
}
}