Test Failed
Branch develop (c705b4)
by Florian
03:13
created

ImageVariant::fit()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 16
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 7
c 0
b 0
f 0
nc 1
nop 5
dl 0
loc 16
rs 10
1
<?php
2
3
/**
4
 * Copyright (c) Florian Krämer (https://florian-kraemer.net)
5
 * Licensed under The MIT License
6
 * For full copyright and license information, please see the LICENSE.txt
7
 * Redistributions of files must retain the above copyright notice.
8
 *
9
 * @copyright Copyright (c) Florian Krämer (https://florian-kraemer.net)
10
 * @author    Florian Krämer
11
 * @link      https://github.com/Phauthentic
12
 * @license   https://opensource.org/licenses/MIT MIT License
13
 */
14
15
declare(strict_types=1);
16
17
namespace Phauthentic\Infrastructure\Storage\Processor\Image;
18
19
use InvalidArgumentException;
20
use Phauthentic\Infrastructure\Storage\Processor\Variant;
21
22
/**
23
 * Image Manipulation
24
 */
25
class ImageVariant extends Variant
26
{
27
    protected string $name;
28
    protected array $operations;
29
    protected string $path = '';
30
    protected bool $optimize = false;
31
    protected string $url = '';
32
33
    public const FLIP_HORIZONTAL = 'h';
34
    public const FLIP_VERTICAL = 'v';
35
36
    /**
37
     * @param string $name Name
38
     * @return self
39
     */
40
    public static function create(string $name): self
41
    {
42
        $self = new self();
43
        $self->name = $name;
44
45
        return $self;
46
    }
47
48
    /**
49
     * Try to apply image optimizations if available on the system
50
     *
51
     * @return $this
52
     */
53
    public function optimize(): self
54
    {
55
        $this->optimize = true;
56
57
        return $this;
58
    }
59
60
    /**
61
     * @param int $height Width
62
     * @param int|null $width Height
63
     * @param int|null $x X
64
     * @param int|null $y Y
65
     * @return $this
66
     */
67
    public function crop(int $height, ?int $width = null, ?int $x = null, ?int $y = null): self
68
    {
69
        $this->operations['crop'] = [
70
            'width' => $width,
71
            'height' => $height,
72
            'x' => $x,
73
            'y' => $y
74
        ];
75
76
        return $this;
77
    }
78
79
    /**
80
     * @param int $amount Angle
81
     * @return $this
82
     */
83
    public function sharpen(int $amount): self
84
    {
85
        $this->operations['sharpen'] = [
86
            'amount' => $amount,
87
        ];
88
89
        return $this;
90
    }
91
92
    /**
93
     * @param int $angle Angle
94
     * @return $this
95
     */
96
    public function rotate(int $angle): self
97
    {
98
        $this->operations['rotate'] = [
99
            'angle' => $angle,
100
        ];
101
102
        return $this;
103
    }
104
105
    /**
106
     * @param int $height Height
107
     * @param boolean $preventUpscale Prevent Upscaling
108
     * @return $this
109
     */
110
    public function heighten(int $height, bool $preventUpscale = false): self
111
    {
112
        $this->operations['heighten'] = [
113
            'height' => $height,
114
            'preventUpscale' => $preventUpscale
115
        ];
116
117
        return $this;
118
    }
119
120
    /**
121
     * @param int $width Width
122
     * @param boolean $preventUpscale Prevent Upscaling
123
     * @return $this
124
     */
125
    public function widen(int $width, bool $preventUpscale = false): self
126
    {
127
        $this->operations['widen'] = [
128
            'width' => $width,
129
            'preventUpscale' => $preventUpscale
130
        ];
131
132
        return $this;
133
    }
134
135
    /**
136
     * @param int $width Width
137
     * @param int $height Height
138
     * @param bool $aspectRatio Keeps the aspect ratio
139
     * @param bool $preventUpscale Prevents upscaling
140
     * @return $this
141
     */
142
    public function resize(int $width, int $height, bool $aspectRatio = true, bool $preventUpscale = false): self
143
    {
144
        $this->operations['resize'] = [
145
            'width' => $width,
146
            'height' => $height,
147
            'aspectRatio' => $aspectRatio,
148
            'preventUpscale' => $preventUpscale
149
        ];
150
151
        return $this;
152
    }
153
154
    /**
155
     * Flips the image horizontal
156
     *
157
     * @return $this
158
     */
159
    public function flipHorizontal(): self
160
    {
161
        $this->operations['flipHorizontal'] = [
162
            'direction' => self::FLIP_HORIZONTAL
163
        ];
164
165
        return $this;
166
    }
167
168
    /**
169
     * Flips the image vertical
170
     *
171
     * @return $this
172
     */
173
    public function flipVertical(): self
174
    {
175
        $this->operations['flipVertical'] = [
176
            'direction' => self::FLIP_VERTICAL
177
        ];
178
179
        return $this;
180
    }
181
182
    /**
183
     * Flips the image
184
     *
185
     * @param string $direction Direction, h or v
186
     * @return $this
187
     */
188
    public function flip(string $direction): self
189
    {
190
        if ($direction !== 'h' && $direction !== 'v') {
191
            throw new InvalidArgumentException(sprintf(
192
                '`%s` is invalid, provide `h` or `v`',
193
                $direction
194
            ));
195
        }
196
197
        $this->operations['flip'] = [
198
            'direction' => $direction
199
        ];
200
201
        return $this;
202
    }
203
204
    /**
205
     * Allows the declaration of a callable that gets the image manager instance
206
     * and the arguments passed to it.
207
     *
208
     * @param callable $callback callback
209
     * @return $this
210
     */
211
    public function callback(callable $callback): self
212
    {
213
        $this->operations['callback'] = [
214
            'callback' => $callback
215
        ];
216
217
        return $this;
218
    }
219
220
    /**
221
     * @link http://image.intervention.io/api/fit
222
     * @param int $width Width
223
     * @param int|null $height Height
224
     * @param callable|null $callback Callback
225
     * @param bool $preventUpscale Prevent Upscaling
226
     * @param string $position Position
227
     * @return $this
228
     */
229
    public function fit(
230
        int $width,
231
        ?int $height = null,
232
        ?callable $callback = null,
233
        bool $preventUpscale = false,
234
        string $position = 'center'
235
    ): self {
236
        $this->operations['fit'] = [
237
            'width' => $width,
238
            'height' => $height,
239
            'callback' => $callback,
240
            'preventUpscale' => $preventUpscale,
241
            'position' => $position
242
        ];
243
244
        return $this;
245
    }
246
247
    /**
248
     * @return array
249
     */
250
    public function toArray(): array
251
    {
252
        return [
253
            'operations' => $this->operations,
254
            'path' => $this->path,
255
            'url' => $this->url,
256
            'optimize' => $this->optimize,
257
        ];
258
    }
259
}
260