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
Push — master ( 9b7a02...bb805e )
by Freek
05:21
created

Manipulations::flip()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 12
Code Lines 7

Duplication

Lines 12
Ratio 100 %

Importance

Changes 0
Metric Value
dl 12
loc 12
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 7
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 FLIP_H = 'h';
27
    const FLIP_V = 'v';
28
    const FLIP_BOTH = 'both';
29
30
    const FIT_CONTAIN = 'contain';
31
    const FIT_MAX = 'max';
32
    const FIT_FILL = 'fill';
33
    const FIT_STRETCH = 'stretch';
34
    const FIT_CROP = 'crop';
35
36
    const BORDER_OVERLAY = 'overlay';
37
    const BORDER_SHRINK = 'shrink';
38
    const BORDER_EXPAND = 'expand';
39
40
    const FORMAT_JPG = 'jpg';
41
    const FORMAT_PJPG = 'pjpg';
42
    const FORMAT_PNG = 'png';
43
    const FORMAT_GIF = 'gif';
44
45
    const FILTER_GREYSCALE = 'greyscale';
46
    const FILTER_SEPIA = 'sepia';
47
48
    const UNIT_PIXELS = 'px';
49
    const UNIT_PERCENT = '%';
50
51
    const POSITION_TOP_LEFT = 'top-left';
52
    const POSITION_TOP = 'top';
53
    const POSITION_TOP_RIGHT = 'top-right';
54
    const POSITION_LEFT = 'left';
55
    const POSITION_CENTER = 'center';
56
    const POSITION_RIGHT = 'right';
57
    const POSITION_BOTTOM_LEFT = 'bottom-left';
58
    const POSITION_BOTTOM = 'bottom';
59
    const POSITION_BOTTOM_RIGHT = 'bottom-right';
60
61
    /** @var \Spatie\Image\ManipulationSequence */
62
    protected $manipulationSequence;
63
64
    public function __construct(array $manipulations = [])
65
    {
66
        if (! $this->hasMultipleConversions($manipulations)) {
67
            $manipulations = [$manipulations];
68
        }
69
70
        foreach ($manipulations as $manipulation) {
71
            $this->manipulationSequence = new ManipulationSequence($manipulation);
72
        }
73
    }
74
75
    /**
76
     * @param string $orientation
77
     *
78
     * @return $this
79
     *
80
     * @throws InvalidManipulation
81
     */
82 View Code Duplication
    public function orientation(string $orientation)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
83
    {
84
        if (! $this->validateManipulation($orientation, 'orientation')) {
85
            throw InvalidManipulation::invalidParameter(
86
                'orientation',
87
                $orientation,
88
                $this->getValidManipulationOptions('orientation')
89
            );
90
        }
91
92
        return $this->addManipulation('orientation', $orientation);
93
    }
94
    
95
    /**
96
     * @param string $orientation
97
     *
98
     * @return $this
99
     *
100
     * @throws InvalidManipulation
101
     */
102 View Code Duplication
    public function flip(string $orientation)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
103
    {
104
        if (! $this->validateManipulation($orientation, 'flip')) {
105
            throw InvalidManipulation::invalidParameter(
106
                'flip',
107
                $orientation,
108
                $this->getValidManipulationOptions('flip')
109
            );
110
        }
111
112
        return $this->addManipulation('flip', $orientation);
113
    }
114
115
    /**
116
     * @param string $cropMethod
117
     * @param int $width
118
     * @param int $height
119
     *
120
     * @return $this
121
     *
122
     * @throws InvalidManipulation
123
     */
124 View Code Duplication
    public function crop(string $cropMethod, int $width, int $height)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
125
    {
126
        if (! $this->validateManipulation($cropMethod, 'crop')) {
127
            throw InvalidManipulation::invalidParameter(
128
                'cropmethod',
129
                $cropMethod,
130
                $this->getValidManipulationOptions('crop')
131
            );
132
        }
133
134
        $this->width($width);
135
        $this->height($height);
136
137
        return $this->addManipulation('crop', $cropMethod);
138
    }
