marccoup / social-image-generator
生成适合开放图协议分享的简单图片
0.0.2
2022-09-07 01:34 UTC
Requires
- php: ^8.1
- intervention/image: ^2.7
README
为开放图协议生成简单的社交图片
要求
- PHP >= 8.1
- Fileinfo 扩展
- 以下图像库之一
- GD 库 >= 2.0
- Imagick PHP 扩展 >= 6.5.7
安装
composer require marccoup/social-image-generator
使用
驱动配置
此包依赖于intervention/image,并需要将他们的ImageManager
类的一个实例注入到SocialImageGenerator
中进行驱动配置。
<?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
有关更多信息,请参阅他们的驱动配置文档。
字体
此包运行所需的其他唯一东西是任何用于生成图片上文本的.ttf
字体文件。
<?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
构建你的图片
此包提供了一个流畅的 API 来构建图像,具有合理的(尽管有些无聊)默认值。
/** @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);
一旦你的生成器对象按你的喜好配置,你就可以生成你的图像。
唯一的非链式方法是应该是链中的最后一个方法 - $generator->generate()
- 它将返回一个Intervention\Image\Image
实例供你保存或按你希望的任何方式进行操作。
/** @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();
通常使用$generator->generate()
方法就足够了,但如果你希望以不同的顺序写入图像的各个部分,需要多个网格,或对图像进行其他操作,你可以以下方式与之交互
/** @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;
工作示例
我可以给出的最好例子,即如何生成此README顶部所示的图像。只要你有字体,此代码应生成完全相同的图像。
<?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');