@vituum/vite-plugin-latte 1.1.0 → 1.2.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (71) hide show
  1. package/latte/PlaceholderFunction.php +2 -2
  2. package/package.json +1 -1
  3. package/vendor/autoload.php +1 -1
  4. package/vendor/composer/autoload_classmap.php +51 -0
  5. package/vendor/composer/autoload_real.php +4 -4
  6. package/vendor/composer/autoload_static.php +53 -2
  7. package/vendor/composer/installed.json +96 -7
  8. package/vendor/composer/installed.php +14 -5
  9. package/vendor/latte/latte/src/Latte/Compiler/Escaper.php +11 -2
  10. package/vendor/latte/latte/src/Latte/Compiler/Nodes/Php/FilterNode.php +1 -1
  11. package/vendor/latte/latte/src/Latte/Compiler/Nodes/Php/ModifierNode.php +2 -2
  12. package/vendor/latte/latte/src/Latte/Compiler/Nodes/Php/NameNode.php +11 -21
  13. package/vendor/latte/latte/src/Latte/Compiler/PrintContext.php +1 -1
  14. package/vendor/latte/latte/src/Latte/Compiler/TagLexer.php +2 -2
  15. package/vendor/latte/latte/src/Latte/Compiler/TagParserData.php +275 -280
  16. package/vendor/latte/latte/src/Latte/Compiler/TemplateGenerator.php +4 -4
  17. package/vendor/latte/latte/src/Latte/Compiler/TemplateLexer.php +10 -2
  18. package/vendor/latte/latte/src/Latte/Compiler/TemplateParserHtml.php +6 -2
  19. package/vendor/latte/latte/src/Latte/Engine.php +109 -97
  20. package/vendor/latte/latte/src/Latte/Essential/AuxiliaryIterator.php +46 -0
  21. package/vendor/latte/latte/src/Latte/Essential/Blueprint.php +42 -25
  22. package/vendor/latte/latte/src/Latte/Essential/CoreExtension.php +76 -72
  23. package/vendor/latte/latte/src/Latte/Essential/Filters.php +95 -37
  24. package/vendor/latte/latte/src/Latte/Essential/Nodes/ContentTypeNode.php +7 -0
  25. package/vendor/latte/latte/src/Latte/Essential/Nodes/ForeachNode.php +36 -0
  26. package/vendor/latte/latte/src/Latte/Essential/Nodes/TemplatePrintNode.php +25 -3
  27. package/vendor/latte/latte/src/Latte/Essential/Nodes/VarPrintNode.php +9 -2
  28. package/vendor/latte/latte/src/Latte/Essential/Passes.php +10 -50
  29. package/vendor/latte/latte/src/Latte/Helpers.php +3 -1
  30. package/vendor/latte/latte/src/Latte/Loader.php +1 -0
  31. package/vendor/latte/latte/src/Latte/Loaders/FileLoader.php +1 -2
  32. package/vendor/latte/latte/src/Latte/Runtime/Filters.php +1 -4
  33. package/vendor/latte/latte/src/Latte/Runtime/FunctionExecutor.php +68 -0
  34. package/vendor/latte/latte/src/Latte/Runtime/Template.php +1 -3
  35. package/vendor/nette/utils/.phpstorm.meta.php +13 -0
  36. package/vendor/nette/utils/composer.json +51 -0
  37. package/vendor/nette/utils/license.md +60 -0
  38. package/vendor/nette/utils/readme.md +56 -0
  39. package/vendor/nette/utils/src/HtmlStringable.php +22 -0
  40. package/vendor/nette/utils/src/Iterators/CachingIterator.php +164 -0
  41. package/vendor/nette/utils/src/Iterators/Mapper.php +34 -0
  42. package/vendor/nette/utils/src/SmartObject.php +140 -0
  43. package/vendor/nette/utils/src/StaticClass.php +34 -0
  44. package/vendor/nette/utils/src/Translator.php +25 -0
  45. package/vendor/nette/utils/src/Utils/ArrayHash.php +106 -0
  46. package/vendor/nette/utils/src/Utils/ArrayList.php +136 -0
  47. package/vendor/nette/utils/src/Utils/Arrays.php +522 -0
  48. package/vendor/nette/utils/src/Utils/Callback.php +137 -0
  49. package/vendor/nette/utils/src/Utils/DateTime.php +140 -0
  50. package/vendor/nette/utils/src/Utils/FileInfo.php +69 -0
  51. package/vendor/nette/utils/src/Utils/FileSystem.php +326 -0
  52. package/vendor/nette/utils/src/Utils/Finder.php +510 -0
  53. package/vendor/nette/utils/src/Utils/Floats.php +107 -0
  54. package/vendor/nette/utils/src/Utils/Helpers.php +104 -0
  55. package/vendor/nette/utils/src/Utils/Html.php +839 -0
  56. package/vendor/nette/utils/src/Utils/Image.php +829 -0
  57. package/vendor/nette/utils/src/Utils/ImageColor.php +75 -0
  58. package/vendor/nette/utils/src/Utils/ImageType.php +25 -0
  59. package/vendor/nette/utils/src/Utils/Iterables.php +159 -0
  60. package/vendor/nette/utils/src/Utils/Json.php +84 -0
  61. package/vendor/nette/utils/src/Utils/ObjectHelpers.php +229 -0
  62. package/vendor/nette/utils/src/Utils/Paginator.php +245 -0
  63. package/vendor/nette/utils/src/Utils/Random.php +52 -0
  64. package/vendor/nette/utils/src/Utils/Reflection.php +320 -0
  65. package/vendor/nette/utils/src/Utils/ReflectionMethod.php +36 -0
  66. package/vendor/nette/utils/src/Utils/Strings.php +708 -0
  67. package/vendor/nette/utils/src/Utils/Type.php +267 -0
  68. package/vendor/nette/utils/src/Utils/Validators.php +416 -0
  69. package/vendor/nette/utils/src/Utils/exceptions.php +50 -0
  70. package/vendor/nette/utils/src/compatibility.php +32 -0
  71. package/vendor/nette/utils/src/exceptions.php +109 -0
