Files
3dealer/app/Catalog/ProductMediaDefinitions.php
T

43 lines
1.5 KiB
PHP
Raw Normal View History

<?php
namespace App\Catalog;
use Lunar\Base\StandardMediaDefinitions;
use Spatie\Image\Enums\Constraint;
use Spatie\MediaLibrary\HasMedia;
use Spatie\MediaLibrary\MediaCollections\MediaCollection;
use Spatie\MediaLibrary\MediaCollections\Models\Media;
/**
* INTERIM — belongs in boboko-core (see the core task in the image
* performance discussion). Delete this class and point
* config/lunar/media.php back at core's definitions once it lands there.
*
* Storefront product image sizes for srcset: WebP at fixed widths, the
* original's aspect ratio preserved and never upscaled. Replaces Lunar's
* square white-padded zoom/large/medium conversions (nothing reads them);
* the parent's `small` conversion stays for the admin panel.
*/
class ProductMediaDefinitions extends StandardMediaDefinitions
{
/** Conversion name => width in px. Named `w{width}` so the srcset can be built from the name. */
public const WIDTHS = [
'w160' => 160,
'w400' => 400,
'w800' => 800,
'w1200' => 1200,
'w1600' => 1600,
];
protected function registerCollectionConversions(MediaCollection $collection, HasMedia $model): void
{
$collection->registerMediaConversions(function (Media $media) use ($model) {
foreach (self::WIDTHS as $name => $width) {
$model->addMediaConversion($name)
->width($width, [Constraint::PreserveAspectRatio, Constraint::DoNotUpsize])
->format('webp')
->quality(80);
}
});
}
}