Skip to content

Commit

Permalink
Merge pull request #522 from hydephp/create-image-file-object
Browse files Browse the repository at this point in the history
Create image link helper, fix #434
  • Loading branch information
caendesilva authored Jun 6, 2022
2 parents 90b338a + 9201367 commit 74439f0
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 13 deletions.
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
14 changes: 13 additions & 1 deletion src/Concerns/Internal/FileHelpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ public static function pageLink(string $destination): string
* @see \Tests\Unit\FileHelperRelativeLinkTest
*
* @param string $destination relative to output directory on compiled site
* @param string $current the current URI path relative to the same root
* @param string $current the current URI path relative to the site root
* @return string
*/
public static function relativeLink(string $destination, string $current = ''): string
Expand All @@ -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
11 changes: 0 additions & 11 deletions src/Helpers/Image.php

This file was deleted.

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 74439f0

Please sign in to comment.