PHPackages                             brunoinds/link-preview-detector - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. brunoinds/link-preview-detector

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

brunoinds/link-preview-detector
===============================

Detect when a request is for preview generation

v1.0.0(2y ago)35.0k↓23.5%2MITPHP

Since Nov 28Pushed 2y ago1 watchersCompare

[ Source](https://github.com/brunoinds/link-preview-detector)[ Packagist](https://packagist.org/packages/brunoinds/link-preview-detector)[ RSS](/packages/brunoinds-link-preview-detector/feed)WikiDiscussions main Synced 1mo ago

READMEChangelog (1)Dependencies (1)Versions (2)Used By (0)

Detect when a request is for preview generation
===============================================

[](#detect-when-a-request-is-for-preview-generation)

 [![](https://raw.githubusercontent.com/brunoinds/link-preview-origin/main/link-preview-origin.png)](https://raw.githubusercontent.com/brunoinds/link-preview-origin/main/link-preview-origin.png)

Use-case:
---------

[](#use-case)

This tool addresses a common issue in modern web development: **How to detect if a request is for generating a link preview?**

Suppose your webpage needs to generate link previews for platforms like Facebook, WhatsApp, and Twitter. These previews are generated using meta-tags, which can slow down your page load time. Ideally, you want to generate these meta-tags only when the request is made by a bot from these platforms, not when a regular user is opening the page.

This tool provides a simple function that allows you to determine whether a page request is for generating a link preview or just a regular page visit by a user. This way, you can optimize your page load time by generating link previews only when necessary.

How to install it?
------------------

[](#how-to-install-it)

You can install this package via composer:

```
composer require brunoinds/link-preview-detector
```

How to use it?
--------------

[](#how-to-use-it)

### Method 1: Auto recognition

[](#method-1-auto-recognition)

This method auto-capture the request origin and user-agent, and detects if is for link preview.

```
use Brunoinds\LinkPreviewDetector\LinkPreviewDetector;

$response = LinkPreviewDetector::isForLinkPreview();
//returns a boolean (true/false). If it is true, it means the request is coming from a link preview crawler.
```

### Method 2: Manual recognition

[](#method-2-manual-recognition)

With this method you can pass an specific User-Agent and will return if is a link preview crawler:

```
use Brunoinds\LinkPreviewDetector\LinkPreviewDetector;

$userAgent = $_SERVER['HTTP_USER_AGENT'];

$response = LinkPreviewDetector::isForLinkPreviewUserAgent($userAgent);
```

How to implement it (examples):
-------------------------------

[](#how-to-implement-it-examples)

With the following example you can only send the metadata when it's required.

```
use Brunoinds\LinkPreviewDetector\LinkPreviewDetector;
$isForLinkPreview = LinkPreviewDetector::isForLinkPreview();

if ($isForLinkPreview)
{
  echo '';
  echo '';
  echo '';
  echo '';
}
```

One of the key benefits of this library is that it allows you to optimize your server's response based on the type of request. When a request is made specifically for link preview generation, **you don't need to send the entire webpage content**. Instead, you can **just send the meta-data required for the link preview**. This means you can **avoid sending unnecessary data** like JavaScript files, images, and other HTML content. This can significantly **improve the performance** of your server and the speed at which link previews are generated. Here's an example of how you can use this library to achieve this:

```
use Brunoinds\LinkPreviewDetector\LinkPreviewDetector;

$isForLinkPreview = LinkPreviewDetector::isForLinkPreview();

if ($isForLinkPreview) {
  // Send only the meta-data required for link preview
  echo '';

} else {
  // Send the entire webpage
   echo '

        Your Website Title

        Welcome to My Website
        This is a sample paragraph.

    ';
}
```

###  Health Score

28

—

LowBetter than 54% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity28

Limited adoption so far

Community11

Small or concentrated contributor base

Maturity42

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 94.1% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Unknown

Total

1

Last Release

902d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/54acc3d3fd4f73272aaec761ab85bf40397736e250431ec7c1cb34de791e9f18?d=identicon)[brunoinds](/maintainers/brunoinds)

---

Top Contributors

[![brunoinds](https://avatars.githubusercontent.com/u/49789611?v=4)](https://github.com/brunoinds "brunoinds (16 commits)")[![dajuly20](https://avatars.githubusercontent.com/u/7774238?v=4)](https://github.com/dajuly20 "dajuly20 (1 commits)")

###  Code Quality

TestsPest

### Embed Badge

![Health badge](/badges/brunoinds-link-preview-detector/health.svg)

```
[![Health](https://phpackages.com/badges/brunoinds-link-preview-detector/health.svg)](https://phpackages.com/packages/brunoinds-link-preview-detector)
```

###  Alternatives

[webgriffe/sylius-upgrade-plugin

Helps you upgrade your Sylius app to a new version.

13103.1k](/packages/webgriffe-sylius-upgrade-plugin)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
