Skip to content

Commit

Permalink
Add image path helper, fix #434
Browse files Browse the repository at this point in the history
  • Loading branch information
caendesilva committed Jun 6, 2022
1 parent 7170717 commit 9201367
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 1 deletion.
2 changes: 1 addition & 1 deletion resources/views/components/post/image.blade.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<figure aria-label="Cover image" itemprop="image" itemscope itemtype="http://schema.org/ImageObject" role="doc-cover">
<img src="{{ $page->image->getSource() }}" alt="{{ $page->image->description ?? '' }}" title="{{ $page->image->title ?? '' }}" itemprop="image" class="mb-0">
<img src="{{ $page->image->getLink($currentPage) }}" alt="{{ $page->image->description ?? '' }}" title="{{ $page->image->title ?? '' }}" itemprop="image" class="mb-0">
<figcaption aria-label="Image caption" itemprop="caption">
{!! $page->image->getFluentAttribution() !!}
</figcaption>
Expand Down
12 changes: 12 additions & 0 deletions src/Concerns/Internal/FileHelpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,18 @@ public static function relativeLink(string $destination, string $current = ''):
return str_replace('//', '/', $route);
}

/**
* Gets a relative link to the given image stored in the _site/media folder.
*/
public static function image(string $name, string $current = ''): string
{
if (str_starts_with($name, 'http')) {
return $name;
}

return static::relativeLink('media/'.basename($name), $current);
}

/**
* Return a qualified URI path, if SITE_URL is set in .env, else return false.
*
Expand Down
5 changes: 5 additions & 0 deletions src/Models/Image.php
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,11 @@ public function getSource(): ?string
return $this->uri ?? $this->path ?? null;
}

public function getLink(?string $currentPage = ''): string
{
return Hyde::image($this->getSource() ?? '', $currentPage);
}

public function getContentLength(): int
{
return (new FindsContentLengthForImageObject($this))->execute();
Expand Down

0 comments on commit 9201367

Please sign in to comment.