139
140
    /**
141
     * @param int $width
142
     * @param int $height
143
     * @param int $focalX Crop center X in percent
144
     * @param int $focalY Crop center Y in percent
145
     *
146
     * @return $this
147
     */
148
    public function focalCrop(int $width, int $height, int $focalX, int $focalY)
149
    {
150
        $this->width($width);
151
        $this->height($height);
152
153
        return $this->addManipulation('crop', "crop-{$focalX}-{$focalY}");
154
    }
155
156
    /**
157
     * @param int $width
158
     * @param int $height
159
     * @param int $x
160
     * @param int $y
161
     *
162
     * @return $this
163
     *
164
     * @throws InvalidManipulation
165
     */
166
    public function manualCrop(int $width, int $height, int $x, int $y)
167
    {
168
        if ($width < 0) {
169
            throw InvalidManipulation::invalidWidth($width);
170
        }
171
172
        if ($height < 0) {
173
            throw InvalidManipulation::invalidWidth($height);
174
        }
175
176
        return $this->addManipulation('manualCrop', "{$width},{$height},{$x},{$y}");
177
    }
178
179
    /**
180
     * @param int $width
181
     *
182
     * @return $this
183
     *
184
     * @throws InvalidManipulation
185
     */
186
    public function width(int $width)
187
    {
188
        if ($width < 0) {
189
            throw InvalidManipulation::invalidWidth($width);
190
        }
191
192
        return $this->addManipulation('width', $width);
193
    }
194
195
    /**
196
     * @param int $height
197
     *
198
     * @return $this
199
     *
200
     * @throws InvalidManipulation
201
     */
202
    public function height(int $height)
203
    {
204
        if ($height < 0) {
205
            throw InvalidManipulation::invalidWidth($height);
206
        }
207
208
        return $this->addManipulation('height', $height);
209
    }
210
211
    /**
212
     * @param string $fitMethod
213
     * @param int $width
214
     * @param int $height
215
     *
216
     * @return $this
217
     *
218
     * @throws InvalidManipulation
219
     */
220 View Code Duplication
    public function fit(string $fitMethod, int $width, int $height)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
221
    {
222
        if (! $this->validateManipulation($fitMethod, 'fit')) {
223
            throw InvalidManipulation::invalidParameter(
224
                'fit',
225
                $fitMethod,
226
                $this->getValidManipulationOptions('fit')
227
            );
228
        }
229
230
        $this->width($width);
231
        $this->height($height);
232
233
        return $this->addManipulation('fit', $fitMethod);
234
    }
235
236
    /**
237
     * @param int $ratio A value between 1 and 8
238
     *
239
     * @return $this
240
     *
241
     * @throws InvalidManipulation
242
     */
243
    public function devicePixelRatio(int $ratio)
244
    {
245
        if ($ratio < 1 || $ratio > 8) {
246
            throw InvalidManipulation::valueNotInRange('ratio', $ratio, 1, 8);
247
        }
248
249
        return $this->addManipulation('devicePixelRatio', $ratio);
250
    }
251
252
    /**
253
     * @param int $brightness A value between -100 and 100
254
     *
255
     * @return $this
256
     *
257
     * @throws InvalidManipulation
258
     */
259
    public function brightness(int $brightness)
260
    {
261
        if ($brightness < -100 || $brightness > 100) {
262
            throw InvalidManipulation::valueNotInRange('brightness', $brightness, -100, 100);
263
        }
264
265
        return $this->addManipulation('brightness', $brightness);
266
    }
267
268
    /**
269
     * @param float $gamma A value between 0.01 and 9.99
270
     *
271
     * @return $this
272
     *
273
     * @throws InvalidManipulation
274
     */
275
    public function gamma(float $gamma)
276
    {
277
        if ($gamma < 0.01 || $gamma > 9.99) {
278
            throw InvalidManipulation::valueNotInRange('gamma', $gamma, 0.01, 9.00);
279
        }
280
281
        return $this->addManipulation('gamma', $gamma);
282
    }
283
284
    /**
285
     * @param int $contrast A value between -100 and 100
286
     *
287
     * @return $this
288
     *
289
     * @throws InvalidManipulation
290
     */
291
    public function contrast(int $contrast)
