Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
69.70% |
23 / 33 |
|
0.00% |
0 / 2 |
CRAP | |
0.00% |
0 / 1 |
Resize | |
69.70% |
23 / 33 |
|
0.00% |
0 / 2 |
19.45 | |
0.00% |
0 / 1 |
__construct | |
52.94% |
9 / 17 |
|
0.00% |
0 / 1 |
14.67 | |||
perform | |
87.50% |
14 / 16 |
|
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 | |
15 | declare(strict_types=1); |
16 | |
17 | namespace Karla\Action; |
18 | |
19 | use Karla\Query; |
20 | use Karla\Action; |
21 | use InvalidArgumentException; |
22 | |
23 | /** |
24 | * Class for handeling 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 | */ |
31 | class 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 integer |
51 | */ |
52 | private int $width; |
53 | |
54 | /** |
55 | * Height of new image |
56 | * |
57 | * @var integer |
58 | */ |
59 | private int $height; |
60 | |
61 | /** |
62 | * Maintain aspect ratio |
63 | * |
64 | * @var boolean |
65 | */ |
66 | private bool $maintainAspectRatio; |
67 | |
68 | /** |
69 | * Don't scale up |
70 | * |
71 | * @var boolean |
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 integer $width |
85 | * Image width |
86 | * @param integer $height |
87 | * Image height |
88 | * @param boolean $maintainAspectRatio |
89 | * Should we maintain aspect ratio? |
90 | * @param boolean $dontScaleUp |
91 | * Should we prohipped scaling up? |
92 | * @param string $aspect |
93 | * How should we handle aspect ratio? |
94 | * |
95 | * @throws \InvalidArgumentException |
96 | */ |
97 | public function __construct( |
98 | int|null $width, |
99 | int|null $height, |
100 | bool $maintainAspectRatio, |
101 | bool $dontScaleUp, |
102 | string $aspect = Resize::ASPECT_FIT |
103 | ) { |
104 | if ($width == "" && $height == "") { |
105 | $message = 'You must supply height or width or both to resize the image'; |
106 | throw new InvalidArgumentException($message); |
107 | } |
108 | if (! is_numeric($width) && $width != '') { |
109 | $message = 'width must be an integer value or empty.'; |
110 | throw new InvalidArgumentException($message); |
111 | } |
112 | if (! is_numeric($height) && $height != '') { |
113 | $message = 'height must be an integer value or empty.'; |
114 | throw new InvalidArgumentException($message); |
115 | } |
116 | if (!in_array($aspect, array(Resize::ASPECT_FIT, Resize::ASPECT_FILL))) { |
117 | $message = sprintf('aspect must be "%s" or "%s".', Resize::ASPECT_FIT, Resize::ASPECT_FILL); |
118 | throw new \InvalidArgumentException($message); |
119 | } |
120 | |
121 | $this->width = $width; |
122 | $this->height = $height; |
123 | $this->maintainAspectRatio = $maintainAspectRatio; |
124 | $this->dontScaleUp = $dontScaleUp; |
125 | $this->aspect = $aspect; |
126 | } |
127 | |
128 | /** |
129 | * (non-PHPdoc) |
130 | * |
131 | * @param Query $query |
132 | * The query to add the action to |
133 | * @return Query |
134 | * @see Action::perform() |
135 | */ |
136 | public function perform(Query $query): Query |
137 | { |
138 | $query->notWith('resize', Query::ARGUMENT_TYPE_INPUT); |
139 | $query->notWith('resample', Query::ARGUMENT_TYPE_INPUT); |
140 | |
141 | $option = " -resize "; |
142 | |
143 | if ($this->width != '') { |
144 | $option .= $this->width; |
145 | } |
146 | |
147 | if ($this->height != '') { |
148 | $option .= "x" . $this->height; |
149 | } |
150 | |
151 | if ($this->aspect == Resize::ASPECT_FILL) { |
152 | $option .= "^"; |
153 | } |
154 | |
155 | if ($this->dontScaleUp) { |
156 | $option .= "\>"; |
157 | } |
158 | |
159 | if (! $this->maintainAspectRatio) { |
160 | $option .= "!"; |
161 | } |
162 | $option .= " "; |
163 | $query->setInputOption($option); |
164 | |
165 | return $query; |
166 | } |
167 | } |