fractal512 / og-image
基于 Intervention Image 的 Open Graph 图片生成器 - PHP 图像处理库
0.2
2024-08-11 16:46 UTC
Requires
- php: ^8.1
- intervention/image: ^3.0
Requires (Dev)
- ext-fileinfo: *
- mikey179/vfsstream: ^1.6
- phpunit/phpunit: ^9.5
This package is auto-updated.
Last update: 2024-09-11 17:14:53 UTC
README
基于 Intervention Image 的 Open Graph 图片生成器 - PHP 图像处理库。
特性
- 使用可完全自定义的文本、标志、背景和叠加来生成图像
- 背景图像效果,如模糊、灰度等。
- 在背景上自定义渐变叠加
- 轻松切换 GD 和 Imagick 驱动程序
要求
- PHP 8.1 或更高版本
- Intervention Image 3.0 或更高版本
安装
可以通过 composer 安装此包
composer require fractal512/og-image
对于 Laravel 框架,请使用 fractal512/laravel-og-image 包。
用法
使用 make()
方法创建 Open Graph 图像,在 make()
方法之前用需要的设置覆盖默认设置 $config
数组
<?php use Fractal512\OpenGraphImage\OpenGraphImage; require __DIR__ . '/vendor/autoload.php'; $config = [ 'logo_path' => __DIR__ . '/path/to/logo.png', 'background_path' => __DIR__ . '/path/to/background.png', // override other default settings from the configuration (see below all list) ]; $text = 'Text to be printed on the Open Graph Image'; $output = __DIR__ . '/generated-image.png'; $image = new OpenGraphImage($config); $image->make($text)->save($output);
可以在 make()
方法的第二个参数中传递自定义背景
// ... $background = __DIR__ . '/background-image.png'; $image = new OpenGraphImage($config); $image->make($text, $background)->save($output);
输出图像格式
输出图像的格式将自动检测从传递给 save()
方法的路径
// ... // supported output image formats: png, jpg, webp (with imagick driver only) $output = __DIR__ . '/generated-image.png'; $image = new OpenGraphImage($config); $result = $image->make($text)->save($output); // $result is boolean true or false
配置
内置默认配置设置列表
$config = [ /* |-------------------------------------------------------------------------- | Image Processing Driver |-------------------------------------------------------------------------- | | Supported: "gd" or "imagick" | */ 'driver' => 'gd', /* |-------------------------------------------------------------------------- | Image Width |-------------------------------------------------------------------------- | */ 'image_width' => 1200, /* |-------------------------------------------------------------------------- | Image Height |-------------------------------------------------------------------------- | */ 'image_height' => 630, /* |-------------------------------------------------------------------------- | Image Background Color |-------------------------------------------------------------------------- | */ 'background_color' => '#444444', /* |-------------------------------------------------------------------------- | Background Image Path |-------------------------------------------------------------------------- | | Path to the image used for background. | | Supported: "absolute/path/to/your/background/image.png", null | */ 'background_path' => null, /* |-------------------------------------------------------------------------- | Background Fill |-------------------------------------------------------------------------- | | Scale the image to fill the background. | */ 'background_fill' => true, /* |-------------------------------------------------------------------------- | Background Image Effects |-------------------------------------------------------------------------- | | An associative array of image effects, where an element's key is | the effect method's name in the Intervention library. | Different methods accept different numbers of arguments. | If no arguments, set null as an element's value, if more than one | argument needs to be passed, wrap them into an array. | Read more: https://image.intervention.io/v3/modifying/effects | | Available effects are: | "brightness" - 1 argument, integer in range -100..100 | "contrast" - 1 argument, integer in range -100..100 | "gamma" - 1 argument, float | "colorize" - 3 arguments, all integers in range -100..100 | "greyscale" - no arguments, set null | "flop" - no arguments, set null | "flip" - no arguments, set null | "blur" - 1 argument, integer in range 0..100 | "sharpen" - 1 argument, integer in range 0..100 | "invert" - no arguments, set null | "pixelate" - 1 argument, integer | "reduceColors" - 2 arguments, integer, string | */ 'background_effects' => [], /* |-------------------------------------------------------------------------- | Overlay Color |-------------------------------------------------------------------------- | */ 'overlay_color' => '#000000', /* |-------------------------------------------------------------------------- | Overlay Transparency Alpha Channel |-------------------------------------------------------------------------- | | Supported: float | */ 'overlay_alpha' => 0.35, /* |-------------------------------------------------------------------------- | Logo Image Path |-------------------------------------------------------------------------- | | Path to the image used for the logo. | | Supported: "absolute/path/to/your/logo/image.png", null | */ 'logo_path' => null, /* |-------------------------------------------------------------------------- | Logo Position |-------------------------------------------------------------------------- | | Supported: "top-left", "top", "top-right", "left", "center", "right", | "bottom-left", "bottom", "bottom-right" | */ 'logo_position' => 'bottom-right', /* |-------------------------------------------------------------------------- | Logo Offset on X-axis |-------------------------------------------------------------------------- | */ 'logo_pos_x' => 100, /* |-------------------------------------------------------------------------- | Logo Offset on Y-axis |-------------------------------------------------------------------------- | */ 'logo_pos_y' => 50, /* |-------------------------------------------------------------------------- | Logo Opacity |-------------------------------------------------------------------------- | */ 'logo_opacity' => 100, /* |-------------------------------------------------------------------------- | Text X Position |-------------------------------------------------------------------------- | | Coordinate on X-axis defining the base point of the first character. | */ 'text_pos_x' => 600, /* |-------------------------------------------------------------------------- | Text Y Position |-------------------------------------------------------------------------- | | Coordinate on Y-axis defining the base point of the first character. | */ 'text_pos_y' => 315, /* |-------------------------------------------------------------------------- | Text Horizontal Alignment |-------------------------------------------------------------------------- | | Supported: "left", "center", "right" | */ 'text_horizontal_align' => 'center', /* |-------------------------------------------------------------------------- | Text Vertical Alignment |-------------------------------------------------------------------------- | | Supported: "top", "middle", "bottom" | */ 'text_vertical_align' => 'middle', /* |-------------------------------------------------------------------------- | Text Wrap Width |-------------------------------------------------------------------------- | */ 'text_wrap_width' => 1000, /* |-------------------------------------------------------------------------- | Line Height |-------------------------------------------------------------------------- | | Supported: float | */ 'text_line_height' => 1.5, /* |-------------------------------------------------------------------------- | Text Color |-------------------------------------------------------------------------- | */ 'text_color' => '#ffffff', /* |-------------------------------------------------------------------------- | Text Font Size |-------------------------------------------------------------------------- | */ 'text_font_size' => 48, /* |-------------------------------------------------------------------------- | Font Path |-------------------------------------------------------------------------- | | If null, preset font (Roboto Regular) will be used. | | Supported: "absolute/path/to/your/font.ttf", null | */ 'text_font_path' => null, ];
致谢
- Denys Vashchuk (fractal512)
- 所有贡献者
许可
MIT 许可证 (MIT)。有关更多信息,请参阅 许可文件。