From 5c6bb6275875daf96e958e2edc792c6dadd95468 Mon Sep 17 00:00:00 2001 From: "Ralph J. Smit" <59207045+ralphjsmit@users.noreply.github.com> Date: Wed, 9 Mar 2022 17:11:49 +0100 Subject: [PATCH] Update implementation for image handling --- src/Models/SEO.php | 4 ---- src/Support/ImageMeta.php | 17 +++++++++++++---- src/Tags/OpenGraphTags.php | 7 +++++-- tests/Fixtures/PageWithOverrides.php | 5 ----- 4 files changed, 18 insertions(+), 15 deletions(-) diff --git a/src/Models/SEO.php b/src/Models/SEO.php index fd135b6..c96f0d1 100644 --- a/src/Models/SEO.php +++ b/src/Models/SEO.php @@ -29,10 +29,6 @@ public function prepareForUsage(): SEOData $enableTitleSuffix = $this->model->enableTitleSuffix; } - if ( method_exists($this->model, 'getSEOImageUrl') ) { - $image = $this->model->getSEOImageUrl(); - } - return new SEOData( title : $overrides->title ?? $this->title, description : $overrides->description ?? $this->description, diff --git a/src/Support/ImageMeta.php b/src/Support/ImageMeta.php index 30a3fe0..a9208d4 100644 --- a/src/Support/ImageMeta.php +++ b/src/Support/ImageMeta.php @@ -2,15 +2,24 @@ namespace RalphJSmit\Laravel\SEO\Support; -use Illuminate\Support\Str; +use PHPUnit\Runner\Exception; class ImageMeta { - public function __construct(string $imagePath) + public ?int $width = null; + public ?int $height = null; + + public function __construct(string $path) { - $path = public_path(Str::of($imagePath)->after('public')->trim('/')); + $publicPath = public_path($path); + + if ( ! is_file($publicPath) ) { + report(new Exception("Path {$publicPath} is not a file.")); + + return; + } - [$width, $height] = getimagesize($path); + [$width, $height] = getimagesize($publicPath); $this->width = $width; $this->height = $height; diff --git a/src/Tags/OpenGraphTags.php b/src/Tags/OpenGraphTags.php index 6a0a07b..adc2f8c 100644 --- a/src/Tags/OpenGraphTags.php +++ b/src/Tags/OpenGraphTags.php @@ -32,8 +32,11 @@ public static function initialize(SEOData $SEOData): static if ( $SEOData->image ) { $collection->push(new OpenGraphTag('image', $SEOData->image)); - $collection->push(new OpenGraphTag('image:width', $SEOData->imageMeta->width)); - $collection->push(new OpenGraphTag('image:height', $SEOData->imageMeta->height)); + if ( $meta = $SEOData->imageMeta() ) { + $collection + ->when($meta->width, fn (self $collection): self => $collection->push(new OpenGraphTag('image:width', $meta->width))) + ->when($meta->height, fn (self $collection): self => $collection->push(new OpenGraphTag('image:height', $meta->height))); + } } $collection->push(new OpenGraphTag('url', url()->current())); diff --git a/tests/Fixtures/PageWithOverrides.php b/tests/Fixtures/PageWithOverrides.php index b678ef9..54bfda6 100644 --- a/tests/Fixtures/PageWithOverrides.php +++ b/tests/Fixtures/PageWithOverrides.php @@ -14,9 +14,4 @@ class PageWithOverrides extends Model protected $table = 'pages'; public static array $overrides = []; - - public function getSEOImageUrl(): string - { - return secure_asset('public/storage/images/test.jpg'); - } } \ No newline at end of file