0

7moHi, I have joomla 3 and I want set the first image of article as intro image in category blog override. I have a plugin that grab first image of article and set it in og image metatag. I think that we can use this function for my question. This is the code of plugin

// Image
     $pictures = '';
     if (isset($row->images)) {
        //$pictures = json_decode($row->images);
        $pictures = (is_string($row->images) ? json_decode($row->images) : $row->images);
     }






     $imgSet = 0;


     if ($this->params->get('image'.$suffix, '') != '' && $parameterImage == 1) {
        $this->renderTag('og:image', $this->setImage($this->params->get('image'.$suffix, '')), $type);
        $imgSet = 1;
     } else if ($thisImg != ''){
        $this->renderTag('og:image', $this->setImage($thisImg), $type);
        $imgSet = 1;
     } else if (isset($pictures->{'image_intro'}) && $pictures->{'image_intro'} != '') {
        $this->renderTag('og:image', $this->setImage($pictures->{'image_intro'}), $type);
        $imgSet = 1;
     } else if (isset($pictures->{'image_fulltext'}) && $pictures->{'image_fulltext'} != '') {
        $this->renderTag('og:image', $this->setImage($pictures->{'image_fulltext'}), $type);
        $imgSet = 1;
     } else {
        // Try to find image in article


        $fulltext = '';
        if (isset($row->fulltext) && $row->fulltext != '') {
           $fulltext = $row->fulltext;
        }
        $introtext = '';
        if (isset($row->introtext) && $row->introtext != '') {
           $introtext = $row->introtext;
        }
        $content = $introtext . $fulltext;
        preg_match('/< *img[^>]*src *= *["\']?([^"\']*)/i', $content, $src);
        if (isset($src[1]) && $src[1] != '') {
           $this->renderTag('og:image', $this->setImage($src[1]), $type);
           //$this->renderTag('og:image', JURI::base(false).$src[1], $type);
           $imgSet = 1;
        }


        // Try to find image in images/phocaopengraph folder
        if ($imgSet == 0) {
           if (isset($row->id) && (int)$row->id > 0) {


              jimport( 'joomla.filesystem.file' );
              $imgPath   = '';
              $path       = JPATH_ROOT . '/images/phocaopengraph/';
              if (JFile::exists($path . '/' . (int)$row->id.'.jpg')) {
                 $imgPath = 'images/phocaopengraph/'.(int)$row->id.'.jpg';
              } else if (JFile::exists($path . '/' . (int)$row->id.'.png')) {
                 $imgPath = 'images/phocaopengraph/'.(int)$row->id.'.png';
              } else if (JFile::exists($path . '/' . (int)$row->id.'.gif')) {
                 $imgPath = 'images/phocaopengraph/'.(int)$row->id.'.gif';
              }


              if ($imgPath != '') {
                 $this->renderTag('og:image', $this->setImage($imgPath), $type);
                 $imgSet = 1;
              }
           }
        }
     }


     // If still image not set and parameter Image is set as last, then try to add the parameter image
     if ($imgSet == 0 && $this->params->get('image'.$suffix, '') != '' && $parameterImage == 0) {
        $this->renderTag('og:image', $this->setImage($this->params->get('image'.$suffix, '')), $type);
     }


     // END IMAGE

And I want insert result in my override page, in this point

   <div class="image-intro">
      <?php echo JLayoutHelper::render('joomla.content.intro_image', $this->item); ?>
    <div>

But I can't modify the code for my purpose. In override eof template I have find also $this->item->event->beforeDisolayContent; That think show full article. Can I grab first image of article and insert in intro text?

  • `$pictures->{'image_intro'}` is more simply written as `$pictures->image_intro`. When checking that a variable `isset()` and `non-falsey`, it is simplest to use `!empty()`... `isset($pictures->{'image_intro'}) && $pictures->{'image_intro'} != ''` becomes `!empty($pictures->image_intro)` – mickmackusa Nov 30 '19 at 08:54

1 Answers1

0
$texthtml=$this->item->text; 
preg_match('/<img.+src=[\'"](?P<src>.+?)[\'"].*>/i', $texthtml, $image);
 $imgkk=$image['src'];
 echo '<img src="' . $imgkk. ' ">'
?>

I've solved!

  • When parsing valid html, you should avoid using regex for reliability. https://stackoverflow.com/a/56149100/2943403 – mickmackusa Nov 30 '19 at 08:52