-
Notifications
You must be signed in to change notification settings - Fork 611
/
ImageTransform.php
323 lines (291 loc) · 7.89 KB
/
ImageTransform.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
<?php
/**
* @link https://craftcms.com/
* @copyright Copyright (c) Pixel & Tonic, Inc.
* @license https://craftcms.github.io/license/
*/
namespace craft\models;
use Craft;
use craft\base\imagetransforms\ImageTransformerInterface;
use craft\base\Model;
use craft\imagetransforms\ImageTransformer;
use craft\records\ImageTransform as ImageTransformRecord;
use craft\validators\ColorValidator;
use craft\validators\DateTimeValidator;
use craft\validators\HandleValidator;
use craft\validators\UniqueValidator;
use DateTime;
/**
* The ImageTransform model class.
*
* @property bool $isNamedTransform Whether this is a named transform
* @author Pixel & Tonic, Inc. <support@pixelandtonic.com>
* @since 4.0.0
*/
class ImageTransform extends Model
{
/**
* @var string The default image transformer
*/
public const DEFAULT_TRANSFORMER = ImageTransformer::class;
/**
* @var int|null ID
*/
public ?int $id = null;
/**
* @var string|null Name
*/
public ?string $name = null;
/**
* @var string|null Handle
*/
public ?string $handle = null;
/**
* @var int|null Width
*/
public ?int $width = null;
/**
* @var int|null Height
*/
public ?int $height = null;
/**
* @var string|null Format
*/
public ?string $format = null;
/**
* @var DateTime|null Dimension change time
*/
public ?DateTime $parameterChangeTime = null;
/**
* @var string 'crop'|'fit'|'stretch'|'letterbox' Mode
*/
public string $mode = 'crop';
/**
* @var string Position
* @phpstan-var 'top-left'|'top-center'|'top-right'|'center-left'|'center-center'|'center-right'|'bottom-left'|'bottom-center'|'bottom-right'
*/
public string $position = 'center-center';
/**
* @var string Interlace
* @phpstan-var 'none'|'line'|'plane'|'partition'
*/
public string $interlace = 'none';
/**
* @var int|null Quality
*/
public ?int $quality = null;
/**
* @var string|null UID
*/
public ?string $uid = null;
/**
* @var string|null Fill color
* @since 4.4.0
*/
public ?string $fill = null;
/**
* @var bool|null Allow upscaling
* @since 4.4.0
*/
public ?bool $upscale = null;
/**
* @var string The image transformer to use.
* @phpstan-var class-string<ImageTransformerInterface>
*/
protected string $transformer = self::DEFAULT_TRANSFORMER;
/**
* @inheritdoc
*/
public function __construct($config = [])
{
if (isset($config['width']) && !$config['width']) {
unset($config['width']);
}
if (isset($config['height']) && !$config['height']) {
unset($config['height']);
}
if (isset($config['quality']) && !$config['quality']) {
unset($config['quality']);
}
parent::__construct($config);
}
/**
* @inheritdoc
*/
public function init(): void
{
parent::init();
if (!isset($this->upscale)) {
$this->upscale = Craft::$app->getConfig()->getGeneral()->upscaleImages;
}
}
/**
* @inheritdoc
*/
public function attributeLabels(): array
{
return [
'handle' => Craft::t('app', 'Handle'),
'height' => Craft::t('app', 'Height'),
'mode' => Craft::t('app', 'Mode'),
'name' => Craft::t('app', 'Name'),
'position' => Craft::t('app', 'Position'),
'quality' => Craft::t('app', 'Quality'),
'width' => Craft::t('app', 'Width'),
'fill' => Craft::t('app', 'Fill Color'),
'upscale' => Craft::t('app', 'Allow Upscaling'),
'transformer' => Craft::t('app', 'Image transformer'),
];
}
/**
* @inheritdoc
*/
protected function defineRules(): array
{
$rules = parent::defineRules();
$rules[] = [['id', 'width', 'height', 'quality'], 'number', 'integerOnly' => true];
$rules[] = [['parameterChangeTime'], DateTimeValidator::class];
$rules[] = [['handle'], 'string', 'max' => 255];
$rules[] = [['name', 'handle', 'mode', 'position'], 'required'];
$rules[] = [['handle'], 'string', 'max' => 255];
$rules[] = [['fill'], ColorValidator::class];
$rules[] = [['upscale'], 'boolean'];
$rules[] = [
['mode'],
'in',
'range' => [
'stretch',
'fit',
'crop',
'letterbox',
],
];
$rules[] = [
['position'],
'in',
'range' => [
'top-left',
'top-center',
'top-right',
'center-left',
'center-center',
'center-right',
'bottom-left',
'bottom-center',
'bottom-right',
],
];
$rules[] = [
['interlace'],
'in',
'range' => [
'none',
'line',
'plane',
'partition',
],
];
$rules[] = [
['handle'],
HandleValidator::class,
'reservedWords' => [
'id',
'dateCreated',
'dateUpdated',
'uid',
'title',
],
];
$rules[] = [
['name', 'handle'],
UniqueValidator::class,
'targetClass' => ImageTransformRecord::class,
];
return $rules;
}
/**
* Use the folder name as the string representation.
*
* @return string
*/
public function __toString(): string
{
return (string)$this->name;
}
/**
* Return whether this is a named transform
*
* @return bool
*/
public function getIsNamedTransform(): bool
{
return !empty($this->name);
}
/**
* Get a list of transform modes.
*
* @return array
*/
public static function modes(): array
{
return [
'crop' => Craft::t('app', 'Scale and crop'),
'fit' => Craft::t('app', 'Scale to fit'),
'stretch' => Craft::t('app', 'Stretch to fit'),
'letterbox' => Craft::t('app', 'Letterbox'),
];
}
/**
* Return the image transformer for this transform.
*
* @return ImageTransformerInterface
*/
public function getImageTransformer(): ImageTransformerInterface
{
return Craft::$app->getImageTransforms()->getImageTransformer($this->transformer);
}
/**
* Returns the image transformer.
*
* @return string
*/
public function getTransformer(): string
{
return $this->transformer;
}
/**
* Sets the image transformer.
*
* @param string $transformer
*/
public function setTransformer(string $transformer): void
{
if (!is_subclass_of($transformer, ImageTransformerInterface::class)) {
Craft::warning("Invalid image transformer: $transformer", __METHOD__);
$transformer = self::DEFAULT_TRANSFORMER;
}
$this->transformer = $transformer;
}
/**
* Returns the transform’s config.
*
* @return array
* @since 4.4.2
*/
public function getConfig(): array
{
return [
'fill' => $this->fill,
'format' => $this->format,
'handle' => $this->handle,
'height' => $this->height,
'interlace' => $this->interlace,
'mode' => $this->mode,
'name' => $this->name,
'position' => $this->position,
'quality' => $this->quality,
'upscale' => $this->upscale ?? Craft::$app->getConfig()->getGeneral()->upscaleImages,
'width' => $this->width,
];
}
}