Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
15 / 15
100.00% covered (success)
100.00%
2 / 2
CRAP
100.00% covered (success)
100.00%
1 / 1
Crop
100.00% covered (success)
100.00%
15 / 15
100.00% covered (success)
100.00%
2 / 2
4
100.00% covered (success)
100.00%
1 / 1
 __construct
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
1
 perform
100.00% covered (success)
100.00%
11 / 11
100.00% covered (success)
100.00%
1 / 1
3
1<?php
2
3/**
4 * Karla ImageMagick wrapper library
5 *
6 * PHP Version 8.0<
7 *
8 * @category Utility
9 * @author   Johannes Skov Frandsen <jsf@greenoak.dk>
10 * @license  http://www.opensource.org/licenses/mit-license.php MIT
11 * @link     https://github.com/localgod/karla Karla
12 * @since    2013-05-26
13 */
14
15declare(strict_types=1);
16
17namespace Karla\Action;
18
19use Karla\Query;
20use Karla\Action;
21
22/**
23 * Class for handling crop action
24 *
25 * @category Utility
26 * @author   Johannes Skov Frandsen <jsf@greenoak.dk>
27 * @license  http://www.opensource.org/licenses/mit-license.php MIT
28 * @link     https://github.com/localgod/karla Karla
29 */
30class Crop implements Action
31{
32    /**
33     * Width
34     *
35     * @var int
36     */
37    private int|null $width;
38
39    /**
40     * Height
41     *
42     * @var int
43     */
44    private int|null $height;
45
46    /**
47     * X offset
48     *
49     * @var int
50     */
51    private int $xOffset;
52
53    /**
54     * Y offset
55     *
56     * @var int
57     */
58    private int $yOffset;
59
60    /**
61     * Construct a new crop action
62     *
63     * @param int $width Image width
64     * @param int $height Image height
65     * @param int $xOffset X offset from upper-left corner
66     * @param int $yOffset Y offset from upper-left corner
67     *
68     * @throws \InvalidArgumentException
69     */
70    public function __construct(int|null $width, int|null $height, int $xOffset, int $yOffset)
71    {
72        $this->width = $width;
73        $this->height = $height;
74        $this->xOffset = $xOffset;
75        $this->yOffset = $yOffset;
76    }
77
78    /**
79     * (non-PHPdoc)
80     *
81     * @param Query $query The query to add the action to
82     *
83     * @see Action::perform()
84     */
85    public function perform(Query $query): Query
86    {
87        $query->notWith('crop', Query::ARGUMENT_TYPE_INPUT);
88
89        $option = " -crop " . $this->width . "x" . $this->height;
90
91        if ($this->xOffset >= 0) {
92            $option = $option . "+" . $this->xOffset;
93        } else {
94            $option = $option . $this->xOffset;
95        }
96
97        if ($this->yOffset >= 0) {
98            $option = $option . "+" . $this->yOffset;
99        } else {
100            $option = $option . $this->yOffset;
101        }
102
103        // http://www.imagemagick.org/Usage/crop/#crop_repage
104        $option = $option . " +repage ";
105        $query->setInputOption($option);
106
107        return $query;
108    }
109}