Crop   A
last analyzed

Coupling/Cohesion

Components 1
Dependencies 2

Complexity

Total Complexity 17

Size/Duplication

Total Lines 75
Duplicated Lines 0 %

Test Coverage

Coverage 82.98%

Importance

Changes 0
Metric Value
wmc 17
lcom 1
cbo 2
dl 0
loc 75
ccs 39
cts 47
cp 0.8298
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 1
    public function run(Image $image)
18
    {
19 1
        $coordinates = $this->getCoordinates($image);
20
21 1
        if ($coordinates) {
22 1
            $coordinates = $this->limitToImageBoundaries($image, $coordinates);
23
24 1
            $image->crop(
25 1
                $coordinates[0],
26 1
                $coordinates[1],
27 1
                $coordinates[2],
28 1
                $coordinates[3]
29 1
            );
30 1
        }
31
32 1
        return $image;
33
    }
34
35
    /**
36
     * Resolve coordinates.
37
     * @param  Image $image The source image.
38
     * @return int[] The resolved coordinates.
39
     */
40 2
    public function getCoordinates(Image $image)
41
    {
42 2
        $coordinates = explode(',', $this->crop);
43
44 2
        if (count($coordinates) !== 4 or
45 2
            (!is_numeric($coordinates[0])) or
46 2
            (!is_numeric($coordinates[1])) or
47 2
            (!is_numeric($coordinates[2])) or
48 2
            (!is_numeric($coordinates[3])) or
49 2
            ($coordinates[0] <= 0) or
50 2
            ($coordinates[1] <= 0) or
51 2
            ($coordinates[2] < 0) or
52 2
            ($coordinates[3] < 0) or
53 2
            ($coordinates[2] >= $image->width()) or
54 2
            ($coordinates[3] >= $image->height())) {
55 1
            return;
56
        }
57
58
        return [
59 2
            (int) $coordinates[0],
60 2
            (int) $coordinates[1],
61 2
            (int) $coordinates[2],
62 2
            (int) $coordinates[3],
63 2
        ];
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 2
    public function limitToImageBoundaries(Image $image, array $coordinates)
73
    {
74 2
        if ($coordinates[0] > ($image->width() - $coordinates[2])) {
75 1
            $coordinates[0] = $image->width() - $coordinates[2];
76 1
        }
77
78 2
        if ($coordinates[1] > ($image->height() - $coordinates[3])) {
79 1
            $coordinates[1] = $image->height() - $coordinates[3];
80 1
        }
81
82 2
        return $coordinates;
83
    }
84
}
85