Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
77.78% covered (warning)
77.78%
21 / 27
0.00% covered (danger)
0.00%
0 / 2
CRAP
0.00% covered (danger)
0.00%
0 / 1
Resize
77.78% covered (warning)
77.78%
21 / 27
0.00% covered (danger)
0.00%
0 / 2
11.10
0.00% covered (danger)
0.00%
0 / 1
 __construct
63.64% covered (warning)
63.64%
7 / 11
0.00% covered (danger)
0.00%
0 / 1
4.77
 perform
87.50% covered (warning)
87.50%
14 / 16
0.00% covered (danger)
0.00%
0 / 1
6.07
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;
21use InvalidArgumentException;
22
23/**
24 * Class for handling resize action
25 *
26 * @category Utility
27 * @author   Johannes Skov Frandsen <jsf@greenoak.dk>
28 * @license  http://www.opensource.org/licenses/mit-license.php MIT
29 * @link     https://github.com/localgod/karla Karla
30 */
31class Resize implements Action
32{
33    /**
34     * Preserve aspect ratio
35     *
36     * @var string
37     */
38    public const ASPECT_FILL = 'aspect_fill';
39
40    /**
41     * Ignored aspect ratio
42     *
43     * @var string
44     */
45    public const ASPECT_FIT = 'aspect_fit';
46
47    /**
48     * Width of new image
49     *
50     * @var int|null
51     */
52    private int|null $width;
53
54    /**
55     * Height of new image
56     *
57     * @var int|null
58     */
59    private int|null $height;
60
61    /**
62     * Maintain aspect ratio
63     *
64     * @var bool
65     */
66    private bool $maintainAspectRatio;
67
68    /**
69     * Don't scale up
70     *
71     * @var bool
72     */
73    private bool $dontScaleUp;
74
75    /**
76     * Default we ignored aspect ratio
77     * @var string
78     */
79    private string $aspect = self::ASPECT_FIT;
80
81    /**
82     * Construct a new size action
83     *
84     * @param int|null $width Image width
85     * @param int|null $height Image height
86     * @param bool $maintainAspectRatio Should we maintain aspect ratio?
87     * @param bool $dontScaleUp Should we prohibit scaling up?
88     * @param string $aspect How should we handle aspect ratio?
89     *
90     * @throws \InvalidArgumentException
91     */
92    public function __construct(
93        int|null $width,
94        int|null $height,
95        bool $maintainAspectRatio,
96        bool $dontScaleUp,
97        string $aspect = Resize::ASPECT_FIT
98    ) {
99        if ($width === null && $height === null) {
100            $message = 'You must supply height or width or both to resize the image';
101            throw new InvalidArgumentException($message);
102        }
103        if (!in_array($aspect, array(Resize::ASPECT_FIT, Resize::ASPECT_FILL))) {
104            $message = sprintf('aspect must be "%s" or "%s".', Resize::ASPECT_FIT, Resize::ASPECT_FILL);
105            throw new \InvalidArgumentException($message);
106        }
107
108        $this->width = $width;
109        $this->height = $height;
110        $this->maintainAspectRatio = $maintainAspectRatio;
111        $this->dontScaleUp = $dontScaleUp;
112        $this->aspect = $aspect;
113    }
114
115    /**
116     * Perform the resize action
117     *
118     * @param Query $query The query to add the action to
119     *
120     * @see Action::perform()
121     */
122    public function perform(Query $query): Query
123    {
124        $query->notWith('resize', Query::ARGUMENT_TYPE_INPUT);
125        $query->notWith('resample', Query::ARGUMENT_TYPE_INPUT);
126
127        $option = " -resize ";
128
129        if ($this->width != '') {
130            $option .= $this->width;
131        }
132
133        if ($this->height != '') {
134            $option .= "x" . $this->height;
135        }
136
137        if ($this->aspect == Resize::ASPECT_FILL) {
138            $option .= "^";
139        }
140
141        if ($this->dontScaleUp) {
142            $option .= "\>";
143        }
144
145        if (! $this->maintainAspectRatio) {
146            $option .= "!";
147        }
148        $option .= " ";
149        $query->setInputOption($option);
150
151        return $query;
152    }
153}