diff options
author | Pedro Amorim <[email protected]> | 2017-11-21 17:47:48 +0100 |
---|---|---|
committer | Pedro Amorim <[email protected]> | 2017-11-23 15:50:50 +0100 |
commit | 420215cb30a69d64cb4d233cd7a0885ae6474710 (patch) | |
tree | db7fedf875166840c4c7a4f3c25a85ed47de3c1d /src | |
parent | be0285ffc340dcbca76908caa0ce513706f0b316 (diff) |
Use absolute Uri on main image.
Sometimes og:image use a relative URI too.
Diffstat (limited to 'src')
-rw-r--r-- | src/HTMLParser.php | 29 |
1 files 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; } /** |