1

I have been trying to develop my own opengraph. I found some codes online and try to merge them but to no avail. I wonder if I have done it wrong.

// Try to find image in article
         $img = 0;
         $fulltext = '';
         if (isset($row->fulltext) && $row->fulltext != '') {
        $fulltext = $row->fulltext;
         }
         $introtext = '';
         if (isset($row->introtext) && $row->introtext != '') {
            $fulltext = $row->introtext;
         }
         $content = $introtext . $fulltext;
         preg_match('/< *img[^>]*src *= *["\']?([^"\']*)/i', $content, $src);
         if (isset($src[1]) && $src[1] != '') {
            $timage = htmlspecialchars($src[1]));
            $img = 1;
         }

$doc =& JFactory::getDocument();
$doc->addCustomTag( '
<meta property="og:title" content="'.$this->escape($this->item->title).'"/>
<meta property="og:type" content="article"/>
<meta property="og:image" content="'.$timage.'";/>
');

Second Code I am experimenting (I received the Undefined Variable : timage

function get_joomla_image($url)
    {
            if (preg_match('/< *img[^>]*src *= *["\']?([^"\']*)/i', $url, $match)) {
            $timage = $match[1];
            echo $match[1];
        }
    }



// get the corresponding thumbnail images   
if (isset($match[1]) and !empty($match[1]))
{
 $timage= htmlspecialchars(JURI::root().$match[1]);
}

$doc =& JFactory::getDocument();
$doc->addCustomTag( '
<meta name="og:image" content="'.$timage.'">

Thank you Joomla guru for your help.

  • Have you tried var_dump($match); to see what the variable contains? – johanpw Jan 30 '15 at 7:14
2

How about using DOMDocument instead of preg_match? That way you should be able to get all images in the body using getElementsByTagName(). Maybe something like this could work:

$buffer = JResponse::getBody();
$document = new DOMDocument();
@$document->loadHTML($buffer);

$images = $document->getElementsByTagName('img');

$images will be an object containing all elements (images) in your document. If you want to get the URL of the first image, you can use

 $image = $images->item(0)->getAttribute('src');

then

$doc = JFactory::getDocument();
$doc->addCustomTag( '
   <meta name="og:image" content="'.$image.'">
');

Just some quick ideas, you'll probably have to modify the code to make it work.

| improve this answer | |
  • When using your code, I received this error message when loading locally. Notice: Undefined variable: images in C:\xampp\htdocs\tyoga\templates\tyoga\html\com_content\article\default.php on line 13 Fatal error: Call to a member function item() on a non-object in C:\xampp\htdocs\tyoga\templates\tyoga\html\com_content\article\default.php on line 13 I hope dont mind giving me some enlightenment – Ompit Feb 7 '15 at 12:34
  • For researchers: JResponse::getBody() is deprecated since Joomla 3.2 and should be replaced with JFactory::getApplication()->getBody() joomla.stackexchange.com/a/10233/12352 – mickmackusa Nov 30 '18 at 0:58
2

For your Error:- Undefined Variable : timage

I have noticed that $timage variable is declared inside condition statement in both code.

 if (isset($src[1]) && $src[1] != '') {
            $timage = htmlspecialchars($src[1]));
            $img = 1;
         }

If above condition fails, $timage variable will never be declared. Try declaring this variable outside statement and change value as per condition. Hope this helps.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.