292
    {
293
        if ($contrast < -100 || $contrast > 100) {
294
            throw InvalidManipulation::valueNotInRange('contrast', $contrast, -100, 100);
295
        }
296
297
        return $this->addManipulation('contrast', $contrast);
298
    }
299
300
    /**
301
     * @param int $sharpen A value between 0 and 100
302
     *
303
     * @return $this
304
     *
305
     * @throws InvalidManipulation
306
     */
307
    public function sharpen(int $sharpen)
308
    {
309
        if ($sharpen < 0 || $sharpen > 100) {
310
            throw InvalidManipulation::valueNotInRange('sharpen', $sharpen, 0, 100);
311
        }
312
313
        return $this->addManipulation('sharpen', $sharpen);
314
    }
315
316
    /**
317
     * @param int $blur A value between 0 and 100
318
     *
319
     * @return $this
320
     *
321
     * @throws InvalidManipulation
322
     */
323 View Code Duplication
    public function blur(int $blur)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
324
    {
325
        if ($blur < 0 || $blur > 100) {
326
            throw InvalidManipulation::valueNotInRange('blur', $blur, 0, 100);
327
        }
328
329
        return $this->addManipulation('blur', $blur);
330
    }
331
332
    /**
333
     * @param int $pixelate A value between 0 and 1000
334
     *
335
     * @return $this
336
     *
337
     * @throws InvalidManipulation
338
     */
339
    public function pixelate(int $pixelate)
340
    {
341
        if ($pixelate < 0 || $pixelate > 1000) {
342
            throw InvalidManipulation::valueNotInRange('pixelate', $pixelate, 0, 1000);
343
        }
344
345
        return $this->addManipulation('pixelate', $pixelate);
346
    }
347
348
    /**
349
     * @return $this
350
     */
351
    public function greyscale()
352
    {
353
        return $this->filter('greyscale');
354
    }
355
356
    /**
357
     * @return $this
358
     */
359
    public function sepia()
360
    {
361
        return $this->filter('sepia');
362
    }
363
364
    /**
365
     * @param string $colorName
366
     *
367
     * @return $this
368
     */
369
    public function background(string $colorName)
370
    {
371
        return $this->addManipulation('background', $colorName);
372
    }
373
374
    /**
375
     * @param int $width
376
     * @param string $color
377
     * @param string $borderType
378
     *
379
     * @return $this
380
     *
381
     * @throws InvalidManipulation
382
     */
383
    public function border(int $width, string $color, string $borderType = 'overlay')
384
    {
385
        if ($width < 0) {
386
            throw InvalidManipulation::invalidWidth($width);
387
        }
388
389
        if (! $this->validateManipulation($borderType, 'border')) {
390
            throw InvalidManipulation::invalidParameter(
391
                'border',
392
                $borderType,
393
                $this->getValidManipulationOptions('border')
394
            );
395
        }
396
397
        return $this->addManipulation('border', "{$width},{$color},{$borderType}");
398
    }
399
400
    /**
401
     * @param int $quality
402
     *
403
     * @return $this
404
     *
405
     * @throws InvalidManipulation
406
     */
407 View Code Duplication
    public function quality(int $quality)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
408
    {
409
        if ($quality < 0 || $quality > 100) {
410
            throw InvalidManipulation::valueNotInRange('quality', $quality, 0, 100);
411
        }
412
413
        return $this->addManipulation('quality', $quality);
414
    }
415
416
    /**
417
     * @param string $format
418
     *
419
     * @return $this
420
     *
421
     * @throws InvalidManipulation
422
     */
423 View Code Duplication
    public function format(string $format)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
424
    {
425
        if (! $this->validateManipulation($format, 'format')) {
426
            throw InvalidManipulation::invalidParameter(
427
                'format',
428
                $format,
429
                $this->getValidManipulationOptions('format')
430
            );
431
        }
432
433
        return $this->addManipulation('format', $format);
434
    }
435
436
    /**
437
     * @param string $filterName
438
     *
439
     * @return $this
440
     *
441
     * @throws InvalidManipulation
442
     */
443 View Code Duplication
    protected function filter(string $filterName)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
