定制 cardei/link-preview 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

cardei/link-preview

Composer 安装命令:

composer require cardei/link-preview

包简介

Link preview generation for Laravel

README 文档

README

Link preview generation 🛠️ for Laravel 🚀 applications. This package 📦 generates link previews for Laravel applications, similar to how social media platforms display link previews when a URL is shared.

Features 🌟

  • Automatic Metadata Extraction 📝: Extracts titles 🏷️, descriptions 📄, and images 🖼️ from URLs to generate rich link previews.
  • Supports Popular Platforms 🌐: Built-in support for platforms like YouTube ▶️ and Vimeo 🎥 for embedded previews.
  • Customizable Configuration ⚙️: Fine-tune 🔧 the package to meet your application's specific requirements.
  • Laravel Integration 🤝: Seamlessly integrates into your Laravel projects with service providers and facades.
  • Simple Usage 😊: Easy to implement in any controller 🎛️ or service.

Compatibility ✅

  • PHP Version: PHP >= 8.2 🐘
  • Laravel Versions: Laravel 10.x and 11.x 🚀

Dependencies 📦

  • Guzzle (guzzlehttp/guzzle): ^7.4 - For HTTP 🌐 requests to fetch URL metadata.
  • Symfony Dom Crawler (symfony/dom-crawler): ^7.0 - For parsing HTML content 📄.
  • Symfony CSS Selector (symfony/css-selector): ^7.0 - For extracting metadata from the parsed HTML.

Installation 🛠️

To install the package via Composer, run the following command:

composer require cardei/link-preview

After installation, you can publish the configuration file if you need to customize the settings:

php artisan vendor:publish --tag=link-preview-config

This will create a configuration file at config/link-preview.php.

Requirements 📋

  • PHP >= 8.2 🐘
  • Laravel 10.x or 11.x 🚀

Configuration ⚙️

The package provides a configuration file that can be published using the vendor:publish command. By default, the package includes basic configuration for logging 📝 and URL processing:

return [
    'enable_logging' => env('LINK_PREVIEW_ENABLE_LOGS', false),
];
  • enable_logging: If set to true ✅, the package will log information about the URLs being processed when APP_DEBUG=true.

To enable logging, you can add the following to your .env file:

APP_DEBUG=true
LINK_PREVIEW_ENABLE_LOGS=true

Usage 💻

Generating Link Previews 🔗

The package automatically detects URLs and generates previews using metadata from the target URL (e.g., titles 🏷️, images 🖼️, descriptions 📄). It supports platforms such as YouTube ▶️, Vimeo 🎥, and general HTML pages 📄.

To use the package, simply call the link-preview service in your Laravel controllers or services:

$linkPreview = app('link-preview');
$preview = $linkPreview->setUrl('https://example.com')->getPreview();

You can then access metadata like title 🏷️, description 📄, and cover image 🖼️ from the $preview object.

Supported Platforms 🌐

  • YouTube ▶️: Automatically generates embedded previews for YouTube links. Note: Integration with the YouTube API is required for optimal performance. This is particularly important when running the package from cloud instances, as direct page scraping for YouTube links might fail due to network restrictions or bot detection mechanisms. Using the YouTube API ensures consistent and reliable previews.
  • Vimeo 🎥: Similar to YouTube, generates embedded previews for Vimeo links.
  • General HTML Links 🌍: Extracts metadata such as titles 🏷️, descriptions 📄, and images 🖼️ from any general webpage.

Why Use Laravel Link Preview? 🤔

  • Simplicity 😊: This package is designed to be easy to integrate and use within any Laravel project, with minimal configuration required.
  • Rich Integration 💎: Direct integration with Laravel services and easy access via facades.
  • Customizable ⚙️: Configuration options allow you to tailor the package's behavior to your specific needs, including the ability to add new parsers.
  • Actively Developed 🛠️: Ongoing support and compatibility with the latest versions of Laravel.

Integration 🤝

The package provides seamless integration into your Laravel projects by including:

  • Service Providers 🛠️: The LaravelServiceProvider is automatically registered.
  • Facades 🏷️: Use the provided facade to easily access the link-preview functionality.

Example: Using in a Controller 📄

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Cardei\LinkPreview\Facades\LinkPreview;

class PreviewController extends Controller
{
    public function showPreview(Request $request)
    {
        $url = $request->input('url');
        $preview = LinkPreview::setUrl($url)->getPreview();
        
        return view('preview', ['preview' => $preview]);
    }
}

TODO 📝

  • Support More Platforms 🌐: Expand support for additional platforms, such as SoundCloud 🎶 and TikTok 🎵.
  • Advanced Error Handling ⚠️: Improve error handling for failed previews.
  • Caching 🗄️: Implement caching for previews to avoid multiple requests for the same URL.
  • Internationalization 🌍: Add support for parsing metadata in different languages.

Versioning and Branches 📌

  • v1.0.0 - 1.0.1: Supports Laravel 10.x 🚀
  • v2.x: Supports Laravel 11.x and newer versions 🚀

Branches:

  • main 🚀: Default branch with the latest stable version ready for production.
  • dev-main 🛠️: Development branch with the latest updates.
  • 2.1.0-DEV 🚧: Latest development version for Laravel 11.x support.

Documentation 📚

For detailed documentation, refer to the GitHub Wiki.

Contributing 🤝

Contributions are always welcome! If you'd like to contribute, please fork this repository, make your changes, and submit a pull request 📥.

License 📜

This package is open-sourced software licensed under the MIT license.

Support 🆘

For issues and feature requests, please visit the GitHub Issues page.

If you have any questions or need support, you can contact M. Catalin Cardei.

Why Laravel Link Preview is Better? 🏆

  • Native Laravel Integration 🤝: Unlike other link preview libraries that require extensive setup, this package is designed with Laravel in mind, providing native support for service providers and facades.
  • Modular Parser System 🔧: The parser system allows for easy addition of custom parsers, making it extensible.
  • Lightweight and Fast ⚡: Optimized for performance, it does not add unnecessary overhead to your application.
  • Open Source and Transparent 🔍: The code is available on GitHub, providing transparency and the ability to modify the package to fit your needs.

cardei/link-preview 适用场景与选型建议

cardei/link-preview 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 805 次下载、GitHub Stars 达 4, 最近一次更新时间为 2022 年 06 月 28 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「php」 「url」 「laravel」 「scraping」 「preview」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 cardei/link-preview 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 cardei/link-preview 我们能提供哪些服务?
定制开发 / 二次开发

基于 cardei/link-preview 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 805
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 4
  • 点击次数: 13
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 4
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2022-06-28