marccoup/social-image-generator
最新稳定版本:0.0.2
Composer 安装命令:
composer require marccoup/social-image-generator
包简介
Generate simple images suitable for open graph protocol sharing
README 文档
README
Generate simple social images for the Open Graph protocol
Requirements
- PHP >= 8.1
- Fileinfo Extension
- One of the following image libraries:
- GD Library >= 2.0
- Imagick PHP extension >= 6.5.7
Installation
composer require marccoup/social-image-generator
Usage
Driver configuration
This package depends on intervention/image and requires an instance of their
ImageManager class to be injected into the SocialImageGenerator for driver configuration.
<?php use Intervention\Image\ImageManager; use Marccoup\SocialImageGenerator\SocialImageGenerator; $imageManager = new ImageManager(['driver' => 'imagick']); $generator = SocialImageGenerator::make( $imageManager, __DIR__.'/path/to/my/font.ttf' ); // Do stuff with $generator
Refer to their documentation on driver configuration for more information.
Fonts
The only other thing required for this package to work is any .ttf font file to be used for the text written to the
generated images.
<?php use Intervention\Image\ImageManager; use Marccoup\SocialImageGenerator\SocialImageGenerator; $myFont = __DIR__.'/path/to/my/font.ttf'; $generator = SocialImageGenerator::make( new ImageManager(['driver' => 'imagick']), $myFont ); // Do stuff with $generator
Building your image
This package provides a fluid API for building an image, with sensible (if boring) defaults.
/** @var Marccoup\SocialImageGenerator\SocialImageGenerator $generator */ // Pixel width of the image (default: 1200) $generator->width(1200); // Pixel height of the image (default: 630) $generator->height(630); // Pixels within the image to pain content (default: 100) $generator->safeAreaBoundary(100); // The primary background colour (default: '#ffffff') $generator->backgroundColorHex('#ffffff'); // The text used for the primary text on the image (default: '') $generator->titleText('My Awesome Blog Post'); // The font size of the title text (default: 72) $generator->titleTextSize(72); // The colour of the title text (default: '#000000') $generator->titleTextColorHex('#000000'); // The text used for the smaller text at the bottom on the image (default: '') $generator->footerText('my-blog.example'); // The font size of the footer text (default: 36) $generator->footerTextSize(36); // The colour of the footer text (default: '#000000') $generator->footerTextColorHex('#000000'); // Enable the lattice background effect (default) $generator->withLattice(); // Disable the lattice background effect $generator->withoutLattice(); // The amount of "diamonds" in one "row" of the lattice (default: 30) $generator->latticeSize(30); // The color of the lines making up the lattice (default: #dddddd) $generator->latticeColorHex('#dddddd'); // The width of the lines making up the lattice (default: 1) $generator->latticeLineWidth(1); // Enable the border (default) $generator->withBorder(); // Disable the border $generator->withoutBorder(); // The colour of the border (default: '#000000') $generator->borderColorHex('#000000'); // The width of the border (default: 30) $generator->borderWidth(30);
Once your generator object has been configured how you like, you can generate your image.
The only non-chainable method is the one that should always be the final one in the chain - $generator->generate() -
which will return an instance of Intervention\Image\Image for you to save, or otherwise do with as you wish.
/** @var Marccoup\SocialImageGenerator\SocialImageGenerator $generator */ // Generates an image according to the configuration of the object and returns the resulting `Intervention\Image\Image` instance $generator->generate();
Usually using the $generator->generate() method will be all you need but if you wish to write the sections of the
image in a different order, want multiple lattices, or to do anything else with the image you can interact with it in the
following ways:
/** @var Marccoup\SocialImageGenerator\SocialImageGenerator $generator */ // Manually start the generation of the image if it hasn't already been started $generator->start(); // Manually draw the lattice on the image, ignores any prior calls to `$generator->withoutLattice()` $generator->drawLattice(); // Manually draw the border on the image, ignores any prior calls the `$generator->withoutBorder()` $generator->drawBorder(); // Manually write the title text to the image $generator->writeTitle(); // Manually write the footer text to the image $generator->writeFooter(); // Access the Intervention\Image\Image object itself to manipulate it how you like $generator->image;
Working example
The best example I can give, how I generated the image at the top of this readme. As long as you have the font, this code should generate the exact same image.
<?php use Intervention\Image\ImageManager; use Marccoup\SocialImageGenerator\SocialImageGenerator; $imageManager = new ImageManager(['driver' => 'imagick']); SocialImageGenerator::make($imageManager, __DIR__.'/fonts/Roboto-Medium.ttf') ->titleText('Social Image Generator') ->titleTextSize(84) ->titleTextColorHex('#4a4a4a') ->backgroundColorHex('#f9f9f9') ->footerTextColorHex('#1d7484') ->borderColorHex('#1d7484') ->footerText('`composer require marccoup/social-image-generator`') ->latticeSize(15) ->generate() ->save(__DIR__.'/og-img.png');
marccoup/social-image-generator 适用场景与选型建议
marccoup/social-image-generator 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 8 次下载、GitHub Stars 达 0, 最近一次更新时间为 2022 年 09 月 06 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「image」 「social」 「open graph」 「image generator」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 marccoup/social-image-generator 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 marccoup/social-image-generator 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 marccoup/social-image-generator 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Netgen Open Graph Bundle is an Ibexa Platform bundle that allows simple integration with Open Graph protocol.
Simple Sharing generates social media share links within CP entry pages, allowing you to quickly & easily share entries.
Yii2 LightBox image galary widget uses Lightbox v2.10.0 by Lokesh Dhakar
The Yii2 extension uses jQuery jquery.carousel-1.1.min.js and makes image carousel from php array of structure defined.
LinkedIn integration for Social
A tool for scraping URL resources (oEmbed, OpenGraph, Twitter cards, JSON-LD)
统计信息
- 总下载量: 8
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 8
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2022-09-06
