feat: add options to select aspect ratio and cron algo for product images

This commit is contained in:
2025-12-08 21:25:00 +03:00
parent bf674473e9
commit e9c6ed8ddf
19 changed files with 98 additions and 188 deletions

View File

@@ -7,7 +7,9 @@ return [
'app_icon' => null,
"theme_light" => "light",
"theme_dark" => "dark",
"app_debug" => false
"app_debug" => false,
'image_aspect_ratio' => '1:1',
'image_crop_algorithm' => 'cover',
],
'telegram' => [

View File

@@ -61,7 +61,12 @@ class ImageFactory
return $this;
}
public function resize(?int $width = null, ?int $height = null, $operation = 'resize'): self
public function crop(string $algorithm, ?int $width = null, ?int $height = null): self
{
return $this->{$algorithm}($width, $height);
}
public function resize(?int $width = null, ?int $height = null): self
{
$this->modifications['resize'] = [
'width' => $width,

View File

@@ -34,14 +34,12 @@ class ProductsHandler
$maxPages = (int) $request->json('maxPages', 10);
$search = trim($request->get('search', ''));
$filters = $request->json('filters');
$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,
$aspectRatio,
);
return new JsonResponse($response);

View File

@@ -7,7 +7,6 @@ use Exception;
use GuzzleHttp\Exception\ClientException;
use Openguru\OpenCartFramework\Http\Request;
use Openguru\OpenCartFramework\ImageTool\ImageFactory;
use Openguru\OpenCartFramework\Router\Router;
use Openguru\OpenCartFramework\Telegram\TelegramService;
use Symfony\Component\HttpFoundation\JsonResponse;
@@ -15,18 +14,15 @@ class SettingsHandler
{
private SettingsService $settings;
private ImageFactory $image;
private Router $router;
private TelegramService $telegramService;
public function __construct(
SettingsService $settings,
ImageFactory $image,
Router $router,
TelegramService $telegramService
) {
$this->settings = $settings;
$this->image = $image;
$this->router = $router;
$this->telegramService = $telegramService;
}
@@ -36,15 +32,8 @@ class SettingsHandler
$appIcon = $appConfig->getAppIcon();
$hash = $this->settings->getHash();
$icons = [];
if ($appIcon) {
$icons['icon192'] = $this->image->make($appIcon)->resize(192, 192)->url('png') . '?_v=' . $hash;
$icons['icon180'] = $this->image->make($appIcon)->resize(180, 180)->url('png') . '?_v=' . $hash;
;
$icons['icon152'] = $this->image->make($appIcon)->resize(152, 152)->url('png') . '?_v=' . $hash;
;
$icons['icon120'] = $this->image->make($appIcon)->resize(120, 120)->url('png') . '?_v=' . $hash;
$appIcon = $this->image->make($appIcon)->resize(null, 64)->url('png') . '?_v=' . $hash;
}
@@ -52,11 +41,6 @@ class SettingsHandler
'app_name' => $appConfig->getAppName(),
'app_debug' => $appConfig->isAppDebug(),
'app_icon' => $appIcon,
'app_icon192' => $icons['icon192'] ?? '',
'app_icon180' => $icons['icon180'] ?? '',
'app_icon152' => $icons['icon152'] ?? '',
'app_icon120' => $icons['icon120'] ?? '',
'manifest_url' => $this->router->url('manifest', ['_v' => $hash]),
'theme_light' => $appConfig->getThemeLight(),
'theme_dark' => $appConfig->getThemeDark(),
'ya_metrika_enabled' => $this->settings->config()->getMetrics()->isYandexMetrikaEnabled(),
@@ -68,42 +52,10 @@ class SettingsHandler
'texts' => $this->settings->config()->getTexts()->toArray(),
'mainpage_blocks' => $this->settings->get('mainpage_blocks', []),
'privacy_policy_link' => $this->settings->get('app.privacy_policy_link'),
'image_aspect_ratio' => $this->settings->get('app.image_aspect_ratio', '1:1'),
]);
}
public function manifest(): JsonResponse
{
$manifest = [
'name' => $this->settings->config()->getApp()->getAppName(),
'short_name' => $this->settings->config()->getApp()->getAppName(),
'start_url' => '/image/catalog/tgshopspa/',
'display' => 'standalone',
'background_color' => '#ffffff',
'theme_color' => '#000000',
'orientation' => 'portrait',
];
$appIcon = $this->settings->config()->getApp()->getAppIcon();
if ($appIcon) {
$icon192 = $this->image->make($appIcon)->resize(192, 192)->url('png');
$icon512 = $this->image->make($appIcon)->resize(512, 512)->url('png');
$manifest['icons'] = [
[
'src' => $icon192,
'sizes' => '192x192',
'type' => 'image/png',
],
[
'src' => $icon512,
'sizes' => '512x512',
'type' => 'image/png',
]
];
}
return new JsonResponse($manifest);
}
public function testTgMessage(Request $request): JsonResponse
{
$template = $request->json('template', 'Нет шаблона');

View File

@@ -6,7 +6,6 @@ use Openguru\OpenCartFramework\Cache\CacheInterface;
use Openguru\OpenCartFramework\ImageTool\ImageFactory;
use Openguru\OpenCartFramework\QueryBuilder\Builder;
use Openguru\OpenCartFramework\QueryBuilder\JoinClause;
use Openguru\OpenCartFramework\Support\Arr;
use RuntimeException;
class BlocksService
@@ -142,9 +141,7 @@ class BlocksService
],
];
$aspectRatio = Arr::get($block, 'data.image_aspect_ratio', '1:1');
$response = $this->productsService->getProductsResponse($params, $languageId, $aspectRatio);
$response = $this->productsService->getProductsResponse($params, $languageId);
$block['data']['products'] = $response;

