-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Expand file tree
/
Copy pathAnimationFactory.php
More file actions
174 lines (151 loc) · 4.61 KB
/
AnimationFactory.php
File metadata and controls
174 lines (151 loc) · 4.61 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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
<?php
declare(strict_types=1);
namespace Intervention\Image;
use Error;
use Intervention\Gif\DisposalMethod;
use Intervention\Image\Exceptions\DecoderException;
use Intervention\Image\Exceptions\FilesystemException;
use Intervention\Image\Interfaces\AnimationFactoryInterface;
use Intervention\Image\Interfaces\DriverInterface;
use Intervention\Image\Interfaces\FrameInterface;
use Intervention\Image\Interfaces\ImageInterface;
class AnimationFactory implements AnimationFactoryInterface
{
/**
* Current frame number.
*/
protected int $currentFrameNumber = 0;
/**
* Image sources of animation frames.
*
* @var array<mixed>
*/
protected array $sources = [];
/**
* Frame delays of animation frames in seconds.
*
* @var array<float>
*/
protected array $delays = [];
/**
* Frame processing call names.
*
* @var array<null|string>
*/
protected array $processingCalls = [];
/**
* Frame processing arguments of calls.
*
* @var array<null|array<mixed>>
*/
protected array $processingArguments = [];
/**
* Create new instance.
*/
public function __construct(
protected int $width,
protected int $height,
null|callable $animation = null,
) {
if (is_callable($animation)) {
$animation($this);
}
}
/**
* {@inheritdoc}
*
* @see AnimationFactoryInterface::build()
*/
public static function build(
int $width,
int $height,
callable $animation,
DriverInterface $driver,
): ImageInterface {
return (new self($width, $height, $animation))->image($driver);
}
/**
* {@inheritdoc}
*
* @see AnimationFactoryInterface::add()
*/
public function add(mixed $source, float $delay = 1): AnimationFactoryInterface
{
$this->currentFrameNumber++;
$this->sources[$this->currentFrameNumber] = $source;
$this->delays[$this->currentFrameNumber] = $delay;
$this->processingCalls[$this->currentFrameNumber] = null;
$this->processingArguments[$this->currentFrameNumber] = null;
return $this;
}
/**
* {@inheritdoc}
*
* @see AnimationFactoryInterface::image()
*/
public function image(DriverInterface $driver): ImageInterface
{
if (count($this->sources) === 0) {
return $driver->createImage($this->width, $this->height);
}
$frames = array_map(
$this->buildFrame(...),
array_fill(0, count($this->sources), $driver),
$this->sources,
$this->delays,
$this->processingCalls,
$this->processingArguments,
);
return new Image($driver, $driver->createCore($frames));
}
/**
* Build frame from given image source and delay.
*
* @param null|array<mixed> $processingArguments
*/
private function buildFrame(
DriverInterface $driver,
mixed $source,
float $delay,
?string $processingCall = null,
?array $processingArguments = null,
): FrameInterface {
try {
// try to decode image source
$image = $driver->decodeImage($source);
} catch (DecoderException | FilesystemException) {
// create empty image with colored background
$image = $driver->createImage($this->width, $this->height)
->fill($driver->decodeColor($source));
}
// adjust size if necessary
if ($image->width() !== $this->width || $image->height() !== $this->height) {
$image->cover($this->width, $this->height);
}
// apply processing call if available
if ($processingCall !== null) {
call_user_func_array([$image, $processingCall], $processingArguments);
}
// return ready-made frame with all attributes
return $image
->core()
->first()
->setDelay($delay)
->setDisposalMethod(DisposalMethod::BACKGROUND->value);
}
/**
* Collect processing calls on frame images.
*
* @param array<null|array<mixed>> $arguments
* @throws Error
*/
public function __call(string $name, array $arguments): self
{
if (!method_exists(Image::class, $name)) {
throw new Error('Call to undefined method ' . Image::class . '::' . $name . '()');
}
$this->processingCalls[$this->currentFrameNumber] = $name;
$this->processingArguments[$this->currentFrameNumber] = $arguments;
return $this;
}
}