feat: add aspect ratio selector for products_carousel

This commit is contained in:
2025-12-06 18:14:07 +03:00
parent 1fbbb7b6db
commit 615e8c54a6
8 changed files with 96 additions and 54 deletions

View File

@@ -0,0 +1,18 @@
<?php
namespace Openguru\OpenCartFramework\ImageTool;
class ImageUtils
{
private static array $aspectRatiosMap = [
'1:1' => [400, 400],
'4:5' => [400, 500],
'3:4' => [400, 533],
'2:3' => [400, 600],
];
public static function aspectRatioToSize(string $aspectRatio, array $default = [400, 400]): array
{
return self::$aspectRatiosMap[$aspectRatio] ?? $default;
}
}

View File

@@ -34,16 +34,14 @@ class ProductsHandler
$maxPages = (int) $request->json('maxPages', 10);
$search = trim($request->get('search', ''));
$filters = $request->json('filters');
$width = (int) $request->json('width', 300);
$height = (int) $request->json('height', 300);
$aspectRatio = $request->json('image_aspect_ratio', '1:1');
$languageId = $this->settings->config()->getApp()->getLanguageId();
$response = $this->productsService->getProductsResponse(
compact('page', 'perPage', 'search', 'filters', 'maxPages'),
$languageId,
$width,
$height,
$aspectRatio,
);
return new JsonResponse($response);

View File

@@ -18,13 +18,6 @@ class BlocksService
'products_carousel' => [self::class, 'processProductsCarousel'],
];
private static array $aspectRatiosMap = [
'1:1' => [400, 400],
'4:5' => [400, 500],
'3:4' => [400, 533],
'2:3' => [400, 600],
];
private ImageFactory $image;
private CacheInterface $cache;
private SettingsService $settings;
@@ -121,11 +114,6 @@ class BlocksService
private function processProductsFeed(array $block): array
{
[$width, $height] = $this->aspectRatioToSize(Arr::get($block, 'data.image_aspect_ratio', '1:1'));
Arr::set($block, 'data.image_width', $width);
Arr::set($block, 'data.image_height', $height);
return $block;
}
@@ -154,15 +142,12 @@ class BlocksService
],
];
$response = $this->productsService->getProductsResponse($params, $languageId);
$aspectRatio = Arr::get($block, 'data.image_aspect_ratio', '1:1');
$response = $this->productsService->getProductsResponse($params, $languageId, $aspectRatio);
$block['data']['products'] = $response;
return $block;
}
private function aspectRatioToSize($aspectRatio): array
{
return self::$aspectRatiosMap[$aspectRatio] ?? [400, 400];
}
}

View File

@@ -9,6 +9,7 @@ use Openguru\OpenCartFramework\CriteriaBuilder\CriteriaBuilder;
use Openguru\OpenCartFramework\Exceptions\EntityNotFoundException;
use Openguru\OpenCartFramework\ImageTool\ImageFactory;
use Openguru\OpenCartFramework\ImageTool\ImageNotFoundException;
use Openguru\OpenCartFramework\ImageTool\ImageUtils;
use Openguru\OpenCartFramework\OpenCart\Decorators\OcRegistryDecorator;
use Openguru\OpenCartFramework\OpenCart\PriceCalculator;
use Openguru\OpenCartFramework\QueryBuilder\Builder;
@@ -57,12 +58,8 @@ class ProductsService
/**
* @throws ImageNotFoundException
*/
public function getProductsResponse(
array $params,
int $languageId,
int $imageWidth = 300,
int $imageHeight = 300
): array {
public function getProductsResponse(array $params, int $languageId, string $aspectRatio): array
{
$page = $params['page'];
$perPage = $params['perPage'];
$search = $params['search'] ?? false;
@@ -70,8 +67,7 @@ class ProductsService
$maxPages = $params['maxPages'] ?? 50;
$filters = $params['filters'] ?? [];
$imageWidth = $imageWidth ?: 300;
$imageHeight = $imageHeight ?: 300;
[$imageWidth, $imageHeight] = ImageUtils::aspectRatioToSize($aspectRatio);
$customerGroupId = $this->settings->config()->getOrders()->getOcCustomerGroupId();
$currency = $this->settings->config()->getStore()->getOcDefaultCurrency();