View File

@@ -58,7 +58,7 @@ class ProductsService
/**
* @throws ImageNotFoundException
*/
public function getProductsResponse(array $params, int $languageId, string $aspectRatio): array
public function getProductsResponse(array $params, int $languageId): array
{
$page = $params['page'];
$perPage = $params['perPage'];
@@ -67,6 +67,9 @@ class ProductsService
$maxPages = $params['maxPages'] ?? 50;
$filters = $params['filters'] ?? [];
$aspectRatio = $this->settings->get('app.image_aspect_ratio', '1:1');
$cropAlgorithm = $this->settings->get('app.image_crop_algorithm', 'cover');
[$imageWidth, $imageHeight] = ImageUtils::aspectRatioToSize($aspectRatio);
$customerGroupId = $this->settings->config()->getOrders()->getOcCustomerGroupId();
@@ -152,13 +155,13 @@ class ProductsService
$productId = $item['product_id'];
// Ограничиваем количество картинок для каждого товара до 3
if (!isset($productsImagesMap[$productId])) {
if (! isset($productsImagesMap[$productId])) {
$productsImagesMap[$productId] = [];
}
if (count($productsImagesMap[$productId]) < 2) {
$productsImagesMap[$productId][] = [
'url' => $this->image->make($item['image'])->cover($imageWidth, $imageHeight)->url(),
'url' => $this->image->make($item['image'])->crop($cropAlgorithm, $imageWidth, $imageHeight)->url(),
'alt' => 'Product Image',
];
}
@@ -172,54 +175,60 @@ class ProductsService
}
return [
'data' => array_map(function ($product) use ($productsImagesMap, $imageWidth, $imageHeight, $currency) {
$allImages = [];
'data' => array_map(
function ($product) use ($productsImagesMap, $cropAlgorithm, $imageWidth, $imageHeight, $currency) {
$allImages = [];
$image = $this->image->make($product['product_image'])
->resize($imageWidth, $imageHeight)
->url();
$image = $this->image->make($product['product_image'], false)
->crop($cropAlgorithm, $imageWidth, $imageHeight)
->url();
$allImages[] = [
'url' => $image,
'alt' => Str::htmlEntityEncode($product['product_name']),
];
$allImages[] = [
'url' => $image,
'alt' => Str::htmlEntityEncode($product['product_name']),
];
$price = $this->priceCalculator->format($product['price'], $product['tax_class_id']);
$priceNumeric = $this->priceCalculator->getPriceNumeric($product['price'], $product['tax_class_id']);
$special = false;
$specialPriceNumeric = null;
if ($product['special'] && (float) $product['special'] >= 0) {
$specialPriceNumeric = $this->tax->calculate(
$product['special'],
$product['tax_class_id'],
$this->settings->config()->getStore()->isOcConfigTax(),
$price = $this->priceCalculator->format($product['price'], $product['tax_class_id']);
$priceNumeric = $this->priceCalculator->getPriceNumeric(
$product['price'],
$product['tax_class_id']
);
$special = $this->currency->format(
$specialPriceNumeric,
$currency,
);
}
if (! empty($productsImagesMap[$product['product_id']])) {
$allImages = array_merge($allImages, $productsImagesMap[$product['product_id']]);
}
$special = false;
$specialPriceNumeric = null;
if ($product['special'] && (float) $product['special'] >= 0) {
$specialPriceNumeric = $this->tax->calculate(
$product['special'],
$product['tax_class_id'],
$this->settings->config()->getStore()->isOcConfigTax(),
);
$special = $this->currency->format(
$specialPriceNumeric,
$currency,
);
}
return [
'id' => (int) $product['product_id'],
'product_quantity' => (int) $product['product_quantity'],
'name' => Str::htmlEntityEncode($product['product_name']),
'price' => $price,
'special' => $special,
'image' => $image,
'images' => $allImages,
'special_numeric' => $specialPriceNumeric,
'price_numeric' => $priceNumeric,
'final_price_numeric' => $specialPriceNumeric ?: $priceNumeric,
'manufacturer_name' => $product['manufacturer_name'],
'category_name' => $product['category_name'],
];
}, $products),
if (! empty($productsImagesMap[$product['product_id']])) {
$allImages = array_merge($allImages, $productsImagesMap[$product['product_id']]);
}
return [
'id' => (int) $product['product_id'],
'product_quantity' => (int) $product['product_quantity'],
'name' => Str::htmlEntityEncode($product['product_name']),
'price' => $price,
'special' => $special,
'image' => $image,
'images' => $allImages,
'special_numeric' => $specialPriceNumeric,
'price_numeric' => $priceNumeric,
'final_price_numeric' => $specialPriceNumeric ?: $priceNumeric,
'manufacturer_name' => $product['manufacturer_name'],
'category_name' => $product['category_name'],
];
},
$products
),
'meta' => [
'currentCategoryName' => $categoryName,

View File

@@ -25,7 +25,6 @@ return [
'health' => [HealthCheckHandler::class, 'handle'],
'ingest' => [TelemetryHandler::class, 'ingest'],
'heartbeat' => [TelemetryHandler::class, 'heartbeat'],
'manifest' => [SettingsHandler::class, 'manifest'],
'processBlock' => [BlocksHandler::class, 'processBlock'],
'product_show' => [ProductsHandler::class, 'show'],
'products' => [ProductsHandler::class, 'index'],