GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Pull Request — master (#10)
by
unknown
07:08 queued 04:04
created

Manipulations::gamma()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 8
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 4
nc 2
nop 1
1
<?php
2
3
namespace Spatie\Image;
4
5
use ReflectionClass;
6
use League\Flysystem\FileNotFoundException;
7
use Spatie\Image\Exceptions\InvalidManipulation;
8
9
class Manipulations
10
{
11
    const CROP_TOP_LEFT = 'crop-top-left';
12
    const CROP_TOP = 'crop-top';
13
    const CROP_TOP_RIGHT = 'crop-top-right';
14
    const CROP_LEFT = 'crop-left';
15
    const CROP_CENTER = 'crop-center';
16
    const CROP_RIGHT = 'crop-right';
17
    const CROP_BOTTOM_LEFT = 'crop-bottom-left';
18
    const CROP_BOTTOM = 'crop-bottom';
19
    const CROP_BOTTOM_RIGHT = 'crop-bottom-right';
20
21
    const ORIENTATION_AUTO = 'auto';
22
    const ORIENTATION_90 = 90;
23
    const ORIENTATION_180 = 180;
24
    const ORIENTATION_270 = 270;
25
26
    const FIT_CONTAIN = 'contain';
27
    const FIT_MAX = 'max';
28
    const FIT_FILL = 'fill';
29
    const FIT_STRETCH = 'stretch';
30
    const FIT_CROP = 'crop';
31
32
    const BORDER_OVERLAY = 'overlay';
33
    const BORDER_SHRINK = 'shrink';
34
    const BORDER_EXPAND = 'expand';
35
36
    const FORMAT_JPG = 'jpg';
37
    const FORMAT_PJPG = 'pjpg';
38
    const FORMAT_PNG = 'png';
39
    const FORMAT_GIF = 'gif';
40
41
    const FILTER_GREYSCALE = 'greyscale';
42
    const FILTER_SEPIA = 'sepia';
43
44
    const UNIT_PIXELS = 'px';
45
    const UNIT_PERCENT = '%';
46
47
    const POSITION_TOP_LEFT = 'top-left';
48
    const POSITION_TOP = 'top';
49
    const POSITION_TOP_RIGHT = 'top-right';
50
    const POSITION_LEFT = 'left';
51
    const POSITION_CENTER = 'center';
52
    const POSITION_RIGHT = 'right';
53
    const POSITION_BOTTOM_LEFT = 'bottom-left';
54
    const POSITION_BOTTOM = 'bottom';
55
    const POSITION_BOTTOM_RIGHT = 'bottom-right';
56
57
    /** @var \Spatie\Image\ManipulationSequence */
58
    protected $manipulationSequence;
59
60
    public function __construct(array $manipulations = [])
61
    {
62
        $this->manipulationSequence = new ManipulationSequence($manipulations);
63
    }
64
65
    /**
66
     * @param string $orientation
67
     *
68
     * @return $this
69
     *
70
     * @throws InvalidManipulation
71
     */
72
    public function orientation(string $orientation)
73
    {
74
        if (! $this->validateManipulation($orientation, 'orientation')) {
75
            throw InvalidManipulation::invalidParameter(
76
                'orientation',
77
                $orientation,
78
                $this->getValidManipulationOptions('orientation')
79
            );
80
        }
81
82
        return $this->addManipulation('orientation', $orientation);
83
    }
84
85
    /**
86
     * @param string $cropMethod
87
     * @param int $width
88
     * @param int $height
89
     *
90
     * @return $this
91
     *
92
     * @throws InvalidManipulation
93
     */
94
    public function crop(string $cropMethod, int $width, int $height)
95
    {
96
        if (! $this->validateManipulation($cropMethod, 'crop')) {
97
            throw InvalidManipulation::invalidParameter(
98
                'cropmethod',
99
                $cropMethod,
100
                $this->getValidManipulationOptions('crop')
101
            );
102
        }
103
104
        $this->width($width);
105
        $this->height($height);
106
107
        return $this->addManipulation('crop', $cropMethod);
108
    }
109
110
    /**
111
     * @param int $width
112
     * @param int $height
113
     * @param int $focalX Crop center X in percent
114
     * @param int $focalY Crop center Y in percent
115
     *
116
     * @return $this
117
     */
118
    public function focalCrop(int $width, int $height, int $focalX, int $focalY)
119
    {
120
        $this->width($width);
121
        $this->height($height);
122
123
        return $this->addManipulation('crop', "crop-{$focalX}-{$focalY}");
124
    }
125
126
    /**
127
     * @param int $width
128
     * @param int $height
129
     * @param int $x
130
     * @param int $y
131
     *
132
     * @return $this
133
     *
134
     * @throws InvalidManipulation
135
     */
136
    public function manualCrop(int $width, int $height, int $x, int $y)
137
    {
138
        if ($width < 0) {
139
            throw InvalidManipulation::invalidWidth($width);
140
        }
141
142
        if ($height < 0) {
143
            throw InvalidManipulation::invalidWidth($height);
144
        }
145
146
        return $this->addManipulation('manualCrop', "{$width},{$height},{$x},{$y}");
147
    }
148
149
    /**
150
     * @param int $width
151
     *
152
     * @return $this
153
     *
154
     * @throws InvalidManipulation
155
     */
156
    public function width(int $width)
157
    {
158
        if ($width < 0) {
159
            throw InvalidManipulation::invalidWidth($width);
160
        }
161
162
        return $this->addManipulation('width', $width);
163
    }
164
165
    /**
166
     * @param int $height
167
     *
168
     * @return $this
169
     *
170
     * @throws InvalidManipulation
171
     */
172
    public function height(int $height)
173
    {
174
        if ($height < 0) {
175
            throw InvalidManipulation::invalidWidth($height);
176
        }
177
178
        return $this->addManipulation('height', $height);
179
    }
180
181
    /**
182
     * @param string $fitMethod
183
     * @param int $width
184
     * @param int $height
185
     *
186
     * @return $this
187
     *
188
     * @throws InvalidManipulation
189
     */
190
    public function fit(string $fitMethod, int $width, int $height)
191
    {
192
        if (! $this->validateManipulation($fitMethod, 'fit')) {
193
            throw InvalidManipulation::invalidParameter(
194
                'fit',
195
                $fitMethod,
196
                $this->getValidManipulationOptions('fit')
197
            );
198
        }
199
200
        $this->width($width);
201
        $this->height($height);
202
203
        return $this->addManipulation('fit', $fitMethod);
204
    }
205
206
    /**
207
     * @param int $ratio A value between 1 and 8
208
     *
209
     * @return $this
210
     *
211
     * @throws InvalidManipulation
212
     */
213
    public function devicePixelRatio(int $ratio)
214
    {
215
        if ($ratio < 1 || $ratio > 8) {
216
            throw InvalidManipulation::valueNotInRange('ratio', $ratio, 1, 8);
217
        }
218
219
        return $this->addManipulation('devicePixelRatio', $ratio);
220
    }
221
222
    /**
223
     * @param int $brightness A value between -100 and 100
224
     *
225
     * @return $this
226
     *
227
     * @throws InvalidManipulation
228
     */
229
    public function brightness(int $brightness)
230
    {
231
        if ($brightness < -100 || $brightness > 100) {
232
            throw InvalidManipulation::valueNotInRange('brightness', $brightness, -100, 100);
233
        }
234
235
        return $this->addManipulation('brightness', $brightness);
236
    }
237
238
    /**
239
     * @param float $gamma A value between 0.01 and 9.99
240
     *
241
     * @return $this
242
     *
243
     * @throws InvalidManipulation
244
     */
245
    public function gamma(float $gamma)
246
    {
247
        if ($gamma < 0.01 || $gamma > 9.99) {
248
            throw InvalidManipulation::valueNotInRange('gamma', $gamma, 0.01, 9.00);
249
        }
250
251
        return $this->addManipulation('gamma', $gamma);
252
    }
253
254
    /**
255
     * @param int $contrast A value between -100 and 100
256
     *
257
     * @return $this
258
     *
259
     * @throws InvalidManipulation
260
     */
261
    public function contrast(int $contrast)
262
    {
263
        if ($contrast < -100 || $contrast > 100) {
264
            throw InvalidManipulation::valueNotInRange('contrast', $contrast, -100, 100);
265
        }
266
267
        return $this->addManipulation('contrast', $contrast);
268
    }
269
270
    /**
271
     * @param int $sharpen A value between 0 and 100
272
     *
273
     * @return $this
274
     *
275
     * @throws InvalidManipulation
276
     */
277
    public function sharpen(int $sharpen)
278
    {
279
        if ($sharpen < 0 || $sharpen > 100) {
280
            throw InvalidManipulation::valueNotInRange('sharpen', $sharpen, 0, 100);
281
        }
282
283
        return $this->addManipulation('sharpen', $sharpen);
284
    }
285
286
    /**
287
     * @param int $blur A value between 0 and 100
288
     *
289
     * @return $this
290
     *
291
     * @throws InvalidManipulation
292
     */
293
    public function blur(int $blur)
294
    {
295
        if ($blur < 0 || $blur > 100) {
296
            throw InvalidManipulation::valueNotInRange('blur', $blur, 0, 100);
297
        }
298
299
        return $this->addManipulation('blur', $blur);
300
    }
301
302
    /**
303
     * @param int $pixelate A value between 0 and 1000
304
     *
305
     * @return $this
306
     *
307
     * @throws InvalidManipulation
308
     */
309
    public function pixelate(int $pixelate)
310
    {
311
        if ($pixelate < 0 || $pixelate > 1000) {
312
            throw InvalidManipulation::valueNotInRange('pixelate', $pixelate, 0, 1000);
313
        }
314
315
        return $this->addManipulation('pixelate', $pixelate);
316
    }
317
318
    /**
319
     * @return $this
320
     */
321
    public function greyscale()
322
    {
323
        return $this->filter('greyscale');
324
    }
325
326
    /**
327
     * @return $this
328
     */
329
    public function sepia()
330
    {
331
        return $this->filter('sepia');
332
    }
333
334
    /**
335
     * @param string $colorName
336
     *
337
     * @return $this
338
     */
339
    public function background(string $colorName)
340
    {
341
        return $this->addManipulation('background', $colorName);
342
    }
343
344
    /**
345
     * @param int $width
346
     * @param string $color
347
     * @param string $borderType
348
     *
349
     * @return $this
350
     *
351
     * @throws InvalidManipulation
352
     */
353
    public function border(int $width, string $color, string $borderType = 'overlay')
354
    {
355
        if ($width < 0) {
356
            throw InvalidManipulation::invalidWidth($width);
357
        }
358
359
        if (! $this->validateManipulation($borderType, 'border')) {
360
            throw InvalidManipulation::invalidParameter(
361
                'border',
362
                $borderType,
363
                $this->getValidManipulationOptions('border')
364
            );
365
        }
366
367
        return $this->addManipulation('border', "{$width},{$color},{$borderType}");
368
    }
369
370
    /**
371
     * @param int $quality
372
     *
373
     * @return $this
374
     *
375
     * @throws InvalidManipulation
376
     */
377
    public function quality(int $quality)
378
    {
379
        if ($quality < 0 || $quality > 100) {
380
            throw InvalidManipulation::valueNotInRange('quality', $quality, 0, 100);
381
        }
382
383
        return $this->addManipulation('quality', $quality);
384
    }
385
386
    /**
387
     * @param string $format
388
     *
389
     * @return $this
390
     *
391
     * @throws InvalidManipulation
392
     */
393
    public function format(string $format)
394
    {
395
        if (! $this->validateManipulation($format, 'format')) {
396
            throw InvalidManipulation::invalidParameter(
397
                'format',
398
                $format,
399
                $this->getValidManipulationOptions('format')
400
            );
401
        }
402
403
        return $this->addManipulation('format', $format);
404
    }
405
406
    /**
407
     * @param string $filterName
408
     *
409
     * @return $this
410
     *
411
     * @throws InvalidManipulation
412
     */
413
    protected function filter(string $filterName)
414
    {
415
        if (! $this->validateManipulation($filterName, 'filter')) {
416
            throw InvalidManipulation::invalidParameter(
417
                'filter',
418
                $filterName,
419
                $this->getValidManipulationOptions('filter')
420
            );
421
        }
422
423
        return $this->addManipulation('filter', $filterName);
424
    }
425
426
    /**
427
     * @param string $filePath
428
     *
429
     * @return $this
430
     *
431
     * @throws FileNotFoundException
432
     */
433
    public function watermark(string $filePath)
434
    {
435
        if (! file_exists($filePath)) {
436
            throw new FileNotFoundException($filePath);
437
        }
438
439
        $this->addManipulation('watermark', $filePath);
440
441
        return $this;
442
    }
443
444
    /**
445
     * @param int    $width The width of the watermark in pixels (default) or percent.
446
     * @param string $unit  The unit of the `$width` parameter. Use `Manipulations::UNIT_PERCENT` or `Manipulations::UNIT_PIXELS`.
447
     *
448
     * @return $this
449
     */
450
    public function watermarkWidth(int $width, string $unit = 'px')
451
    {
452
        $width = ($unit == static::UNIT_PERCENT ? $width.'w' : $width);
453
454
        return $this->addManipulation('watermarkWidth', $width);
455
    }
456
457
    /**
458
     * @param int    $height The height of the watermark in pixels (default) or percent.
459
     * @param string $unit   The unit of the `$height` parameter. Use `Manipulations::UNIT_PERCENT` or `Manipulations::UNIT_PIXELS`.
460
     *
461
     * @return $this
462
     */
463
    public function watermarkHeight(int $height, string $unit = 'px')
464
    {
465
        $height = ($unit == static::UNIT_PERCENT ? $height.'h' : $height);
466
467
        return $this->addManipulation('watermarkHeight', $height);
468
    }
469
470
    /**
471
     * @param string $fitMethod How is the watermark fitted into the watermarkWidth and watermarkHeight properties.
472
     *
473
     * @return $this
474
     *
475
     * @throws InvalidManipulation
476
     */
477
    public function watermarkFit(string $fitMethod)
478
    {
479
        if (! $this->validateManipulation($fitMethod, 'fit')) {
480
            throw InvalidManipulation::invalidParameter(
481
                'watermarkFit',
482
                $fitMethod,
483
                $this->getValidManipulationOptions('fit')
484
            );
485
        }
486
487
        return $this->addManipulation('watermarkFit', $fitMethod);
488
    }
489
490
    /**
491
     * @param int $xPadding         How far is the watermark placed from the top and bottom edges of the image.
492
     * @param int|null $yPadding    How far is the watermark placed from the left and right edges of the image.
493
     * @param string $unit          Unit of the padding values. Use `Manipulations::UNIT_PERCENT` or `Manipulations::UNIT_PIXELS`.
494
     *
495
     * @return $this
496
     */
497
    public function watermarkPadding(int $xPadding, ?int $yPadding = null, string $unit = 'px')
498
    {
499
        $yPadding = $yPadding ?? $xPadding;
500
501
        $xPadding = ($unit == static::UNIT_PERCENT ? $xPadding.'h' : $xPadding);
502
        $yPadding = ($unit == static::UNIT_PERCENT ? $yPadding.'h' : $yPadding);
503
504
        $this->addManipulation('watermarkPaddingX', $xPadding);
505
        $this->addManipulation('watermarkPaddingY', $yPadding);
506
507
        return $this;
508
    }
509
510
    /**
511
     * @param string $position
512
     *
513
     * @return $this
514
     *
515
     * @throws InvalidManipulation
516
     */
517
    public function watermarkPosition(string $position)
518
    {
519
        if (! $this->validateManipulation($position, 'position')) {
520
            throw InvalidManipulation::invalidParameter(
521
                'watermarkPosition',
522
                $position,
523
                $this->getValidManipulationOptions('position')
524
            );
525
        }
526
527
        return $this->addManipulation('watermarkPosition', $position);
528
    }
529
530
    /**
531
     * Sets the opacity of the watermark. Only works with the `imagick` driver.
532
     *
533
     * @param int $opacity A value between 0 and 100.
534
     *
535
     * @return $this
536
     *
537
     * @throws InvalidManipulation
538
     */
539
    public function watermarkOpacity(int $opacity)
540
    {
541
        if ($opacity < 0 || $opacity > 100) {
542
            throw InvalidManipulation::valueNotInRange('opacity', $opacity, 0, 100);
543
        }
544
545
        return $this->addManipulation('watermarkOpacity', $opacity);
546
    }
547
548
    /**
549
     * @return $this
550
     */
551
    public function apply()
552
    {
553
        $this->manipulationSequence->startNewGroup();
554
555
        return $this;
556
    }
557
558
    public function removeManipulation(string $name)
559
    {
560
        $this->manipulationSequence->removeManipulation($name);
561
    }
562
563
    public function hasManipulation(string $manipulationName): bool
564
    {
565
        return ! is_null($this->getManipulationArgument($manipulationName));
566
    }
567
568
    /**
569
     * @param string $manipulationName
570
     *
571
     * @return string|null
572
     */
573
    public function getManipulationArgument(string $manipulationName)
574
    {
575
        foreach ($this->manipulationSequence->getGroups() as $manipulationSet) {
576
            if (array_key_exists($manipulationName, $manipulationSet)) {
577
                return $manipulationSet[$manipulationName];
578
            }
579
        }
580
    }
581
582
    protected function addManipulation(string $manipulationName, string $manipulationArgument)
583
    {
584
        $this->manipulationSequence->addManipulation($manipulationName, $manipulationArgument);
585
586
        return $this;
587
    }
588
589
    public function mergeManipulations(Manipulations $manipulations)
590
    {
591
        $this->manipulationSequence->merge($manipulations->manipulationSequence);
592
593
        return $this;
594
    }
595
596
    public function getManipulationSequence(): ManipulationSequence
597
    {
598
        return $this->manipulationSequence;
599
    }
600
601
    protected function validateManipulation(string $value, string $constantNamePrefix): bool
602
    {
603
        return in_array($value, $this->getValidManipulationOptions($constantNamePrefix));
604
    }
605
606
    protected function getValidManipulationOptions(string $manipulation): array
607
    {
608
        $options = (new ReflectionClass(static::class))->getConstants();
609
610
        return array_filter($options, function ($value, $name) use ($manipulation) {
611
            return strpos($name, strtoupper($manipulation)) === 0;
612
        }, ARRAY_FILTER_USE_BOTH);
613
    }
614
}
615