承接 vormkracht10/laravel-open-graph-image 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

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

vormkracht10/laravel-open-graph-image

Composer 安装命令:

composer require vormkracht10/laravel-open-graph-image

包简介

Laravel package to generate dynamic Open Graph images

README 文档

README

GitHub release (latest by date) Tests Packagist PHP Version Support Latest Version on Packagist Total Downloads

This Laravel package enables you to dynamically create Open Graph images for your website based on a single Blade template with HTML and CSS. In our example we use the Tailwind CDN. So designing a dynamic Open Graph Image as a developer just got very easy using this package!

Just add the meta tag with our url to the head of your page. The package will then generate the image and add it to the page. You can edit the view template which you can find in the resources folder.

Requirements

  • PHP 8.2+

Installation

First install Google Chrome or Chromium, as you need the binary to use it as a headless browser.

On macOS

# Google Chrome
brew install --cask google-chrome 
sudo xattr -rd com.apple.quarantine /Applications/Google\ Chrome.app # Remove quarantine macOS protection mechanism

# Chromium
brew install chromium
sudo xattr -rd com.apple.quarantine /Applications/Chromium.app # Remove quarantine macOS protection mechanism

On Debian/Ubuntu

For Ubuntu 22.04+ (Note: you'll need to update the environment variable of CHROME_PATH to chomium-browser)

sudo add-apt-repository ppa:savoury1/ffmpeg4
sudo add-apt-repository ppa:savoury1/chromium
sudo apt-get update
sudo apt-get install chromium-browser

For Ubuntu everything below 22.04

# Install chromium from PPA instead of snap, because of permission issues with snapd version
sudo add-apt-repository ppa:saiarcot895/chromium-dev -y
sudo apt update
sudo apt install chromium-browser -y

On Windows

Download and install Google Chrome or Chromium using on of these links.

Note: configure path to Chrome/Chromium

Sometimes it's necessary to point the package to a custom path where the binary is located, this can be set in the config og-image.chrome.path. The path can be retrieved using:

# macOS or Debian/Ubuntu
which chrome # probably /Applications/Google\ Chrome.app/Contents/MacOS/Google\ Chrome
which chromium # probably /opt/homebrew/bin/chromium
which chromium-browser # Debian/Ubuntu

# Windows
where chrome # probably C:\Program Files\Google\Chrome\Application\chrome.exe

Install and configure package

You can install the package via composer:

composer require backstage/laravel-og-image

Run the command to install the package:

php artisan og-image:install

You should also publish the views, to change the default layout of your Open Graph images:

php artisan vendor:publish --tag="og-image-views"

This is the content of the published config file (published at config/og-image.php):

<?php

return [
    'debug' => env('OG_IMAGE_DEBUG', false), // disable caching og images for development

    'extension' => 'jpg', // jpg, png, webp

    'width' => 1200,
    'height' => 630,

    'chrome' => [
        'path' => env('CHROME_PATH', 'chromium'),
        'flags' => [
            // '--disable-dev-shm-usage',
            // '--disable-gpu',
            // '--disable-setuid-sandbox',
            // '--disable-software-rasterizer',
            // '--hide-scrollbars',
            // '--mute-audio',
            // '--no-sandbox',
        ],
    ],

    // The cache location to use.
    'storage' => [
        'disk' => 'public',
        'path' => 'og-images',
    ],

    'metatags' => [
        'og:title' => 'title',
        'og:description' => 'description',
        'og:type' => 'type',
        'og:url' => 'url',
    ],
];

Usage

Add the Blade component into the head of your page. Providing the attributes you need in your view file:

<x-og-image-tags title="Backstage" subtitle="" />

If you want to use a different view than the default, add a view attribute with the path using dot or slash notation:

<x-og-image-tags title="Backstage" subtitle="" view="path.to.view.file" />

If you do not want to use a view but HTML directly in your view file, than you can use the slot to add the HTML to:

Note

If you're using this option, make sure to clear caches before adding or changing the HTML using php artisan og-image:clear to see the result in your browser.

<x-og-image-tags title="Backstage" subtitle="" view="path.to.view.file">
    <h1>Use this HTML and inline CSS to style the open graph image...</h1>
</x-og-image-tags>

If you don't want to use the blade component you can also use the facade or helper method to generate the url to the image.

// Facade
use Backstage\OgImage\Laravel\Facades\OgImage;

$url = OgImage::url(['title' => 'Backstage', 'subtitle' => '...']);

// or using the `og()` helper
$url = og(['title' => 'Backstage', 'subtitle' => '...']);

And add it like this to your Blade file:

<meta property="og:image" content="{!! $url !!}">
<meta property="og:image:type" content="image/{{ config('og-image.extension') }}">
<meta property="og:image:width" content="{{ config('og-image.width') }}">
<meta property="og:image:height" content="{{ config('og-image.height') }}">

When you share the page on any platform, the image will automatically be generated, cached and then shown in your post. The image from the default template will look like this:

Default template

This component uses the 'template' blade view by default. You can change this template to your needs. It is even possible to pass more attributes than the default ones. You can find the default template in the resources folder.

Passing extra attributes

Want to add more custom attributes to modify the button text for example? Simply pass them down to the blade component, facade or helper method:

<x-og-image-tags
    title="Backstage"
    subtitle=""
    button="Read more"
/>
// Facade
use Backstage\OgImage\Laravel\Facades\OgImage;

OgImage::url(['title' => 'Slimme websites', 'subtitle' => '...', 'button' => 'Read more']);

// Helper
og(['title' => 'Backstage', 'subtitle' => '...', 'button' => 'Read more']);

You can now access the variable in your view by using the {{ $button }} variable.

Generate image without using the blade component

When you need to generate the image without using the blade component, you can use the following method:

OgImage::createImageFromParams(['title' => 'Backstage', 'subtitle' => '...']);

This will return the actual image from your configured storage. You can use this method to generate the image in your own controller for example.

Clearing cached images

All generated open graph images are cached by default. If you want to remove the cache, you can use the following command:

php artisan og-image:clear-cache

Changelog

Please see CHANGELOG for more information on what has changed recently.

Contributing

Please see CONTRIBUTING for details.

Security Vulnerabilities

Please review our security policy on how to report security vulnerabilities.

Credits

License

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

vormkracht10/laravel-open-graph-image 适用场景与选型建议

vormkracht10/laravel-open-graph-image 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 18.28k 次下载、GitHub Stars 达 72, 最近一次更新时间为 2022 年 09 月 09 日, 在 PHP 生态内属于活跃度较高的组件。

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

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

围绕 vormkracht10/laravel-open-graph-image 我们能提供哪些服务?
定制开发 / 二次开发

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

  • 总下载量: 18.28k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 72
  • 点击次数: 25
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 72
  • Watchers: 3
  • Forks: 4
  • 开发语言: PHP

其他信息

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