jclyons52 / page-preview
get info about page links and generate preview images
Requires
- php: ~5.5|~7.0
- jclyons52/php-query: ^1.0@dev
- league/plates: ^3.1
- psr/cache: ~1.0
Requires (Dev)
- phpunit/phpunit: 4.*
- squizlabs/php_codesniffer: ~2.3
- tedivm/stash: ^0.14.1
This package is not auto-updated.
Last update: 2024-11-09 20:28:13 UTC
README
Creates link previews to pages with thumbnail, title and description
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 jclyons52@gmail.com instead of using the issue tracker.
Credits
License
The MIT License (MIT). Please see License File for more information.