Last active
October 18, 2024 06:22
-
-
Save neverything/86a6f8a17c28cb80e1079fedf9f0c5c4 to your computer and use it in GitHub Desktop.
Set Image and Video dimension for the Spatie Laravel Media Library as custom properties: https://silvanhagen.com/writing/image-and-video-dimensions-laravel-media-library
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace App\Observers; | |
use App\Actions\Media\UpdateMediaInfoAction; | |
use App\Jobs\UpdateMediaInfoJob; | |
use Illuminate\Support\Facades\Log; | |
use Spatie\MediaLibrary\MediaCollections\Models\Media; | |
class MediaObserver | |
{ | |
public function __construct( | |
protected UpdateMediaInfoAction $updateMediaInfoAction | |
) {} | |
public function updated(Media $media): void | |
{ | |
if ($media->hasCustomProperty('dimensions')) { | |
Log::info("Media {$media->id} already processed. Skipping."); | |
return; | |
} | |
UpdateMediaInfoJob::dispatch($media)->delay(now()->addSeconds(10)); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment