diff --git a/app/Entities/Models/Page.php b/app/Entities/Models/Page.php index 93fb21893..6e521b2b8 100644 --- a/app/Entities/Models/Page.php +++ b/app/Entities/Models/Page.php @@ -138,4 +138,23 @@ class Page extends BookChild $refreshed->html = (new PageContent($refreshed))->render(); return $refreshed; } + + /** + * Returns URL to a cover image for the page. + */ + public function getCoverImage() + { + //$default = $this->book->getBookCover(); + $default = url('/logo.png'); + + $firstImage = (new PageContent($this))->fetchFirstImage(); + + try { + $cover = $firstImage ? $firstImage : $default; + } catch (\Exception $err) { + $cover = $default; + } + return $cover; + } + } diff --git a/app/Entities/Tools/PageContent.php b/app/Entities/Tools/PageContent.php index 381ef172b..d178dc040 100644 --- a/app/Entities/Tools/PageContent.php +++ b/app/Entities/Tools/PageContent.php @@ -367,4 +367,18 @@ class PageContent $doc->loadHTML(mb_convert_encoding($html, 'HTML-ENTITIES', 'UTF-8')); return $doc; } + + /** + * Retrieve first image in page content and return the source URL. + */ + public function fetchFirstImage() + { + $htmlContent = $this->page->html; + + $dom = new \DomDocument(); + $dom->loadHTML($htmlContent); + $images = $dom->getElementsByTagName('img'); + + return $images->length > 0 ? $images[0]->getAttribute('src') : null; + } } diff --git a/resources/views/base.blade.php b/resources/views/base.blade.php index 0734466be..dc665a888 100644 --- a/resources/views/base.blade.php +++ b/resources/views/base.blade.php @@ -11,6 +11,12 @@ + + + + @stack('social-meta') + + diff --git a/resources/views/books/show.blade.php b/resources/views/books/show.blade.php index b850127ff..69a945cbe 100644 --- a/resources/views/books/show.blade.php +++ b/resources/views/books/show.blade.php @@ -6,6 +6,11 @@ option:entity-search:entity-type="book" @stop +@push('social-meta') + + +@endpush + @section('body')
diff --git a/resources/views/chapters/show.blade.php b/resources/views/chapters/show.blade.php index 803536e9f..a70f0f9f2 100644 --- a/resources/views/chapters/show.blade.php +++ b/resources/views/chapters/show.blade.php @@ -6,6 +6,11 @@ option:entity-search:entity-type="chapter" @stop +@push('social-meta') + + +@endpush + @section('body')