|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace Assets\ImageCreation\Intervention; |
| 6 | + |
| 7 | +use Assets\Error\InvalidArgumentException; |
| 8 | +use Assets\Error\ModificationFailedException; |
| 9 | +use Assets\ImageCreation\ImageInterface; |
| 10 | +use Intervention\Image\MediaType; |
| 11 | +use Intervention\Image\Interfaces as Intervention; |
| 12 | +use Nette\Utils\FileSystem; |
| 13 | + |
| 14 | +final class InterventionImageFacade implements ImageInterface |
| 15 | +{ |
| 16 | + /** |
| 17 | + * @param array<string, string|callable> $legacyMdifiersMap |
| 18 | + */ |
| 19 | + public function __construct( |
| 20 | + private Intervention\ImageInterface $interventionImage, |
| 21 | + private array $legacyMdifiersMap, |
| 22 | + ) { |
| 23 | + } |
| 24 | + |
| 25 | + public function width(): int |
| 26 | + { |
| 27 | + return $this->interventionImage->width(); |
| 28 | + } |
| 29 | + |
| 30 | + public function height(): int |
| 31 | + { |
| 32 | + return $this->interventionImage->height(); |
| 33 | + } |
| 34 | + |
| 35 | + /** |
| 36 | + * @see MediaType |
| 37 | + */ |
| 38 | + public function mime(): string |
| 39 | + { |
| 40 | + return $this->interventionImage->origin()->mediaType(); |
| 41 | + } |
| 42 | + |
| 43 | + public function save(string $absolutePath, ?int $quality, ?string $format): void |
| 44 | + { |
| 45 | + if ($format !== null && !str_ends_with($absolutePath, '.' . $format)) { |
| 46 | + $absolutePath .= '.' . $format; |
| 47 | + } |
| 48 | + |
| 49 | + $dir = dirname($absolutePath); |
| 50 | + |
| 51 | + if (!is_dir($dir)) { |
| 52 | + FileSystem::createDir($dir); |
| 53 | + } |
| 54 | + |
| 55 | + $this->interventionImage->save($absolutePath, quality: $quality); |
| 56 | + } |
| 57 | + |
| 58 | + public function modify(string $modifier, array $params): ImageInterface |
| 59 | + { |
| 60 | + if ($params === []) { |
| 61 | + throw new ModificationFailedException('Empty params given'); |
| 62 | + } |
| 63 | + |
| 64 | + $modify = function (string $modifier, array $params, ?string $legacyModifier = null) { |
| 65 | + try { |
| 66 | + $this->interventionImage->{$modifier}(...$params); |
| 67 | + } catch (\Throwable $throwable) { |
| 68 | + |
| 69 | + if ($legacyModifier === null) { |
| 70 | + $callback = $this->legacyMdifiersMap[$modifier] ?? null; |
| 71 | + if ($callback !== null && is_callable($callback)) { |
| 72 | + $callback($this->interventionImage, ...$params); |
| 73 | + return $this->interventionImage; |
| 74 | + } |
| 75 | + } |
| 76 | + |
| 77 | + throw new ModificationFailedException( |
| 78 | + sprintf( |
| 79 | + 'Modification `%s` failed with params: %s.%s', |
| 80 | + $modifier, |
| 81 | + var_export($params, true), |
| 82 | + $legacyModifier !== null ? ' Legacy: ' . $legacyModifier : '', |
| 83 | + ), |
| 84 | + previous: $throwable, |
| 85 | + ); |
| 86 | + } |
| 87 | + }; |
| 88 | + |
| 89 | + if (method_exists($this->interventionImage, $modifier)) { |
| 90 | + $modify($modifier, $params); |
| 91 | + return $this; |
| 92 | + } |
| 93 | + |
| 94 | + $mappedFromLegacy = $this->legacyMdifiersMap[$modifier] ?? null; |
| 95 | + |
| 96 | + if ( |
| 97 | + $mappedFromLegacy !== null |
| 98 | + && method_exists($this->interventionImage, $mappedFromLegacy) |
| 99 | + ) { |
| 100 | + $modify($mappedFromLegacy, $params, $modifier); |
| 101 | + return $this; |
| 102 | + } |
| 103 | + |
| 104 | + throw new ModificationFailedException(sprintf('Modifier `%s` does not exist', $modifier)); |
| 105 | + } |
| 106 | + |
| 107 | + public function getInterventionImage(): Intervention\ImageInterface |
| 108 | + { |
| 109 | + return $this->requireInterventionImage(); |
| 110 | + } |
| 111 | + |
| 112 | + public function requireInterventionImage(): Intervention\ImageInterface |
| 113 | + { |
| 114 | + return $this->interventionImage; |
| 115 | + } |
| 116 | +} |
0 commit comments