XF 2.0 how to get external image ?

hemant_bhardwaj

Well-known member
how to get first external URL in template ?
like currently am using to get first attachment image
{{ link('full:attachments', $thread.FirstPost.Attachments.first()) }}
but i all trying to get the external image too not only attachment
or in shot how to get first [IMG] tag
any Help

Regards
Hemant
 
Last edited:
You can extend the thread/post entity an use something like this
PHP:
<?php

namespace My\addon\XF\Entity;

class Thread extends XFCP_Thread
{
    public function getfirstPostImgUrl()
    {
        $message = $this->FirstPost->message;
        
        preg_match('/\[(img|IMG)\]\s*(https?:\/\/([^*\r\n]+|[a-z0-9\/\\\._\- !]+))\[\/(img|IMG)\]/Ui',$message,$matches);

        if(!$matches)
        {
            return false;
        }

        $url = $matches[2];

        $linkInfo = \XF::app()->stringFormatter()->getLinkClassTarget($url);
        
        if ($linkInfo['local'])
        {
            return $url;
        }
        
        if(\XF::app()->options()->imageLinkProxy['images'])
        {
            $proxyUrl = \XF::app()->stringFormatter()->getProxiedUrlIfActive('image', $url);
            return $proxyUrl;
        }

        return $url;
    }
}
and then to get the URL in the template you just have to put {$thread.getfirstPostImgUrl()}
 
You can extend the thread/post entity an use something like this
PHP:
<?php

namespace My\addon\XF\Entity;

class Thread extends XFCP_Thread
{
    public function getfirstPostImgUrl()
    {
        $message = $this->FirstPost->message;
     
        preg_match('/\[(img|IMG)\]\s*(https?:\/\/([^*\r\n]+|[a-z0-9\/\\\._\- !]+))\[\/(img|IMG)\]/Ui',$message,$matches);

        if(!$matches)
        {
            return false;
        }

        $url = $matches[2];

        $linkInfo = \XF::app()->stringFormatter()->getLinkClassTarget($url);
     
        if ($linkInfo['local'])
        {
            return $url;
        }
     
        if(\XF::app()->options()->imageLinkProxy['images'])
        {
            $proxyUrl = \XF::app()->stringFormatter()->getProxiedUrlIfActive('image', $url);
            return $proxyUrl;
        }

        return $url;
    }
}
and then to get the URL in the template you just have to put {$thread.getfirstPostImgUrl()}
Thanks it's work great :D sorry for the delay
i think it's no longer match will fail on the new XF2.1 Example; [img] format. Example; [IMG align="left" width="337px" alt="PHP versions"]
 
You can extend the thread/post entity an use something like this
PHP:
<?php

namespace My\addon\XF\Entity;

class Thread extends XFCP_Thread
{
    public function getfirstPostImgUrl()
    {
        $message = $this->FirstPost->message;
       
        preg_match('/\[(img|IMG)\]\s*(https?:\/\/([^*\r\n]+|[a-z0-9\/\\\._\- !]+))\[\/(img|IMG)\]/Ui',$message,$matches);

        if(!$matches)
        {
            return false;
        }

        $url = $matches[2];

        $linkInfo = \XF::app()->stringFormatter()->getLinkClassTarget($url);
       
        if ($linkInfo['local'])
        {
            return $url;
        }
       
        if(\XF::app()->options()->imageLinkProxy['images'])
        {
            $proxyUrl = \XF::app()->stringFormatter()->getProxiedUrlIfActive('image', $url);
            return $proxyUrl;
        }

        return $url;
    }
}
and then to get the URL in the template you just have to put {$thread.getfirstPostImgUrl()}
This is now possible in 2.2. Look here: https://xenforo.com/community/resources/joyfreak-set-open-graph-image.8121/
 
Back
Top Bottom