]> BookStack Code Mirror - bookstack/blobdiff - app/Entities/Tools/PageContent.php
Merge branch 'create-content-meta-tags' of https://github.com/james-geiger/BookStack...
[bookstack] / app / Entities / Tools / PageContent.php
index 381ef172b47105939740a31ae807a7fbff865f10..d178dc040c075e923bd64889b7954ba4270f8fb6 100644 (file)
@@ -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;
+    }
 }