EshopAssetTrait
Shared image logic of the Eshop Api managers: the main photo from `$meta['image']` and the variant config builders.
Two variant families, both read from the image variants table and sized in the admin (Eshop > Settings > Images):
- product (productImageVariantConfigs()):
Product(main photo) andProductSet(gallery), each under its own usage (eshop-products,eshop-product-sets); - static (staticImageVariantConfigs()): category and parameter.
Requires AssetProcessingTrait (saveAssetVariants(),
getAssetDir*(), getAssetSubDir(), getAssetVariantType()) and $this->variantProvider.
Table of Contents
Constants
- ESHOP_SETTINGS_MODULE : int = 4
- Eshop module index (image directories `assetDir(4, ...)` in managers using this trait).
Methods
- buildVariantConfigs() : array<string, array{maxWidth: int, maxHeight: int, resizeFlags: int[], useWatermark: bool}>
- processMainImage() : Result|null
- Processes the main photo from `$meta` (sent by the admin form).
- productImageVariantConfigs() : array<string, array{maxWidth: int, maxHeight: int, resizeFlags: int[], useWatermark: bool}>
- Variants of product images, by the usage from `getAssetVariantType()` (`eshop-products` or `eshop-product-sets`) in the image variants table; product and set each have their own.
- staticImageVariantConfigs() : array<string, array{maxWidth: int, maxHeight: int, resizeFlags: int[], useWatermark: bool}>
- Variants of category and parameter images, also by the usage from `getAssetVariantType()`.
- storeImage() : Asset|null
- Stores an uploaded image as an `Asset` (variants) under `.../<parentId>/<variant>/`.
Constants
ESHOP_SETTINGS_MODULE
Eshop module index (image directories `assetDir(4, ...)` in managers using this trait).
private
int
ESHOP_SETTINGS_MODULE
= 4
Methods
buildVariantConfigs()
protected
abstract buildVariantConfigs() : array<string, array{maxWidth: int, maxHeight: int, resizeFlags: int[], useWatermark: bool}>
Return values
array<string, array{maxWidth: int, maxHeight: int, resizeFlags: int[], useWatermark: bool}>processMainImage()
Processes the main photo from `$meta` (sent by the admin form).
protected
processMainImage(int $parentId, array<string, mixed> $meta[, int|object|null $currentAsset = null ]) : Result|null
image=['name', 'data' (base64)]builds anAsset(variants) and sets theassetFK.imageDelete= true (without a new image) clears the FK.- A missing key leaves the image unchanged.
IMPORTANT: the caller must pass $currentAsset (the image the FK points at now, entity or
id); the trait has only $parentId. Without it the old Asset is not released after
a replace or removal and stays orphaned.
Parameters
- $parentId : int
- $meta : array<string, mixed>
- $currentAsset : int|object|null = null
Return values
Result|nullproductImageVariantConfigs()
Variants of product images, by the usage from `getAssetVariantType()` (`eshop-products` or `eshop-product-sets`) in the image variants table; product and set each have their own.
protected
productImageVariantConfigs() : array<string, array{maxWidth: int, maxHeight: int, resizeFlags: int[], useWatermark: bool}>
Return values
array<string, array{maxWidth: int, maxHeight: int, resizeFlags: int[], useWatermark: bool}>staticImageVariantConfigs()
Variants of category and parameter images, also by the usage from `getAssetVariantType()`.
protected
staticImageVariantConfigs() : array<string, array{maxWidth: int, maxHeight: int, resizeFlags: int[], useWatermark: bool}>
Return values
array<string, array{maxWidth: int, maxHeight: int, resizeFlags: int[], useWatermark: bool}>storeImage()
Stores an uploaded image as an `Asset` (variants) under `.../<parentId>/<variant>/`.
protected
storeImage(int $parentId, array<string|int, mixed> $item, array<string, array{maxWidth: int, maxHeight: int, resizeFlags: int[], useWatermark: bool}> $variantConfigs[, bool $purgeDir = false ][, array<int, string>|null &$stalePaths = null ]) : Asset|null
$purgeDir = true for a single main image (category/product): old files in the target
directory are listed in the output $stalePaths after a successful replace, but this method
never deletes them. The caller deletes them only after the FK write to the new Asset
succeeds, otherwise a failure would leave the DB pointing at missing files. Name collisions
cannot happen: saveAssetVariants() generates a random fileName.
IMPORTANT: for a gallery (several images per parent) $purgeDir must be false, otherwise
saving the second image would mark the first one's files as stale.
Parameters
- $parentId : int
- $item : array<string|int, mixed>
- $variantConfigs : array<string, array{maxWidth: int, maxHeight: int, resizeFlags: int[], useWatermark: bool}>
- $purgeDir : bool = false
- $stalePaths : array<int, string>|null = null
-
output parameter: absolute paths of files that were in the directory before the write