Skip to content

Instantly share code, notes, and snippets.

@neverything
Last active October 18, 2024 06:22
Show Gist options
  • Save neverything/86a6f8a17c28cb80e1079fedf9f0c5c4 to your computer and use it in GitHub Desktop.
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
<?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