From 13e5bce8a548439da3dcd892b0c5600ffc995be6 Mon Sep 17 00:00:00 2001 From: Nikita Kiselev Date: Sat, 6 Dec 2025 13:21:02 +0300 Subject: [PATCH] feat: better algorythm for image resize --- .../framework/ImageTool/ImageFactory.php | 32 +++++++++++++++++-- .../src/Handlers/TelemetryHandler.php | 4 +-- .../src/Services/ProductsService.php | 2 +- 3 files changed, 32 insertions(+), 6 deletions(-) diff --git a/module/oc_telegram_shop/upload/oc_telegram_shop/framework/ImageTool/ImageFactory.php b/module/oc_telegram_shop/upload/oc_telegram_shop/framework/ImageTool/ImageFactory.php index f9ef8b6..0319b5e 100644 --- a/module/oc_telegram_shop/upload/oc_telegram_shop/framework/ImageTool/ImageFactory.php +++ b/module/oc_telegram_shop/upload/oc_telegram_shop/framework/ImageTool/ImageFactory.php @@ -13,6 +13,12 @@ class ImageFactory private string $fullPath; private array $modifications = []; + private static array $modificatorMethods = [ + 'resize' => [self::class, 'resizeModification'], + 'cover' => [self::class, 'coverModification'], + 'contain' => [self::class, 'containModification'], + ]; + private string $imageDir; private string $siteUrl; private ImageManager $manager; @@ -75,6 +81,17 @@ class ImageFactory return $this; } + public function contain(?int $width = null, ?int $height = null, $background = 'ffffff'): self + { + $this->modifications['contain'] = [ + 'width' => $width, + 'height' => $height, + 'background' => $background, + ]; + + return $this; + } + public function url(?string $format = null, ?int $quality = null): string { $format = $format ?? $this->options['format']; @@ -100,6 +117,7 @@ class ImageFactory private function buildUrl(string $path): string { $relativePath = substr($path, strlen($this->imageDir)); + return $this->siteUrl . '/image/' . ltrim($relativePath, '/'); } @@ -161,9 +179,9 @@ class ImageFactory private function applyModifications(): void { foreach ($this->modifications as $name => $value) { - $method = "{$name}Modification"; - if (! method_exists($this, $method)) { - throw new RuntimeException('Modification method ' . $method . ' does not exist'); + $method = self::$modificatorMethods[$name][1] ?? null; + if (! $method) { + throw new RuntimeException('Modification for ' . $name . ' does not exist'); } $this->{$method}(...array_values($value)); } @@ -183,6 +201,14 @@ class ImageFactory } } + private function containModification(?int $width = null, ?int $height = null, $background = 'ffffff'): void + { + if ($width || $height) { + $this->image->resize($width, $height, static fn($constraint) => $constraint->aspectRatio()); + $this->image->resizeCanvas($width, $height, 'center', false, $background); + } + } + private function ensureDestinationFolterExists(string $newImage): void { $path = pathinfo($newImage, PATHINFO_DIRNAME); diff --git a/module/oc_telegram_shop/upload/oc_telegram_shop/src/Handlers/TelemetryHandler.php b/module/oc_telegram_shop/upload/oc_telegram_shop/src/Handlers/TelemetryHandler.php index 2657d1b..1167822 100755 --- a/module/oc_telegram_shop/upload/oc_telegram_shop/src/Handlers/TelemetryHandler.php +++ b/module/oc_telegram_shop/upload/oc_telegram_shop/src/Handlers/TelemetryHandler.php @@ -4,12 +4,12 @@ declare(strict_types=1); namespace App\Handlers; -use Symfony\Component\HttpFoundation\JsonResponse; use Openguru\OpenCartFramework\Http\Request; -use Symfony\Component\HttpFoundation\Response; use Openguru\OpenCartFramework\TeleCartPulse\PulseIngestException; use Openguru\OpenCartFramework\TeleCartPulse\TeleCartPulseService; use Psr\Log\LoggerInterface; +use Symfony\Component\HttpFoundation\JsonResponse; +use Symfony\Component\HttpFoundation\Response; use Throwable; class TelemetryHandler diff --git a/module/oc_telegram_shop/upload/oc_telegram_shop/src/Services/ProductsService.php b/module/oc_telegram_shop/upload/oc_telegram_shop/src/Services/ProductsService.php index a930abf..70092bd 100755 --- a/module/oc_telegram_shop/upload/oc_telegram_shop/src/Services/ProductsService.php +++ b/module/oc_telegram_shop/upload/oc_telegram_shop/src/Services/ProductsService.php @@ -145,7 +145,7 @@ class ProductsService $productsImagesMap = []; foreach ($productsImages as $item) { $productsImagesMap[$item['product_id']][] = [ - 'url' => $this->image->make($item['image'])->resize($imageWidth, $imageHeight)->url(), + 'url' => $this->image->make($item['image'])->contain($imageWidth, $imageHeight)->url(), 'alt' => 'Product Image', ]; }