Skip to content

Commit

Permalink
Update implementation for image handling
Browse files Browse the repository at this point in the history
  • Loading branch information
ralphjsmit committed Mar 9, 2022
1 parent 835be9e commit 5c6bb62
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 15 deletions.
4 changes: 0 additions & 4 deletions src/Models/SEO.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
17 changes: 13 additions & 4 deletions src/Support/ImageMeta.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
7 changes: 5 additions & 2 deletions src/Tags/OpenGraphTags.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()));
Expand Down
5 changes: 0 additions & 5 deletions tests/Fixtures/PageWithOverrides.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
}
}

0 comments on commit 5c6bb62

Please sign in to comment.