Completed
Pull Request — master (#292)
by Pascal
04:22 queued 03:11
created

Crop   A

Complexity

Total Complexity 17

Size/Duplication

Total Lines 75
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 17
lcom 1
cbo 2
dl 0
loc 75
ccs 34
cts 34
cp 1
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A run() 0 17 2
C getCoordinates() 0 25 12
A limitToImageBoundaries() 0 12 3
1
<?php
2
3
namespace League\Glide\Manipulators;
4
5
use Intervention\Image\Image;
6
7
/**
8
 * @property string $crop
9
 */
10
class Crop extends BaseManipulator
11
{
12
    /**
13
     * Perform crop image manipulation.
14
     * @param  Image $image The source image.
15
     * @return Image The manipulated image.
16
     */
17 3
    public function run(Image $image)
18
    {
19 3
        $coordinates = $this->getCoordinates($image);
20
21 3
        if ($coordinates) {
22 3
            $coordinates = $this->limitToImageBoundaries($image, $coordinates);
23
24 3
            $image->crop(
25 3
                $coordinates[0],
26 3
                $coordinates[1],
27 3
                $coordinates[2],
28 3
                $coordinates[3]
29
            );
30
        }
31
32 3
        return $image;
33
    }
34
35
    /**
36
     * Resolve coordinates.
37
     * @param  Image $image The source image.
38
     * @return int[] The resolved coordinates.
39
     */
40 6
    public function getCoordinates(Image $image)
41
    {
42 6
        $coordinates = explode(',', $this->crop);
43
44 6
        if (count($coordinates) !== 4 or
45 6
            (!is_numeric($coordinates[0])) or
46 6
            (!is_numeric($coordinates[1])) or
47 6
            (!is_numeric($coordinates[2])) or
48 6
            (!is_numeric($coordinates[3])) or
49 6
            ($coordinates[0] <= 0) or
50 6
            ($coordinates[1] <= 0) or
51 6
            ($coordinates[2] < 0) or
52 6
            ($coordinates[3] < 0) or
53 6
            ($coordinates[2] >= $image->width()) or
54 6
            ($coordinates[3] >= $image->height())) {
55 3
            return;
56
        }
57
58
        return [
59 6
            (int) $coordinates[0],
60 6
            (int) $coordinates[1],
61 6
            (int) $coordinates[2],
62 6
            (int) $coordinates[3],
63
        ];
64
    }
65
66
    /**
67
     * Limit coordinates to image boundaries.
68
     * @param  Image $image       The source image.
69
     * @param  int[] $coordinates The coordinates.
70
     * @return int[] The limited coordinates.
71
     */
72 6
    public function limitToImageBoundaries(Image $image, array $coordinates)
73
    {
74 6
        if ($coordinates[0] > ($image->width() - $coordinates[2])) {
75 3
            $coordinates[0] = $image->width() - $coordinates[2];
76
        }
77
78 6
        if ($coordinates[1] > ($image->height() - $coordinates[3])) {
79 3
            $coordinates[1] = $image->height() - $coordinates[3];
80
        }
81
82 6
        return $coordinates;
83
    }
84
}
85