Generating link preview using php

Generating link preview using php

4 min. read

To generate preview for a link we need to parse the link/url & get content of file located at the url and then using pattern matching we can get title, description and image associated with the link.

Sanitize the URL

$url = htmlspecialchars(trim($received_url),ENT_QUOTES,'ISO-8859-1',TRUE);

Parse the URL to get the host name

$url_data = parse_url($url);
$host = $url_data['host'];

Open the file located at given URL

$file = fopen($url,'r');

Read the file content

$content = '';        
while(!feof($file))
{
    $content .= fgets($file,1024);
}

Get meta tags of the file located at the URL

$meta_tags = get_meta_tags($url);

Getting the title

$title = '';

if( array_key_exists('og:title',$meta_tags) )
{
    $title = $meta_tags['og:title'];
}
else if( array_key_exists('twitter:title',$meta_tags) )
{
    $title = $meta_tags['twitter:title'];
}
else
{
    $title_pattern = '/<title>(.+)<\/title>/i';
    preg_match_all($title_pattern,$content,$title,PREG_PATTERN_ORDER);

    if( !is_array($title[1]) )
        $title = $title[1];
    else
    {
        if( count($title[1]) > 0 )
            $title = $title[1][0];
        else
            $title = 'Title not found!';
    }
}

$title = ucfirst($title);

Getting the description

$desc = '';
        
if( array_key_exists('description',$meta_tags) )
{
    $desc = $meta_tags['description'];
}
else if( array_key_exists('og:description',$meta_tags) )
{
    $desc = $meta_tags['og:description'];
}
else if( array_key_exists('twitter:description',$meta_tags) )
{
    $desc = $meta_tags['twitter:description'];
}
else
{
    $desc = 'Description not found!';
}

$desc = ucfirst($desc);

Getting url of link preview image

$img_url = '';

if( array_key_exists('og:image',$meta_tags) )
{
    $img_url = $meta_tags['og:image'];
}
else if( array_key_exists('og:image:src',$meta_tags) )
{
    $img_url = $meta_tags['og:image:src'];
}
else if( array_key_exists('twitter:image',$meta_tags) )
{
    $img_url = $meta_tags['twitter:image'];
}
else if( array_key_exists('twitter:image:src',$meta_tags) )
{
    $img_url = $meta_tags['twitter:image:src'];
}
else
{
    // Image not found in meta tags so find it from content
    $img_pattern = '/<img[^>]*'.'src=[\"|\'](.*)[\"|\']/Ui';
    $images = '';
    preg_match_all($img_pattern,$content,$images,PREG_PATTERN_ORDER);

    $total_images = count($images[1]);
    if( $total_images > 0 )
        $images = $images[1];

    for($i=0; $i<$total_images; $i++)
    {
        if(getimagesize($images[$i]))
        {
            list($width,$height,$type,$attr) = getimagesize($images[$i]);
            
            if( $width > 600 ) // Select an image of width greater than 600px
            {
                $img_url = $images[$i];
                break;
            }
        }
    }
}

Complete code example

$url = htmlspecialchars(trim($received_url),ENT_QUOTES,'ISO-8859-1',TRUE);

$host = '';

if( !empty($url) )
{
    $url_data = parse_url($url);
    $host = $url_data['host'];

    $file = fopen($url,'r');
    
    if(!$file)
    {
        exit();
    }
    else
    {
        $content = '';
        while(!feof($file))
        {
            $content .= fgets($file,1024);
        }

        $meta_tags = get_meta_tags($url);

        // Get the title
        $title = '';

        if( array_key_exists('og:title',$meta_tags) )
        {
            $title = $meta_tags['og:title'];
        }
        else if( array_key_exists('twitter:title',$meta_tags) )
        {
            $title = $meta_tags['twitter:title'];
        }
        else
        {
            $title_pattern = '/<title>(.+)<\/title>/i';
            preg_match_all($title_pattern,$content,$title,PREG_PATTERN_ORDER);

            if( !is_array($title[1]) )
                $title = $title[1];
            else
            {
                if( count($title[1]) > 0 )
                    $title = $title[1][0];
                else
                    $title = 'Title not found!';
            }
        }

        $title = ucfirst($title);

        // Get the description
        $desc = '';
        
        if( array_key_exists('description',$meta_tags) )
        {
            $desc = $meta_tags['description'];
        }
        else if( array_key_exists('og:description',$meta_tags) )
        {
            $desc = $meta_tags['og:description'];
        }
        else if( array_key_exists('twitter:description',$meta_tags) )
        {
            $desc = $meta_tags['twitter:description'];
        }
        else
        {
            $desc = 'Description not found!';
        }

        $desc = ucfirst($desc);

        // Get url of preview image
        $img_url = '';
        if( array_key_exists('og:image',$meta_tags) )
        {
            $img_url = $meta_tags['og:image'];
        }
        else if( array_key_exists('og:image:src',$meta_tags) )
        {
            $img_url = $meta_tags['og:image:src'];
        }
        else if( array_key_exists('twitter:image',$meta_tags) )
        {
            $img_url = $meta_tags['twitter:image'];
        }
        else if( array_key_exists('twitter:image:src',$meta_tags) )
        {
            $img_url = $meta_tags['twitter:image:src'];
        }
        else
        {
            // Image not found in meta tags so find it from content
            $img_pattern = '/<img[^>]*'.'src=[\"|\'](.*)[\"|\']/Ui';
            $images = '';
            preg_match_all($img_pattern,$content,$images,PREG_PATTERN_ORDER);

            $total_images = count($images[1]);
            if( $total_images > 0 )
                $images = $images[1];

            for($i=0; $i<$total_images; $i++)
            {
                if(getimagesize($images[$i]))
                {
                    list($width,$height,$type,$attr) = getimagesize($images[$i]);
                    
                    if( $width > 600 ) // Select an image of width greater than 600px
                    {
                        $img_url = $images[$i];
                        break;
                    }
                }
            }
        }
        
        echo "<div>$title</div>";
        echo "<div><img src='$img_url' alt='Preview image'></div>";
        echo "<div>$desc</div>";
        echo "<div>$host</div>";
    }
}
Write comments