@@ -49,7 +49,7 @@ Here is how our `Channel` class will be defined:
49
49
``` php
50
50
namespace App\Models;
51
51
52
- use Elegantly\Media\Traits \HasMedia;
52
+ use Elegantly\Media\Concerns \HasMedia;
53
53
use Elegantly\Media\MediaCollection;
54
54
use Elegantly\Media\MediaConversion;
55
55
use Elegantly\Media\Enums\MediaType;
@@ -60,7 +60,8 @@ use \App\Jobs\Media\OptimizedImageConversionJob;
60
60
use Elegantly\Media\Models\Media;
61
61
use Elegantly\Media\Contracts\InteractWithMedia;
62
62
use Illuminate\Contracts\Support\Arrayable;
63
- use Elegantly\Media\Support\ResponsiveImagesConversionsPreset;
63
+ use Elegantly\Media\Definitions\MediaConversionImage;
64
+ use Elegantly\Media\Definitions\MediaConversionPoster;
64
65
65
66
class Channel extends Model implements InteractWithMedia
66
67
{
@@ -74,69 +75,38 @@ class Channel extends Model implements InteractWithMedia
74
75
acceptedMimeTypes: [
75
76
'image/jpeg', 'image/png', 'image/gif', 'image/webp',
76
77
],
78
+ transform: function($file){
79
+ Image::load($file->getRealPath())
80
+ ->fit(Fit::Crop, 500, 500)
81
+ ->optimize()
82
+ ->save();
83
+ },
84
+ conversions: [
85
+ new MediaConversionImage(
86
+ name: '360',
87
+ width: 360
88
+ ),
89
+ ],
77
90
)
78
91
new MediaCollection(
79
92
name: 'videos',
80
93
acceptedMimeTypes: [
81
94
'video/mp4', 'video/webm', 'video/ogg', 'video/quicktime',
82
95
],
96
+ conversions: [
97
+ new MediaConversionPoster(
98
+ name: 'poster',
99
+ conversions: [
100
+ new MediaConversionImage(
101
+ name: '360',
102
+ width: 360
103
+ ),
104
+ ],
105
+ ),
106
+ ],
83
107
)
84
108
];
85
109
}
86
-
87
- public function registerMediaTransformations($media, UploadedFile|File $file): UploadedFile|File
88
- {
89
- if($media->collection_name === "avatar"){
90
- Image::load($file->getRealPath())
91
- ->fit(Fit::Crop, 500, 500)
92
- ->optimize()
93
- ->save();
94
- }
95
-
96
- return $file;
97
- }
98
-
99
- public function registerMediaConversions($media): Arrayable|iterable|null;
100
- {
101
-
102
- if($media->collection_name === 'avatar'){
103
- return [
104
- new MediaConversion(
105
- conversionName: '360',
106
- job: new OptimizedImageConversionJob(
107
- media: $media,
108
- width: 360,
109
- fileName: "{$media->name}-360.jpg"
110
- ),
111
- )
112
- ]
113
- }elseif($media->collection_name === 'videos'){
114
- return [
115
- new MediaConversion(
116
- conversionName: 'poster',
117
- sync: true,// The conversion will not be queued, you will have access to it immediatly
118
- job: new VideoPosterConversionJob(
119
- media: $media,
120
- seconds: 1,
121
- fileName: "{$media->name}-poster.jpg"
122
- ),
123
- conversions: function(GeneratedConversion $generatedConversion) use ($media){
124
- return ResponsiveImagesConversionsPreset::make(
125
- media: $media,
126
- generatedConversion: $generatedConversion
127
- widths: [360, 720]
128
- )
129
- }
130
- ),
131
- ...ResponsiveVideosConversionsPreset::make(
132
- media: $media,
133
- widths: [360, 720, 1080],
134
- )
135
- ]
136
- }
137
-
138
- return null;
139
- }
140
110
}
141
111
```
142
112
@@ -155,7 +125,7 @@ class ChannelAvatarController extends Controller
155
125
{
156
126
$channel->addMedia(
157
127
file: $file->file('avatar'),
158
- collection_name : 'avatar',
128
+ collectionName : 'avatar',
159
129
name: "{$channel->name}-avatar",
160
130
)
161
131
}
@@ -182,7 +152,7 @@ class ImageUploader extends Component
182
152
183
153
$this->channel->addMedia(
184
154
file: $file->getRealPath(),
185
- collection_name : 'avatar',
155
+ collectionName : 'avatar',
186
156
name: "{$channel->name}-avatar",
187
157
)
188
158
}
@@ -223,6 +193,12 @@ return [
223
193
*/
224
194
'model' => Media::class,
225
195
196
+ /**
197
+ * The path used to store temporary file copy for conversions
198
+ * This will be used with storage_path() function
199
+ */
200
+ 'temporary_storage_path' => 'app/tmp/media',
201
+
226
202
/**
227
203
* The default disk used for storing files
228
204
*/
@@ -301,7 +277,7 @@ First, you need to add the `HasMedia` trait and the `InteractWithMedia` interfac
301
277
``` php
302
278
namespace App\Models;
303
279
304
- use Elegantly\Media\Traits \HasMedia;
280
+ use Elegantly\Media\Concerns \HasMedia;
305
281
use Illuminate\Database\Eloquent\Model;
306
282
use Elegantly\Media\Contracts\InteractWithMedia;
307
283
@@ -317,7 +293,7 @@ You can then define your media collections in the `registerMediaCollections` met
317
293
``` php
318
294
namespace App\Models;
319
295
320
- use Elegantly\Media\Traits \HasMedia;
296
+ use Elegantly\Media\Concerns \HasMedia;
321
297
use Elegantly\Media\MediaCollection;
322
298
use Illuminate\Database\Eloquent\Model;
323
299
use Elegantly\Media\Contracts\InteractWithMedia;
@@ -351,16 +327,14 @@ class Channel extends Model implements InteractWithMedia
351
327
352
328
This package provides common jobs for your conversions to simplify your work:
353
329
354
- - ` VideoPosterConversionJob ` : This job extracts a poster using ` pbmedia/laravel-ffmpeg ` .
355
- - ` OptimizedVideoConversionJob ` : This job optimizes, resizes, or converts any video using ` pbmedia/laravel-ffmpeg ` .
356
- - ` OptimizedImageConversionJob ` : This job optimizes, resizes, or converts any image using ` spatie/image ` .
357
- - ` ResponsiveImagesConversionsPreset ` : This preset creates a set of optimized images of different sizes.
358
- - ` ResponsiveVideosConversionsPreset ` : This preset creates a set of optimized videos of different sizes.
330
+ - ` MediaConversionImage ` : This job optimizes, resizes, or converts any image using ` spatie/image ` .
331
+ - ` MediaConversionVideo ` : This job optimizes, resizes, or converts any video using ` pbmedia/laravel-ffmpeg ` .
332
+ - ` MediaConversionPoster ` : This job extracts a poster using ` pbmedia/laravel-ffmpeg ` .
359
333
360
334
``` php
361
335
namespace App\Models;
362
336
363
- use Elegantly\Media\Traits \HasMedia;
337
+ use Elegantly\Media\Concerns \HasMedia;
364
338
use Elegantly\Media\MediaCollection;
365
339
use Elegantly\Media\MediaConversion;
366
340
use Elegantly\Media\Enums\MediaType;
@@ -377,111 +351,54 @@ class Channel extends Model implements InteractWithMedia
377
351
{
378
352
use HasMedia;
379
353
380
- // ...
381
-
382
- public function registerMediaConversions($media): Arrayable|iterable|null;
354
+ public function registerMediaCollections(): Arrayable|iterable|null;
383
355
{
384
-
385
- if($media->collection_name === 'avatar'){
386
- return [
387
- new MediaConversion(
388
- conversionName: '360',
389
- job: new OptimizedImageConversionJob(
390
- media: $media,
356
+ return [
357
+ new MediaCollection(
358
+ name: 'avatar',
359
+ acceptedMimeTypes: [
360
+ 'image/jpeg', 'image/png', 'image/gif', 'image/webp',
361
+ ],
362
+ conversions: [
363
+ // using preset conversion definition
364
+ new MediaConversionImage(
365
+ name: '360',
391
366
width: 360,
392
- fileName: "{$media->name}-360.jpg"
393
- ),
394
- )
395
- ]
396
- }elseif($media->collection_name === 'videos'){
397
- return [
398
- new MediaConversion(
399
- conversionName: 'poster',
400
- sync: true, // The conversion will not be queued, you will have access to it immediatly
401
- job: new VideoPosterConversionJob(
402
- media: $media,
403
- seconds: 1,
404
- fileName: "{$media->name}-poster.jpg"
405
367
),
406
- conversions: function(GeneratedConversion $generatedConversion) use ($media){
407
- return ResponsiveImagesConversionsPreset::make(
408
- media: $media,
409
- generatedConversion: $generatedConversion
410
- widths: [360, 720]
411
- )
412
- }
413
- ),
414
- ...ResponsiveVideosConversionsPreset::make(
415
- media: $media,
416
- widths: [360, 720, 1080],
417
- )
418
- ]
419
- }
420
-
421
- return null;
422
- }
423
- }
424
- ```
425
-
426
- ### Defining Your Own MediaConversion
427
-
428
- You can create your own conversion by creating a new class in your app (e.g., ` App\Support\MediaConversions ` ) and extending ` MediaConversionJob ` .
429
-
430
- Media conversions are executed through Laravel Jobs. You can perform any task in the job, provided that:
431
-
432
- - Your job extends ` Elegantly\Media\Jobs\MediaConversion ` .
433
- - Your job defines a ` run ` method.
434
- - Your job calls ` $this->media->storeConversion(...) ` .
435
-
436
- Let's consider a common media conversion task: optimizing an image. Here's how you could implement it in your app:
437
-
438
- > ** Note:** The following job is already provided by this package, but it serves as an excellent introduction to the concept.
439
-
440
- ``` php
441
- namespace App\Support\MediaConversions;
442
-
443
- use Elegantly\Media\Models\Media;
444
- use Illuminate\Support\Facades\File;
445
- use Spatie\Image\Enums\Fit;
446
- use Spatie\Image\Image;
447
- use Spatie\ImageOptimizer\OptimizerChain;
448
- use Elegantly\Media\Jobs\MediaConversionJob;
368
+ // using custom conversion definition
369
+ new MediaConversionDefinition(
370
+ name: 'webp',
371
+ when: fn($media, $parent) => $media->type === MediaType::Image,
372
+ handle: function($media, $parent, $file, $filesystem, $temporaryDirectory){
449
373
450
- class OptimizedImageConversionJob extends MediaConversionJob
451
- {
452
- public string $fileName;
453
-
454
- public function __construct(
455
- public Media $media,
456
- ?string $queue = null,
457
- public ?int $width = null,
458
- public ?int $height = null,
459
- public Fit $fit = Fit::Contain,
460
- public ?OptimizerChain $optimizerChain = null,
461
- ?string $fileName = null,
462
- ) {
463
- parent::__construct($media, $queue);
464
-
465
- $this->fileName = $fileName ?? $this->media->file_name;
466
- }
467
-
468
- public function run(): void
469
- {
470
- $temporaryDisk = $this->getTemporaryDisk();
471
- $path = $this->makeTemporaryFileCopy();
374
+ $target = $filesystem->path("{$media->name}.webp");
472
375
473
- $newPath = $temporaryDisk->path($this->fileName);
376
+ Image::load($filesystem->path($file))
377
+ ->optimize($this->optimizerChain)
378
+ ->save($target);
474
379
475
- Image::load($path)
476
- ->fit($this->fit, $this->width, $this->height)
477
- ->optimize($this->optimizerChain)
478
- ->save($newPath);
380
+ return $media->addConversion(
381
+ file: $target,
382
+ conversionName: $this->name,
383
+ parent: $parent,
384
+ );
479
385
480
- $this->media->storeConversion(
481
- file: $newPath,
482
- conversion: $this->conversionName,
483
- name: File::name($this->fileName)
484
- );
386
+ }
387
+ ),
388
+ ]
389
+ )
390
+ new MediaCollection(
391
+ name: 'videos',
392
+ acceptedMimeTypes: [
393
+ 'video/mp4', 'video/webm', 'video/ogg', 'video/quicktime',
394
+ ],
395
+ conversions: [
396
+ new MediaConversionPoster(
397
+ name: 'poster'
398
+ ),
399
+ ]
400
+ )
401
+ ];
485
402
}
486
403
}
487
404
```
@@ -523,7 +440,7 @@ The library is typed with generics, so you can use your own Media model seamless
523
440
namespace App\Models;
524
441
525
442
use App\Models\Media;
526
- use Elegantly\Media\Traits \HasMedia;
443
+ use Elegantly\Media\Concerns \HasMedia;
527
444
use Elegantly\Media\Contracts\InteractWithMedia;
528
445
529
446
/**
0 commit comments