Compare commits
2 Commits
c712fa07ba
...
ab3e69e831
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
ab3e69e831 | ||
|
|
18401b908f |
@@ -2,7 +2,6 @@
|
||||
|
||||
namespace App\Models\Ecommerce\Product\Product\Concerns;
|
||||
|
||||
use Illuminate\Support\Str;
|
||||
use Spatie\Image\Manipulations;
|
||||
use Spatie\MediaLibrary\MediaCollections\Models\Media;
|
||||
|
||||
@@ -22,24 +21,26 @@ trait ProductMedia
|
||||
*/
|
||||
public function registerMediaConversions(?Media $media = null): void
|
||||
{
|
||||
$conversions = [
|
||||
'thumb200x200' => [200, 200],
|
||||
'thumb400x400' => [400, 400],
|
||||
'thumb720x720' => [720, 720],
|
||||
'thumb800x800' => [800, 800],
|
||||
'thumb1200x1200' => [1200, 1200],
|
||||
'thumb288x431' => [288, 431],
|
||||
'thumb270x350' => [270, 350],
|
||||
];
|
||||
$this->addMediaConversion('thumb200x200')
|
||||
->fit(Manipulations::FIT_CONTAIN, 200, 200);
|
||||
|
||||
foreach ($conversions as $name => [$width, $height]) {
|
||||
$this->addMediaConversion($name)
|
||||
->fit(Manipulations::FIT_CONTAIN, $width, $height)
|
||||
->useFileName(function (Media $media) use ($name) {
|
||||
// generate short random name for the conversion
|
||||
return Str::random(12) . '--' . $name . '.' . $media->getClientOriginalExtension();
|
||||
});
|
||||
}
|
||||
$this->addMediaConversion('thumb400x400')
|
||||
->fit(Manipulations::FIT_CONTAIN, 400, 400);
|
||||
|
||||
$this->addMediaConversion('thumb720x720')
|
||||
->fit(Manipulations::FIT_CONTAIN, 720, 720);
|
||||
|
||||
$this->addMediaConversion('thumb800x800')
|
||||
->fit(Manipulations::FIT_CONTAIN, 800, 800);
|
||||
|
||||
$this->addMediaConversion('thumb1200x1200')
|
||||
->fit(Manipulations::FIT_CONTAIN, 1200, 1200);
|
||||
|
||||
$this->addMediaConversion('thumb288x431')
|
||||
->fit(Manipulations::FIT_CONTAIN, 288, 431);
|
||||
|
||||
$this->addMediaConversion('thumb270x350')
|
||||
->fit(Manipulations::FIT_CONTAIN, 270, 350);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
30
app/Support/ShortFileNamer.php
Normal file
30
app/Support/ShortFileNamer.php
Normal file
@@ -0,0 +1,30 @@
|
||||
<?php
|
||||
namespace App\Support;
|
||||
|
||||
use Spatie\MediaLibrary\Support\FileNamer\FileNamer;
|
||||
use Spatie\MediaLibrary\Conversions\Conversion;
|
||||
use Illuminate\Support\Str;
|
||||
|
||||
class ShortFileNamer extends FileNamer
|
||||
{
|
||||
/**
|
||||
* Keep the original file name exactly as it was uploaded.
|
||||
*/
|
||||
public function originalFileName(string $fileName): string
|
||||
{
|
||||
return pathinfo($fileName, PATHINFO_FILENAME);
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
public function conversionFileName(string $fileName, Conversion $conversion): string
|
||||
{
|
||||
return Str::random(12);
|
||||
}
|
||||
|
||||
public function responsiveFileName(string $fileName): string
|
||||
{
|
||||
return 'res';
|
||||
}
|
||||
}
|
||||
@@ -58,7 +58,7 @@ return [
|
||||
/*
|
||||
* This is the class that is responsible for naming generated files.
|
||||
*/
|
||||
'file_namer' => Spatie\MediaLibrary\Support\FileNamer\DefaultFileNamer::class,
|
||||
'file_namer' => App\Support\ShortFileNamer::class,
|
||||
|
||||
/*
|
||||
* The class that contains the strategy for determining a media file's path.
|
||||
|
||||
Reference in New Issue
Block a user