Php 约姆拉3号。将文章的第一个图像设置为简介图像

Php 约姆拉3号。将文章的第一个图像设置为简介图像,php,joomla,Php,Joomla,7moHi, 我有joomla 3,我想在分类博客覆盖中将文章的第一个图像设置为简介图像。 我有一个插件,可以抓取文章的第一个图像,并将其设置在og图像元标记中。我想我们可以用这个函数来回答我的问题。 这是插件的代码 // Image $pictures = ''; if (isset($row->images)) { //$pictures = json_decode($row->images); $pictures = (is_

7moHi, 我有joomla 3,我想在分类博客覆盖中将文章的第一个图像设置为简介图像。 我有一个插件,可以抓取文章的第一个图像,并将其设置在og图像元标记中。我想我们可以用这个函数来回答我的问题。 这是插件的代码

// 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
//图像
$pictures='';
if(设置($row->images)){
//$pictures=json_解码($row->images);
$pictures=(是字符串($row->images)?json解码($row->images):$row->images);
}
$imgSet=0;
如果($this->params->get('image'.$suffix',)!=''&$parameterImage==1){
$this->renderTag('og:image',$this->setImage($this->params->get('image'.$suffix',),$type);
$imgSet=1;
}否则如果($thisImg!=''){
$this->renderTag('og:image',$this->setImage($thisImg),$type);
$imgSet=1;
}else if(isset($pictures->{'image\u intro'})&&&$pictures->{'image\u intro'}!=''){
$this->renderTag('og:image',$this->setImage($pictures->{'image\u intro'}),$type);
$imgSet=1;
}else if(isset($pictures->{'image\u fulltext'})&&&$pictures->{'image\u fulltext'}!=''){
$this->renderTag('og:image',$this->setImage($pictures->{'image\u fulltext'}),$type);
$imgSet=1;
}否则{
//尝试在文章中找到图像
$fulltext='';
如果(设置($row->fulltext)&&&$row->fulltext!=''){
$fulltext=$row->fulltext;
}
$introtext='';
如果(isset($row->introtext)&&&$row->introtext!=''){
$introtext=$row->introtext;
}
$content=$introtext.$fulltext;
预匹配('/<*img[^>]*src*=*[“\']?([^“\']*)/i',$content,$src);
如果(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;
}
//尝试在images/phocaopengraph文件夹中查找图像
如果($imgSet==0){
如果(设置($row->id)&(int)$row->id>0){
jimport('joomla.filesystem.file');
$imgPath='';
$path=JPATH_ROOT.'/images/phocaopengraph/';
如果(JFile::exists($path.'/'(int)$row->id..jpg')){
$imgPath='images/phocaopengraph/'(int)$row->id..jpg';
}else如果(JFile::exists($path.'/'(int)$row->id..png')){
$imgPath='images/phocaopengraph/'(int)$row->id..png';
}else如果(JFile::exists($path.'/'(int)$row->id..gif')){
$imgPath='images/phocaopengraph/'(int)$row->id..gif';
}
如果($imgPath!=''){
$this->renderTag('og:image',$this->setImage($imgPath),$type);
$imgSet=1;
}
}
}
}
//如果未设置静止图像且参数图像设置为最后一个,则尝试添加参数图像
如果($imgSet==0&&$this->params->get('image'.$suffix')!='&$parameterImage==0){
$this->renderTag('og:image',$this->setImage($this->params->get('image'.$suffix',),$type);
}
//结束图像
我想在我的覆盖页面中插入结果,在这一点上

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

但是我不能为我的目的修改代码。 在override eof template中,我还找到了
$this->item->event->beforeDisolayContent
那个想法显示了整篇文章。
我能抓取文章的第一张图片并插入介绍文字吗

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

我已经解决了!

在解析有效html时,为了可靠性,应该避免使用正则表达式。
$pictures->{'image\u intro'}
更简单地写为
$pictures->image\u intro
。当检查变量
isset()
非false
时,最简单的方法是使用
!empty()
isset($pictures->{'image\u intro'})和&$pictures->{'image\u intro'}!=''
变为
!空($pictures->image\u intro)