444
    {
445
        if (! $this->validateManipulation($filterName, 'filter')) {
446
            throw InvalidManipulation::invalidParameter(
447
                'filter',
448
                $filterName,
449
                $this->getValidManipulationOptions('filter')
450
            );
451
        }
452
453
        return $this->addManipulation('filter', $filterName);
454
    }
455
456
    /**
457
     * @param string $filePath
458
     *
459
     * @return $this
460
     *
461
     * @throws FileNotFoundException
462
     */
463
    public function watermark(string $filePath)
464
    {
465
        if (! file_exists($filePath)) {
466
            throw new FileNotFoundException($filePath);
467
        }
468
469
        $this->addManipulation('watermark', $filePath);
470
471
        return $this;
472
    }
473
474
    /**
475
     * @param int    $width The width of the watermark in pixels (default) or percent.
476
     * @param string $unit  The unit of the `$width` parameter. Use `Manipulations::UNIT_PERCENT` or `Manipulations::UNIT_PIXELS`.
477
     *
478
     * @return $this
479
     */
480
    public function watermarkWidth(int $width, string $unit = 'px')
481
    {
482
        $width = ($unit == static::UNIT_PERCENT ? $width.'w' : $width);
483
484
        return $this->addManipulation('watermarkWidth', $width);
485
    }
486
487
    /**
488
     * @param int    $height The height of the watermark in pixels (default) or percent.
489
     * @param string $unit   The unit of the `$height` parameter. Use `Manipulations::UNIT_PERCENT` or `Manipulations::UNIT_PIXELS`.
490
     *
491
     * @return $this
492
     */
493
    public function watermarkHeight(int $height, string $unit = 'px')
494
    {
495
        $height = ($unit == static::UNIT_PERCENT ? $height.'h' : $height);
496
497
        return $this->addManipulation('watermarkHeight', $height);
498
    }
499
500
    /**
501
     * @param string $fitMethod How is the watermark fitted into the watermarkWidth and watermarkHeight properties.
502
     *
503
     * @return $this
504
     *
505
     * @throws InvalidManipulation
506
     */
507 View Code Duplication
    public function watermarkFit(string $fitMethod)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
508
    {
509
        if (! $this->validateManipulation($fitMethod, 'fit')) {
510
            throw InvalidManipulation::invalidParameter(
511
                'watermarkFit',
512
                $fitMethod,
513
                $this->getValidManipulationOptions('fit')
514
            );
515
        }
516
517
        return $this->addManipulation('watermarkFit', $fitMethod);
518
    }
519
520
    /**
521
     * @param int $xPadding         How far is the watermark placed from the left and right edges of the image.
522
     * @param int|null $yPadding    How far is the watermark placed from the top and bottom edges of the image.
523
     * @param string $unit          Unit of the padding values. Use `Manipulations::UNIT_PERCENT` or `Manipulations::UNIT_PIXELS`.
524
     *
525
     * @return $this
526
     */
527
    public function watermarkPadding(int $xPadding, int $yPadding = null, string $unit = 'px')
528
    {
529
        $yPadding = $yPadding ?? $xPadding;
530
531
        $xPadding = ($unit == static::UNIT_PERCENT ? $xPadding.'w' : $xPadding);
532
        $yPadding = ($unit == static::UNIT_PERCENT ? $yPadding.'h' : $yPadding);
533
534
        $this->addManipulation('watermarkPaddingX', $xPadding);
535
        $this->addManipulation('watermarkPaddingY', $yPadding);
536
537
        return $this;
538
    }
539
540
    /**
541
     * @param string $position
542
     *
543
     * @return $this
544
     *
545
     * @throws InvalidManipulation
546
     */
547 View Code Duplication
    public function watermarkPosition(string $position)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
548
    {
549
        if (! $this->validateManipulation($position, 'position')) {
550
            throw InvalidManipulation::invalidParameter(
551
                'watermarkPosition',
552
                $position,
553
                $this->getValidManipulationOptions('position')
554
            );
555
        }
556
557
        return $this->addManipulation('watermarkPosition', $position);
558
    }