@@ -0,0 +1,829 @@
1
+ <?php
2
+
3
+ /**
4
+ * This file is part of the Nette Framework (https://nette.org)
5
+ * Copyright (c) 2004 David Grudl (https://davidgrudl.com)
6
+ */
7
+
8
+ declare(strict_types=1);
9
+
10
+ namespace Nette\Utils;
11
+
12
+ use Nette;
13
+
14
+
15
+ /**
16
+ * Basic manipulation with images. Supported types are JPEG, PNG, GIF, WEBP, AVIF and BMP.
17
+ *
18
+ * <code>
19
+ * $image = Image::fromFile('nette.jpg');
20
+ * $image->resize(150, 100);
21
+ * $image->sharpen();
22
+ * $image->send();
23
+ * </code>
24
+ *
25
+ * @method Image affine(array $affine, ?array $clip = null)
26
+ * @method void alphaBlending(bool $enable)
27
+ * @method void antialias(bool $enable)
28
+ * @method void arc(int $centerX, int $centerY, int $width, int $height, int $startAngle, int $endAngle, ImageColor $color)
29
+ * @method int colorAllocate(int $red, int $green, int $blue)
30
+ * @method int colorAllocateAlpha(int $red, int $green, int $blue, int $alpha)
31
+ * @method int colorAt(int $x, int $y)
32
+ * @method int colorClosest(int $red, int $green, int $blue)
33
+ * @method int colorClosestAlpha(int $red, int $green, int $blue, int $alpha)
34
+ * @method int colorClosestHWB(int $red, int $green, int $blue)
35
+ * @method void colorDeallocate(int $color)
36
+ * @method int colorExact(int $red, int $green, int $blue)
37
+ * @method int colorExactAlpha(int $red, int $green, int $blue, int $alpha)
38
+ * @method void colorMatch(Image $image2)
39
+ * @method int colorResolve(int $red, int $green, int $blue)
40
+ * @method int colorResolveAlpha(int $red, int $green, int $blue, int $alpha)
41
+ * @method void colorSet(int $index, int $red, int $green, int $blue, int $alpha = 0)
42
+ * @method array colorsForIndex(int $color)
43
+ * @method int colorsTotal()
44
+ * @method int colorTransparent(?int $color = null)
45
+ * @method void convolution(array $matrix, float $div, float $offset)
46
+ * @method void copy(Image $src, int $dstX, int $dstY, int $srcX, int $srcY, int $srcW, int $srcH)
47
+ * @method void copyMerge(Image $src, int $dstX, int $dstY, int $srcX, int $srcY, int $srcW, int $srcH, int $pct)
48
+ * @method void copyMergeGray(Image $src, int $dstX, int $dstY, int $srcX, int $srcY, int $srcW, int $srcH, int $pct)
49
+ * @method void copyResampled(Image $src, int $dstX, int $dstY, int $srcX, int $srcY, int $dstW, int $dstH, int $srcW, int $srcH)
50
+ * @method void copyResized(Image $src, int $dstX, int $dstY, int $srcX, int $srcY, int $dstW, int $dstH, int $srcW, int $srcH)
51
+ * @method Image cropAuto(int $mode = IMG_CROP_DEFAULT, float $threshold = .5, ?ImageColor $color = null)
52
+ * @method void ellipse(int $centerX, int $centerY, int $width, int $height, ImageColor $color)
53
+ * @method void fill(int $x, int $y, ImageColor $color)
54
+ * @method void filledArc(int $centerX, int $centerY, int $width, int $height, int $startAngle, int $endAngle, ImageColor $color, int $style)
55
+ * @method void filledEllipse(int $centerX, int $centerY, int $width, int $height, ImageColor $color)
56
+ * @method void filledPolygon(array $points, ImageColor $color)
57
+ * @method void filledRectangle(int $x1, int $y1, int $x2, int $y2, ImageColor $color)
58
+ * @method void fillToBorder(int $x, int $y, ImageColor $borderColor, ImageColor $color)
59
+ * @method void filter(int $filter, ...$args)
60
+ * @method void flip(int $mode)
61
+ * @method array ftText(float $size, float $angle, int $x, int $y, ImageColor $color, string $fontFile, string $text, array $options = [])
62
+ * @method void gammaCorrect(float $inputgamma, float $outputgamma)
63
+ * @method array getClip()
64
+ * @method int getInterpolation()
65
+ * @method int interlace(?bool $enable = null)
66
+ * @method bool isTrueColor()
67
+ * @method void layerEffect(int $effect)
68
+ * @method void line(int $x1, int $y1, int $x2, int $y2, ImageColor $color)
69
+ * @method void openPolygon(array $points, ImageColor $color)
70
+ * @method void paletteCopy(Image $source)
71
+ * @method void paletteToTrueColor()
72
+ * @method void polygon(array $points, ImageColor $color)
73
+ * @method void rectangle(int $x1, int $y1, int $x2, int $y2, ImageColor $color)
74
+ * @method mixed resolution(?int $resolutionX = null, ?int $resolutionY = null)
75
+ * @method Image rotate(float $angle, ImageColor $backgroundColor)
76
+ * @method void saveAlpha(bool $enable)
77
+ * @method Image scale(int $newWidth, int $newHeight = -1, int $mode = IMG_BILINEAR_FIXED)
78
+ * @method void setBrush(Image $brush)
79
+ * @method void setClip(int $x1, int $y1, int $x2, int $y2)
80
+ * @method void setInterpolation(int $method = IMG_BILINEAR_FIXED)
81
+ * @method void setPixel(int $x, int $y, ImageColor $color)
82
+ * @method void setStyle(array $style)
83
+ * @method void setThickness(int $thickness)
84
+ * @method void setTile(Image $tile)
85
+ * @method void trueColorToPalette(bool $dither, int $ncolors)
86
+ * @method array ttfText(float $size, float $angle, int $x, int $y, ImageColor $color, string $fontfile, string $text, array $options = [])
87
+ * @property-read positive-int $width
88
+ * @property-read positive-int $height
89
+ * @property-read \GdImage $imageResource
90
+ */
91
+ class Image
92
+ {
93
+ use Nette\SmartObject;
94
+
95
+ /** Prevent from getting resized to a bigger size than the original */
96
+ public const ShrinkOnly = 0b0001;
97
+
98
+ /** Resizes to a specified width and height without keeping aspect ratio */
99
+ public const Stretch = 0b0010;
100
+
101
+ /** Resizes to fit into a specified width and height and preserves aspect ratio */
102
+ public const OrSmaller = 0b0000;
103
+
104
+ /** Resizes while bounding the smaller dimension to the specified width or height and preserves aspect ratio */
105
+ public const OrBigger = 0b0100;
106
+
107
+ /** Resizes to the smallest possible size to completely cover specified width and height and reserves aspect ratio */
108
+ public const Cover = 0b1000;
109
+
110
+ /** @deprecated use Image::ShrinkOnly */
111
+ public const SHRINK_ONLY = self::ShrinkOnly;
112
+
113
+ /** @deprecated use Image::Stretch */
114
+ public const STRETCH = self::Stretch;
115
+
116
+ /** @deprecated use Image::OrSmaller */
117
+ public const FIT = self::OrSmaller;
118
+
119
+ /** @deprecated use Image::OrBigger */
120
+ public const FILL = self::OrBigger;
121
+
122
+ /** @deprecated use Image::Cover */
123
+ public const EXACT = self::Cover;
124
+
125
+ /** @deprecated use Image::EmptyGIF */
126
+ public const EMPTY_GIF = self::EmptyGIF;
127
+
128
+ /** image types */
129
+ public const
130
+ JPEG = ImageType::JPEG,
131
+ PNG = ImageType::PNG,
132
+ GIF = ImageType::GIF,
133
+ WEBP = ImageType::WEBP,
134
+ AVIF = ImageType::AVIF,
135
+ BMP = ImageType::BMP;
136
+
137
+ public const EmptyGIF = "GIF89a\x01\x00\x01\x00\x80\x00\x00\x00\x00\x00\x00\x00\x00!\xf9\x04\x01\x00\x00\x00\x00,\x00\x00\x00\x00\x01\x00\x01\x00\x00\x02\x02D\x01\x00;";
138
+
139
+ private const Formats = [ImageType::JPEG => 'jpeg', ImageType::PNG => 'png', ImageType::GIF => 'gif', ImageType::WEBP => 'webp', ImageType::AVIF => 'avif', ImageType::BMP => 'bmp'];
140
+
141
+ private \GdImage $image;
142
+
143
+
144
+ /**
145
+ * Returns RGB color (0..255) and transparency (0..127).
146
+ * @deprecated use ImageColor::rgb()
147
+ */
148
+ public static function rgb(int $red, int $green, int $blue, int $transparency = 0): array
149
+ {
150
+ return [
151
+ 'red' => max(0, min(255, $red)),
152
+ 'green' => max(0, min(255, $green)),
153
+ 'blue' => max(0, min(255, $blue)),
154
+ 'alpha' => max(0, min(127, $transparency)),
155
+ ];
156
+ }
157
+
158
+
159
+ /**
160
+ * Reads an image from a file and returns its type in $type.
161
+ * @throws Nette\NotSupportedException if gd extension is not loaded
162
+ * @throws UnknownImageFileException if file not found or file type is not known
163
+ */
164
+ public static function fromFile(string $file, ?int &$type = null): static
165
+ {
166
+ if (!extension_loaded('gd')) {
167
+ throw new Nette\NotSupportedException('PHP extension GD is not loaded.');
168
+ }
169
+
170
+ $type = self::detectTypeFromFile($file);
171
+ if (!$type) {
172
+ throw new UnknownImageFileException(is_file($file) ? "Unknown type of file '$file'." : "File '$file' not found.");
173
+ }
174
+
175
+ return self::invokeSafe('imagecreatefrom' . self::Formats[$type], $file, "Unable to open file '$file'.", __METHOD__);
176
+ }
177
+
178
+
179
+ /**
180
+ * Reads an image from a string and returns its type in $type.
181
+ * @throws Nette\NotSupportedException if gd extension is not loaded
182
+ * @throws ImageException
183
+ */
184
+ public static function fromString(string $s, ?int &$type = null): static
185
+ {
186
+ if (!extension_loaded('gd')) {
187
+ throw new Nette\NotSupportedException('PHP extension GD is not loaded.');
188
+ }
189
+
190
+ $type = self::detectTypeFromString($s);
191
+ if (!$type) {
192
+ throw new UnknownImageFileException('Unknown type of image.');
193
+ }
194
+
195
+ return self::invokeSafe('imagecreatefromstring', $s, 'Unable to open image from string.', __METHOD__);
196
+ }
197
+
198
+
199
+ private static function invokeSafe(string $func, string $arg, string $message, string $callee): static
200
+ {
201
+ $errors = [];
202
+ $res = Callback::invokeSafe($func, [$arg], function (string $message) use (&$errors): void {
203
+ $errors[] = $message;
204
+ });
205
+
206
+ if (!$res) {
207
+ throw new ImageException($message . ' Errors: ' . implode(', ', $errors));
208
+ } elseif ($errors) {
209
+ trigger_error($callee . '(): ' . implode(', ', $errors), E_USER_WARNING);
210
+ }
211
+
212
+ return new static($res);
213
+ }
214
+
215
+
216
+ /**
217
+ * Creates a new true color image of the given dimensions. The default color is black.
218
+ * @param positive-int $width
219
+ * @param positive-int $height
220
+ * @throws Nette\NotSupportedException if gd extension is not loaded
221
+ */
222
+ public static function fromBlank(int $width, int $height, ImageColor|array|null $color = null): static
223
+ {
224
+ if (!extension_loaded('gd')) {
225
+ throw new Nette\NotSupportedException('PHP extension GD is not loaded.');
226
+ }
227
+
228
+ if ($width < 1 || $height < 1) {
229
+ throw new Nette\InvalidArgumentException('Image width and height must be greater than zero.');
230
+ }
231
+
232
+ $image = new static(imagecreatetruecolor($width, $height));
233
+ if ($color) {
234
+ $image->alphablending(false);
235
+ $image->filledrectangle(0, 0, $width - 1, $height - 1, $color);
236
+ $image->alphablending(true);
237
+ }
238
+
239
+ return $image;
240
+ }
241
+
242
+
243
+ /**
244
+ * Returns the type of image from file.
245
+ * @return ImageType::*|null
246
+ */
247
+ public static function detectTypeFromFile(string $file, &$width = null, &$height = null): ?int
248
+ {
249
+ [$width, $height, $type] = @getimagesize($file); // @ - files smaller than 12 bytes causes read error
250
+ return isset(self::Formats[$type]) ? $type : null;
251
+ }
252
+
253
+
254
+ /**
255
+ * Returns the type of image from string.
256
+ * @return ImageType::*|null
257
+ */
258
+ public static function detectTypeFromString(string $s, &$width = null, &$height = null): ?int
259
+ {
260
+ [$width, $height, $type] = @getimagesizefromstring($s); // @ - strings smaller than 12 bytes causes read error
261
+ return isset(self::Formats[$type]) ? $type : null;
262
+ }
263
+
264
+
265
+ /**
266
+ * Returns the file extension for the given image type.
267
+ * @param ImageType::* $type
268
+ * @return value-of<self::Formats>
269
+ */
270
+ public static function typeToExtension(int $type): string
271
+ {
272
+ if (!isset(self::Formats[$type])) {
273
+ throw new Nette\InvalidArgumentException("Unsupported image type '$type'.");
274
+ }
275
+
276
+ return self::Formats[$type];
277
+ }
278
+
279
+
280
+ /**
281
+ * Returns the image type for given file extension.
282
+ * @return ImageType::*
283
+ */
284
+ public static function extensionToType(string $extension): int
285
+ {
286
+ $extensions = array_flip(self::Formats) + ['jpg' => ImageType::JPEG];
287
+ $extension = strtolower($extension);
288
+ if (!isset($extensions[$extension])) {
289
+ throw new Nette\InvalidArgumentException("Unsupported file extension '$extension'.");
290
+ }
291
+
292
+ return $extensions[$extension];
293
+ }
294
+
295
+
296
+ /**
297
+ * Returns the mime type for the given image type.
298
+ * @param ImageType::* $type
299
+ */
300
+ public static function typeToMimeType(int $type): string
301
+ {
302
+ return 'image/' . self::typeToExtension($type);
303
+ }
304
+
305
+
306
+ /**
307
+ * @param ImageType::* $type
308
+ */
309
+ public static function isTypeSupported(int $type): bool
310
+ {
311
+ return (bool) (imagetypes() & match ($type) {
312
+ ImageType::JPEG => IMG_JPG,
313
+ ImageType::PNG => IMG_PNG,
314
+ ImageType::GIF => IMG_GIF,
315
+ ImageType::WEBP => IMG_WEBP,
316
+ ImageType::AVIF => 256, // IMG_AVIF,
317
+ ImageType::BMP => IMG_BMP,
318
+ default => 0,
319
+ });
320
+ }
321
+
322
+
323
+ /** @return ImageType[] */
324
+ public static function getSupportedTypes(): array
325
+ {
326
+ $flag = imagetypes();
327
+ return array_filter([
328
+ $flag & IMG_GIF ? ImageType::GIF : null,
329
+ $flag & IMG_JPG ? ImageType::JPEG : null,
330
+ $flag & IMG_PNG ? ImageType::PNG : null,
331
+ $flag & IMG_WEBP ? ImageType::WEBP : null,
332
+ $flag & 256 ? ImageType::AVIF : null, // IMG_AVIF
333
+ $flag & IMG_BMP ? ImageType::BMP : null,
334
+ ]);
335
+ }
336
+
337
+
338
+ /**
339
+ * Wraps GD image.
340
+ */
341
+ public function __construct(\GdImage $image)
342
+ {
343
+ $this->setImageResource($image);
344
+ imagesavealpha($image, true);
345
+ }
346
+
347
+
348
+ /**
349
+ * Returns image width.
350
+ * @return positive-int
351
+ */
352
+ public function getWidth(): int
353
+ {
354
+ return imagesx($this->image);
355
+ }
356
+
357
+
358
+ /**
359
+ * Returns image height.
360
+ * @return positive-int
361
+ */
362
+ public function getHeight(): int
363
+ {
364
+ return imagesy($this->image);
365
+ }
366
+
367
+
368
+ /**
369
+ * Sets image resource.
370
+ */
371
+ protected function setImageResource(\GdImage $image): static
372
+ {
373
+ $this->image = $image;
374
+ return $this;
375
+ }
376
+
377
+
378
+ /**
379
+ * Returns image GD resource.
380
+ */
381
+ public function getImageResource(): \GdImage
382
+ {
383
+ return $this->image;
384
+ }
385
+
386
+
387
+ /**
388
+ * Scales an image. Width and height accept pixels or percent.
389
+ * @param int-mask-of<self::OrSmaller|self::OrBigger|self::Stretch|self::Cover|self::ShrinkOnly> $mode
390
+ */
391
+ public function resize(int|string|null $width, int|string|null $height, int $mode = self::OrSmaller): static
392
+ {
393
+ if ($mode & self::Cover) {
394
+ return $this->resize($width, $height, self::OrBigger)->crop('50%', '50%', $width, $height);
395
+ }
396
+
397
+ [$newWidth, $newHeight] = static::calculateSize($this->getWidth(), $this->getHeight(), $width, $height, $mode);
398
+
399
+ if ($newWidth !== $this->getWidth() || $newHeight !== $this->getHeight()) { // resize
400
+ $newImage = static::fromBlank($newWidth, $newHeight, ImageColor::rgb(0, 0, 0, 0))->getImageResource();
401
+ imagecopyresampled(
402
+ $newImage,
403
+ $this->image,
404
+ 0,
405
+ 0,
406
+ 0,
407
+ 0,
408
+ $newWidth,
409
+ $newHeight,
410
+ $this->getWidth(),
411
+ $this->getHeight(),
412
+ );
413
+ $this->image = $newImage;
414
+ }
415
+
416
+ if ($width < 0 || $height < 0) {
417
+ imageflip($this->image, $width < 0 ? ($height < 0 ? IMG_FLIP_BOTH : IMG_FLIP_HORIZONTAL) : IMG_FLIP_VERTICAL);
418
+ }
419
+
420
+ return $this;
421
+ }
422
+
423
+
424
+ /**
425
+ * Calculates dimensions of resized image. Width and height accept pixels or percent.
426
+ * @param int-mask-of<self::OrSmaller|self::OrBigger|self::Stretch|self::Cover|self::ShrinkOnly> $mode
427
+ */
428
+ public static function calculateSize(
429
+ int $srcWidth,
430
+ int $srcHeight,
431
+ $newWidth,
432
+ $newHeight,
433
+ int $mode = self::OrSmaller,
434
+ ): array
435
+ {
436
+ if ($newWidth === null) {
437
+ } elseif (self::isPercent($newWidth)) {
438
+ $newWidth = (int) round($srcWidth / 100 * abs($newWidth));
439
+ $percents = true;
440
+ } else {
441
+ $newWidth = abs($newWidth);
442
+ }
443
+
444
+ if ($newHeight === null) {
445
+ } elseif (self::isPercent($newHeight)) {
446
+ $newHeight = (int) round($srcHeight / 100 * abs($newHeight));
447
+ $mode |= empty($percents) ? 0 : self::Stretch;
448
+ } else {
449
+ $newHeight = abs($newHeight);
450
+ }
451
+
452
+ if ($mode & self::Stretch) { // non-proportional
453
+ if (!$newWidth || !$newHeight) {
454
+ throw new Nette\InvalidArgumentException('For stretching must be both width and height specified.');
455
+ }
456
+
457
+ if ($mode & self::ShrinkOnly) {
458
+ $newWidth = min($srcWidth, $newWidth);
459
+ $newHeight = min($srcHeight, $newHeight);
460
+ }
461
+ } else { // proportional
462
+ if (!$newWidth && !$newHeight) {
463
+ throw new Nette\InvalidArgumentException('At least width or height must be specified.');
464
+ }
465
+
466
+ $scale = [];
467
+ if ($newWidth > 0) { // fit width
468
+ $scale[] = $newWidth / $srcWidth;
469
+ }
470
+
471
+ if ($newHeight > 0) { // fit height
472
+ $scale[] = $newHeight / $srcHeight;
473
+ }
474
+
475
+ if ($mode & self::OrBigger) {
476
+ $scale = [max($scale)];
477
+ }
478
+
479
+ if ($mode & self::ShrinkOnly) {
480
+ $scale[] = 1;
481
+ }
482
+
483
+ $scale = min($scale);
484
+ $newWidth = (int) round($srcWidth * $scale);
485
+ $newHeight = (int) round($srcHeight * $scale);
486
+ }
487
+
488
+ return [max($newWidth, 1), max($newHeight, 1)];
489
+ }
490
+
491
+
492
+ /**
493
+ * Crops image. Arguments accepts pixels or percent.
494
+ */
495
+ public function crop(int|string $left, int|string $top, int|string $width, int|string $height): static
496
+ {
497
+ [$r['x'], $r['y'], $r['width'], $r['height']]
498
+ = static::calculateCutout($this->getWidth(), $this->getHeight(), $left, $top, $width, $height);
499
+ if (gd_info()['GD Version'] === 'bundled (2.1.0 compatible)') {
500
+ $this->image = imagecrop($this->image, $r);
501
+ imagesavealpha($this->image, true);
502
+ } else {
503
+ $newImage = static::fromBlank($r['width'], $r['height'], ImageColor::rgb(0, 0, 0, 0))->getImageResource();
504
+ imagecopy($newImage, $this->image, 0, 0, $r['x'], $r['y'], $r['width'], $r['height']);
505
+ $this->image = $newImage;
506
+ }
507
+
508
+ return $this;
509
+ }
510
+
511
+
512
+ /**
513
+ * Calculates dimensions of cutout in image. Arguments accepts pixels or percent.
514
+ */
515
+ public static function calculateCutout(
516
+ int $srcWidth,
517
+ int $srcHeight,
518
+ int|string $left,
519
+ int|string $top,
520
+ int|string $newWidth,
521
+ int|string $newHeight,
522
+ ): array
523
+ {
524
+ if (self::isPercent($newWidth)) {
525
+ $newWidth = (int) round($srcWidth / 100 * $newWidth);
526
+ }
527
+
528
+ if (self::isPercent($newHeight)) {
529
+ $newHeight = (int) round($srcHeight / 100 * $newHeight);
530
+ }
531
+
532
+ if (self::isPercent($left)) {
533
+ $left = (int) round(($srcWidth - $newWidth) / 100 * $left);
534
+ }
535
+
536
+ if (self::isPercent($top)) {
537
+ $top = (int) round(($srcHeight - $newHeight) / 100 * $top);
538
+ }
539
+
540
+ if ($left < 0) {
541
+ $newWidth += $left;
542
+ $left = 0;
543
+ }
544
+
545
+ if ($top < 0) {
546
+ $newHeight += $top;
547
+ $top = 0;
548
+ }
549
+
550
+ $newWidth = min($newWidth, $srcWidth - $left);
551
+ $newHeight = min($newHeight, $srcHeight - $top);
552
+ return [$left, $top, $newWidth, $newHeight];
553
+ }
554
+
555
+
556
+ /**
557
+ * Sharpens image a little bit.
558
+ */
559
+ public function sharpen(): static
560
+ {
561
+ imageconvolution($this->image, [ // my magic numbers ;)
562
+ [-1, -1, -1],
563
+ [-1, 24, -1],
564
+ [-1, -1, -1],
565
+ ], 16, 0);
566
+ return $this;
567
+ }
568
+
569
+
570
+ /**
571
+ * Puts another image into this image. Left and top accepts pixels or percent.
572
+ * @param int<0, 100> $opacity 0..100
573
+ */
574
+ public function place(self $image, int|string $left = 0, int|string $top = 0, int $opacity = 100): static
575
+ {
576
+ $opacity = max(0, min(100, $opacity));
577
+ if ($opacity === 0) {
578
+ return $this;
579
+ }
580
+
581
+ $width = $image->getWidth();
582
+ $height = $image->getHeight();
583
+
584
+ if (self::isPercent($left)) {
585
+ $left = (int) round(($this->getWidth() - $width) / 100 * $left);
586
+ }
587
+
588
+ if (self::isPercent($top)) {
589
+ $top = (int) round(($this->getHeight() - $height) / 100 * $top);
590
+ }
591
+
592
+ $output = $input = $image->image;
593
+ if ($opacity < 100) {
594
+ $tbl = [];
595
+ for ($i = 0; $i < 128; $i++) {
596
+ $tbl[$i] = round(127 - (127 - $i) * $opacity / 100);
597
+ }
598
+
599
+ $output = imagecreatetruecolor($width, $height);
600
+ imagealphablending($output, false);
601
+ if (!$image->isTrueColor()) {
602
+ $input = $output;
603
+ imagefilledrectangle($output, 0, 0, $width, $height, imagecolorallocatealpha($output, 0, 0, 0, 127));
604
+ imagecopy($output, $image->image, 0, 0, 0, 0, $width, $height);
605
+ }
606
+
607
+ for ($x = 0; $x < $width; $x++) {
608
+ for ($y = 0; $y < $height; $y++) {
609
+ $c = \imagecolorat($input, $x, $y);
610
+ $c = ($c & 0xFFFFFF) + ($tbl[$c >> 24] << 24);
611
+ \imagesetpixel($output, $x, $y, $c);
612
+ }
613
+ }
614
+
615
+ imagealphablending($output, true);
616
+ }
617
+
618
+ imagecopy(
619
+ $this->image,
620
+ $output,
621
+ $left,
622
+ $top,
623
+ 0,
624
+ 0,
625
+ $width,
626
+ $height,
627
+ );
628
+ return $this;
629
+ }
630
+
631
+
632
+ /**
633
+ * Calculates the bounding box for a TrueType text. Returns keys left, top, width and height.
634
+ */
635
+ public static function calculateTextBox(
636
+ string $text,
637
+ string $fontFile,
638
+ float $size,
639
+ float $angle = 0,
640
+ array $options = [],
641
+ ): array
642
+ {
643
+ $box = imagettfbbox($size, $angle, $fontFile, $text, $options);
644
+ return [
645
+ 'left' => $minX = min([$box[0], $box[2], $box[4], $box[6]]),
646
+ 'top' => $minY = min([$box[1], $box[3], $box[5], $box[7]]),
647
+ 'width' => max([$box[0], $box[2], $box[4], $box[6]]) - $minX + 1,
648
+ 'height' => max([$box[1], $box[3], $box[5], $box[7]]) - $minY + 1,
649
+ ];
650
+ }
651
+
652
+
653
+ /**
654
+ * Draw a rectangle.
655
+ */
656
+ public function rectangleWH(int $x, int $y, int $width, int $height, ImageColor $color): void
657
+ {
658
+ if ($width !== 0 && $height !== 0) {
659
+ $this->rectangle($x, $y, $x + $width + ($width > 0 ? -1 : 1), $y + $height + ($height > 0 ? -1 : 1), $color);
660
+ }
661
+ }
662
+
663
+
664
+ /**
665
+ * Draw a filled rectangle.
666
+ */
667
+ public function filledRectangleWH(int $x, int $y, int $width, int $height, ImageColor $color): void
668
+ {
669
+ if ($width !== 0 && $height !== 0) {
670
+ $this->filledRectangle($x, $y, $x + $width + ($width > 0 ? -1 : 1), $y + $height + ($height > 0 ? -1 : 1), $color);
671
+ }
672
+ }
673
+
674
+
675
+ /**
676
+ * Saves image to the file. Quality is in the range 0..100 for JPEG (default 85), WEBP (default 80) and AVIF (default 30) and 0..9 for PNG (default 9).
677
+ * @param ImageType::*|null $type
678
+ * @throws ImageException
679
+ */
680
+ public function save(string $file, ?int $quality = null, ?int $type = null): void
681
+ {
682
+ $type ??= self::extensionToType(pathinfo($file, PATHINFO_EXTENSION));
683
+ $this->output($type, $quality, $file);
684
+ }
685
+
686
+
687
+ /**
688
+ * Outputs image to string. Quality is in the range 0..100 for JPEG (default 85), WEBP (default 80) and AVIF (default 30) and 0..9 for PNG (default 9).
689
+ * @param ImageType::* $type
690
+ */
691
+ public function toString(int $type = ImageType::JPEG, ?int $quality = null): string
692
+ {
693
+ return Helpers::capture(function () use ($type, $quality): void {
694
+ $this->output($type, $quality);
695
+ });
696
+ }
697
+
698
+
699
+ /**
700
+ * Outputs image to string.
701
+ */
702
+ public function __toString(): string
703
+ {
704
+ return $this->toString();
705
+ }
706
+
707
+
708
+ /**
709
+ * Outputs image to browser. Quality is in the range 0..100 for JPEG (default 85), WEBP (default 80) and AVIF (default 30) and 0..9 for PNG (default 9).
710
+ * @param ImageType::* $type
711
+ * @throws ImageException
712
+ */
713
+ public function send(int $type = ImageType::JPEG, ?int $quality = null): void
714
+ {
715
+ header('Content-Type: ' . self::typeToMimeType($type));
716
+ $this->output($type, $quality);
717
+ }
718
+
719
+
720
+ /**
721
+ * Outputs image to browser or file.
722
+ * @param ImageType::* $type
723
+ * @throws ImageException
724
+ */
725
+ private function output(int $type, ?int $quality, ?string $file = null): void
726
+ {
727
+ switch ($type) {
728
+ case ImageType::JPEG:
729
+ $quality = $quality === null ? 85 : max(0, min(100, $quality));
730
+ $success = @imagejpeg($this->image, $file, $quality); // @ is escalated to exception
731
+ break;
732
+
733
+ case ImageType::PNG:
734
+ $quality = $quality === null ? 9 : max(0, min(9, $quality));
735
+ $success = @imagepng($this->image, $file, $quality); // @ is escalated to exception
736
+ break;
737
+
738
+ case ImageType::GIF:
739
+ $success = @imagegif($this->image, $file); // @ is escalated to exception
740
+ break;
741
+
742
+ case ImageType::WEBP:
743
+ $quality = $quality === null ? 80 : max(0, min(100, $quality));
744
+ $success = @imagewebp($this->image, $file, $quality); // @ is escalated to exception
745
+ break;
746
+
747
+ case ImageType::AVIF:
748
+ $quality = $quality === null ? 30 : max(0, min(100, $quality));
749
+ $success = @imageavif($this->image, $file, $quality); // @ is escalated to exception
750
+ break;
751
+
752
+ case ImageType::BMP:
753
+ $success = @imagebmp($this->image, $file); // @ is escalated to exception
754
+ break;
755
+
756
+ default:
757
+ throw new Nette\InvalidArgumentException("Unsupported image type '$type'.");
758
+ }
759
+
760
+ if (!$success) {
761
+ throw new ImageException(Helpers::getLastError() ?: 'Unknown error');
762
+ }
763
+ }
764
+
765
+
766
+ /**
767
+ * Call to undefined method.
768
+ * @throws Nette\MemberAccessException
769
+ */
770
+ public function __call(string $name, array $args): mixed
771
+ {
772
+ $function = 'image' . $name;
773
+ if (!function_exists($function)) {
774
+ ObjectHelpers::strictCall(static::class, $name);
775
+ }
776
+
777
+ foreach ($args as $key => $value) {
778
+ if ($value instanceof self) {
779
+ $args[$key] = $value->getImageResource();
780
+
781
+ } elseif ($value instanceof ImageColor || (is_array($value) && isset($value['red']))) {
782
+ $args[$key] = $this->resolveColor($value);
783
+ }
784
+ }
785
+
786
+ $res = $function($this->image, ...$args);
787
+ return $res instanceof \GdImage
788
+ ? $this->setImageResource($res)
789
+ : $res;
790
+ }
791
+
792
+
793
+ public function __clone()
794
+ {
795
+ ob_start(function () {});
796
+ imagepng($this->image, null, 0);
797
+ $this->setImageResource(imagecreatefromstring(ob_get_clean()));
798
+ }
799
+
800
+
801
+ private static function isPercent(int|string &$num): bool
802
+ {
803
+ if (is_string($num) && str_ends_with($num, '%')) {
804
+ $num = (float) substr($num, 0, -1);
805
+ return true;
806
+ } elseif (is_int($num) || $num === (string) (int) $num) {
807
+ $num = (int) $num;
808
+ return false;
809
+ }
810
+
811
+ throw new Nette\InvalidArgumentException("Expected dimension in int|string, '$num' given.");
812
+ }
813
+
814
+
815
+ /**
816
+ * Prevents serialization.
817
+ */
818
+ public function __sleep(): array
819
+ {
820
+ throw new Nette\NotSupportedException('You cannot serialize or unserialize ' . self::class . ' instances.');
821
+ }
822
+
823
+
824
+ public function resolveColor(ImageColor|array $color): int
825
+ {
826
+ $color = $color instanceof ImageColor ? $color->toRGBA() : array_values($color);
827
+ return imagecolorallocatealpha($this->image, ...$color) ?: imagecolorresolvealpha($this->image, ...$color);
828
+ }
829
+ }