DOM helper on plugins

This commit is contained in:
Diego Najar 2017-10-21 20:30:59 +02:00
parent 20151c7f05
commit df978c0a75
3 changed files with 4 additions and 43 deletions

View File

@ -4,6 +4,8 @@ class DOM {
public static function getFirstImage($content)
{
// Disable warning
libxml_use_internal_errors();
$dom = new DOMDocument();
$dom->loadHTML('<meta http-equiv="content-type" content="text/html; charset=utf-8">'.$content);
$finder = new DomXPath($dom);

View File

@ -89,7 +89,7 @@ class pluginOpenGraph extends Plugin {
// If the page doesn't have a coverImage try to get an image from the HTML content
if (empty($og['image'])) {
// Get the image from the content
$src = $this->getImage($content);
$src = DOM::getFirstImage($content);
if ($src!==false) {
$og['image'] = $src;
} else {
@ -103,24 +103,4 @@ class pluginOpenGraph extends Plugin {
return $html;
}
// Returns the first image from the HTML content
private function getImage($content)
{
$dom = new DOMDocument();
$dom->loadHTML('<meta http-equiv="content-type" content="text/html; charset=utf-8">'.$content);
$finder = new DomXPath($dom);
$images = $finder->query("//img");
if($images->length>0) {
// First image from the list
$image = $images->item(0);
// Get value from attribute src
$imgSrc = $image->getAttribute('src');
// Returns the image src
return $imgSrc;
}
return false;
}
}

View File

@ -83,7 +83,7 @@ class pluginTwitterCards extends Plugin {
// If the page doesn't have a coverImage try to get an image from the HTML content
if( empty($data['image']) ) {
// Get the image from the content
$src = $this->getImage($content);
$src = DOM::getFirstImage($content);
if ($src!==false) {
$data['image'] = $src;
} else {
@ -96,25 +96,4 @@ class pluginTwitterCards extends Plugin {
$html .= '<meta property="twitter:image" content="'.$data['image'].'">'.PHP_EOL;
return $html;
}
// Returns the first image from the HTML content
private function getImage($content)
{
$dom = new DOMDocument();
$dom->loadHTML('<meta http-equiv="content-type" content="text/html; charset=utf-8">'.$content);
$finder = new DomXPath($dom);
$images = $finder->query("//img");
if($images->length>0) {
// First image from the list
$image = $images->item(0);
// Get value from attribute src
$imgSrc = $image->getAttribute('src');
// Returns the image src
return $imgSrc;
}
return false;
}
}