559
560
    /**
561
     * Sets the opacity of the watermark. Only works with the `imagick` driver.
562
     *
563
     * @param int $opacity A value between 0 and 100.
564
     *
565
     * @return $this
566
     *
567
     * @throws InvalidManipulation
568
     */
569
    public function watermarkOpacity(int $opacity)
570
    {
571
        if ($opacity < 0 || $opacity > 100) {
572
            throw InvalidManipulation::valueNotInRange('opacity', $opacity, 0, 100);
573
        }
574
575
        return $this->addManipulation('watermarkOpacity', $opacity);
576
    }
577
578
    /**
579
     * Shave off some kilobytes by optimizing the image.
580
     *
581
     * @param array $optimizationOptions
582
     *
583
     * @return $this
584
     */
585
    public function optimize(array $optimizationOptions = [])
586
    {
587
        return $this->addManipulation('optimize', json_encode($optimizationOptions));
588
    }
589
590
    /**
591
     * @return $this
592
     */
593
    public function apply()
594
    {
595
        $this->manipulationSequence->startNewGroup();
596
597
        return $this;
598
    }
599
600
    /**
601
     * Create new manipulations class.
602
     *
603
     * @param array $manipulations
604
     *
605
     * @return self
606
     */
607
    public static function create(array $manipulations = [])
608
    {
609
        return new self($manipulations);
610
    }
611
612
    public function toArray() : array
613
    {
614
        return $this->manipulationSequence->toArray();
615
    }
616
617
    /**
618
     * Checks if the given manipulations has arrays inside or not.
619
     *
620
     * @param  array $manipulations
621
     *
622
     * @return bool
623
     */
624
    private function hasMultipleConversions(array $manipulations) : bool
625
    {
626
        foreach ($manipulations as $manipulation) {
627
            if (isset($manipulation[0]) && is_array($manipulation[0])) {
628
                return true;
629
            }
630
        }
631
632
        return false;
633
    }
634
635
    public function removeManipulation(string $name)
636
    {
637
        $this->manipulationSequence->removeManipulation($name);
638
    }
639
640
    public function hasManipulation(string $manipulationName): bool
641
    {
642
        return ! is_null($this->getManipulationArgument($manipulationName));
643
    }
644
645
    /**
646
     * @param string $manipulationName
647
     *
648
     * @return string|null
649
     */
650
    public function getManipulationArgument(string $manipulationName)
651
    {
652
        foreach ($this->manipulationSequence->getGroups() as $manipulationSet) {
653
            if (array_key_exists($manipulationName, $manipulationSet)) {
654
                return $manipulationSet[$manipulationName];
655
            }
656
        }
657
    }
658
659
    protected function addManipulation(string $manipulationName, string $manipulationArgument)
660
    {
661
        $this->manipulationSequence->addManipulation($manipulationName, $manipulationArgument);
662
663
        return $this;
664
    }
665
666
    public function mergeManipulations(Manipulations $manipulations)
667
    {
668
        $this->manipulationSequence->merge($manipulations->manipulationSequence);
669
670
        return $this;
671
    }
672
673
    public function getManipulationSequence(): ManipulationSequence
674
    {
675
        return $this->manipulationSequence;
676
    }
677
678
    protected function validateManipulation(string $value, string $constantNamePrefix): bool
679
    {
680
        return in_array($value, $this->getValidManipulationOptions($constantNamePrefix));
681
    }
682
683
    protected function getValidManipulationOptions(string $manipulation): array
684
    {
685
        $options = (new ReflectionClass(static::class))->getConstants();
686
687
        return array_filter($options, function ($value, $name) use ($manipulation) {
688
            return strpos($name, strtoupper($manipulation)) === 0;
689
        }, ARRAY_FILTER_USE_BOTH);
690
    }
691
692
    public function isEmpty(): bool
693
    {
694
        return $this->manipulationSequence->isEmpty();
695
    }
696
697
    /*
698
     * Get the first manipultion with the given name.
699
     *
700
     * @return mixed
701
     */
702
    public function getFirstManipulationArgument(string $manipulationName)
703
    {
704
        return $this->manipulationSequence->getFirstManipulationArgument($manipulationName);
705
    }
706
}
707