Skip to content

get info about page links and generate preview images

License

Notifications You must be signed in to change notification settings

jclyons52/page-preview

Repository files navigation

page-preview

Latest Version on Packagist Software License Build Status Coverage Status Quality Score Total Downloads

Creates link previews to pages with thumbnail, title and description

demo

Install

Via Composer

$ composer require jclyons52/page-preview

Usage

$previewManager = PreviewManager::create();
$preview = $previewManager->fetch('https://somewebsite.com');
echo $preview->render(); // returns bootstrap media link preview
echo $preview->toJson(); // returns json string of preview attributes

or do it inline:

PreviewManager::create()->fetch('https://somewebsite.com')->render();

Use another one of the other default templates:

$previewManager->render('thumbnail');

define your own templates:

$previewManager->fetch('https://somewebsite.com')->render('myAwesomeTemplate', '/path/to/template/directory');

caching

Http calls are slow, so to speed up your response times you may want to cache your previews. This package can take any psr-6 compliant cache driver as a parameter.

$pool = new Pool();

$previewManager = PreviewManager::create($pool);

$preview = $previewManager->findOrFetch('http://www.example.com/directory');

$previewManager->cache($preview);

The data available for you templates will be:

  • string $title - meta title or page title if not found in meta
  • string $description - meta description
  • string $url - link url
  • array $images - array of image urls
  • array $meta - array of meta values with their names as keys

If you're usign information from tags such as the twitter meta tags (or anything seperated with ':') you may want to use the unFlatten function to get a multi level array.

This meta:

<meta name="twitter:card" content="app">
<meta name="twitter:site" content="@TwitterDev">
<meta name="twitter:description" content="Cannonball is the fun way to create and share stories and poems on your phone. Start with a beautiful image from the gallery, then choose words to complete the story and share it with friends.">
<meta name="twitter:app:country" content="US">
<meta name="twitter:app:name:iphone" content="Cannonball">
<meta name="twitter:app:id:iphone" content="929750075">
<meta name="twitter:app:url:iphone" content="cannonball://poem/5149e249222f9e600a7540ef">
<meta name="twitter:app:name:ipad" content="Cannonball">
<meta name="twitter:app:id:ipad" content="929750075">
<meta name="twitter:app:url:ipad" content="cannonball://poem/5149e249222f9e600a7540ef">
<meta name="twitter:app:name:googleplay" content="Cannonball">
<meta name="twitter:app:id:googleplay" content="io.fabric.samples.cannonball">
<meta name="twitter:app:url:googleplay" content="http://cannonball.fabric.io/poem/5149e249222f9e600a7540ef">

using unFlatten:

$meta = $preview->meta->unFlatten()['twitter'];

Would produce the following array:

[
    "card" => "app",
    "site" => "@TwitterDev",
    "description" => "Cannonball is the fun way to create and share stories and poems on your phone. Start with a beautiful image from the gallery, then choose words to complete the story and share it with friends.",
    "app" => [
        "country" => "US",
        "name" => [
            "iphone" => "Cannonball",
            "ipad" => "Cannonball",
            "googleplay" => "Cannonball",
        ],
        "id" => [
            "iphone" => "929750075",
            "ipad" => "929750075",
            "googleplay" => "io.fabric.samples.cannonball",
        ],
        "url" => [
            "iphone" => "cannonball://poem/5149e249222f9e600a7540ef",
            "ipad" => "cannonball://poem/5149e249222f9e600a7540ef",
            "googleplay" => "http://cannonball.fabric.io/poem/5149e249222f9e600a7540ef",
        ],
    ]

];

Change log

Please see CHANGELOG for more information what has changed recently.

Testing

$ composer test

Contributing

Please see CONTRIBUTING and CONDUCT for details.

Security

If you discover any security related issues, please email [email protected] instead of using the issue tracker.

Credits

License

The MIT License (MIT). Please see License File for more information.

About

get info about page links and generate preview images

Resources

License

Stars

Watchers

Forks

Packages

No packages published