From 420215cb30a69d64cb4d233cd7a0885ae6474710 Mon Sep 17 00:00:00 2001 From: Pedro Amorim Date: Tue, 21 Nov 2017 17:47:48 +0100 Subject: Use absolute Uri on main image. Sometimes og:image use a relative URI too. --- src/HTMLParser.php | 29 +++++++++++++++++++---------- 1 file changed, 19 insertions(+), 10 deletions(-) diff --git a/src/HTMLParser.php b/src/HTMLParser.php index b63deb7..9dff4bd 100644 --- a/src/HTMLParser.php +++ b/src/HTMLParser.php @@ -504,22 +504,31 @@ class HTMLParser */ public function getMainImage() { + $imgUrl = false; + if ($this->metadata['image'] !== null) { - return $this->metadata['image']; + $imgUrl = $this->metadata['image']; } - foreach ($this->dom->getElementsByTagName('link') as $link) { - /** @var \DOMElement $link */ - /* - * Check for the rel attribute, then check if the rel attribute is either img_src or image_src, and - * finally check for the existence of the href attribute, which should hold the image url. - */ - if ($link->hasAttribute('rel') && ($link->getAttribute('rel') === 'img_src' || $link->getAttribute('rel') === 'image_src') && $link->hasAttribute('href')) { - return $link->getAttribute('href'); + if (!$imgUrl) { + foreach ($this->dom->getElementsByTagName('link') as $link) { + /** @var \DOMElement $link */ + /* + * Check for the rel attribute, then check if the rel attribute is either img_src or image_src, and + * finally check for the existence of the href attribute, which should hold the image url. + */ + if ($link->hasAttribute('rel') && ($link->getAttribute('rel') === 'img_src' || $link->getAttribute('rel') === 'image_src') && $link->hasAttribute('href')) { + $imgUrl = $link->getAttribute('href'); + break; + } } } - return false; + if (!empty($imgUrl) && $this->getConfig()->getOption('fixRelativeURLs')) { + $imgUrl = $this->toAbsoluteURI($imgUrl); + } + + return $imgUrl; } /** -- cgit v1.2.3