teners / laravel-link-preview
A Laravel package for extracting link previews with customizable parsers, and caching support
Installs: 2 108
Dependents: 0
Suggesters: 0
Security: 0
Stars: 4
Watchers: 0
Forks: 0
Open Issues: 0
Requires
- guzzlehttp/guzzle: ^7.0
- symfony/dom-crawler: ^7.0
Requires (Dev)
- orchestra/testbench: ^8.21
This package is auto-updated.
Last update: 2025-08-22 13:13:09 UTC
README
A powerful and flexible Laravel package for extracting rich link previews with customizable parsers, intelligent caching, and support for Twitter/X, YouTube, and more. - Some use cases
Features
- Smart Platform Detection - Automatically detects and handles Twitter/X, YouTube, Vimeo, and generic HTML
- Flexible Caching - Database or Laravel cache system with configurable expiration
- Highly Configurable - Extensive configuration options for timeouts, redirects, SSL verification
- Mobile-Optimized - Smart user agent handling for better compatibility
- Extensible Architecture - Easy to add custom parsers for new platforms
- Rich Metadata - Extracts titles, descriptions, images, videos, authors, and more
- Auto-Pruning - Automatic cleanup of expired cached entries
The package automatically detects different platforms and uses specialized parsers:
- Twitter/X - Enhanced metadata extraction with fallback mechanisms
- Generic HTML - Open Graph, Twitter Cards, and standard HTML meta tags
- YouTube/Vimeo - Video-specific metadata (coming soon)
Installation
To install Laravel Link Preview via composer, run the following command in your terminal:
composer require teners/laravel-link-preview
Publish the package configuration file
php artisan vendor:publish --provider="Teners\LaravelLinkPreview\LaravelLinkPreviewServiceProvider" --tag="link-preview-config"
Run migrations to create the cache table (if using model-based caching):
php artisan migrate
Usage
use Teners\LaravelLinkPreview\LinkPreview; // Get link preview for any URL $preview = LinkPreview::getPreview('https://example.com'); // Returns an array with extracted metadata /* [ 'title' => 'Example Website', 'description' => 'This is an example website description', 'cover' => 'https://example.com/image.jpg', 'author' => 'John Doe', 'keywords' => 'example, website, demo', 'icon' => 'https://example.com/favicon.ico', 'video' => 'https://example.com/video.mp4', 'video_type' => 'video/mp4' ] */
Using the Built-in Route
The package includes a convenient route for AJAX requests:
// GET request to /link-preview?url=https://example.com fetch('/link-preview?url=' + encodeURIComponent(url)) .then(response => response.json()) .then(data => { console.log(data); // Link preview data });
Configuration
The package provides extensive configuration options in config/link-preview.php
:
// Enable/disable caching 'enable_caching' => env('LINK_PREVIEW_ENABLE_CACHING', true), // Cache duration in seconds (default: 1 week) 'cache_duration' => env('LINK_PREVIEW_CACHE_DURATION', 604800), // Cache type: 'model' (database) or 'app' (Laravel cache) 'cache_type' => env('LINK_PREVIEW_CACHE_TYPE', 'model'), // Request timeout 'timeout' => env('LINK_PREVIEW_TIMEOUT', 15), // Connection timeout 'connect_timeout' => env('LINK_PREVIEW_CONNECT_TIMEOUT', 10), // Maximum redirects 'max_redirects' => env('LINK_PREVIEW_MAX_REDIRECTS', 5), // User agent 'user_agent' => env('LINK_PREVIEW_USER_AGENT', 'Teners/LinkPreview v2.0.0 (Compatible Bot)'), // SSL verification 'verify_ssl' => env('LINK_PREVIEW_VERIFY_SSL', true),
Contributions
Contributions are welcome via Pull Requests on Github.
- Please document any change you made as neccesary in the README.md.
- Follow PSR-12 coding standards
- Write tests for new features
- Update documentation for any changes
- Make one pull request per feature/fix
- Ensure all tests pass
Roadmap Items (Help Wanted!)
- Add Other Parsers to extrack videos
- YouTube parser
- Vimeo parser implementation
- JavaScript/Puppeteer reader for dynamic content
- Image size validation and quality checks
- Global CDN cache option
- Instagram parser
- LinkedIn parser
Issues
Please report any issue you encounter in using the package through the Github Issues tab.
When reporting issues, please include:
- Laravel version
- PHP version
- Package version
- Code example
- Error messages
Testing
composer test
Credits
Contributors list will be added here
License
The MIT License (MIT). Please see License File for more information.
Made with ❤️ by Teners - if this package helped you ⭐ Star us on GitHub