ghostff/php-text-to-image

添加文本到现有或新图像

v1.0.0 2021-12-04 01:38 UTC

This package is auto-updated.

Last update: 2024-09-15 05:28:48 UTC


README

添加文本到现有或新图像。PHP >= 7.0

composer require ghostff/php-text-to-image

用法

$text1 = Text::from('Text One')->color(231, 81, 0);

$text2 = Text::from('Text Two')->color(130, 146, 145)->position(260, 35);

$text3 = (new Text('Text Three'))->set(150, 0, [0, 0, 252], '', 10, 1, 1, [50, 205, 50]);

$text  = Text::from('Text!')
            ->position(170, 150)
            ->font(20, __DIR__ . '/sweet purple.otf')
            ->shadow(2, 2, [255])
            ->color(255,255, 0)
            ->rotate(20);

写入现有图像

{
    header("Content-Type: image/png");
    echo (new TextToImage(__DIR__ . '/default.png'))->addTexts($text1, $text2, $text3, $text)->render();
}
    
// Or save to a file
(new TextToImage(__DIR__ . '/default.png'))->addTexts($text1, $text2, $text3, $text)->render(__DIR__ . '/tmp.png')

写入新图像

{
    header("Content-Type: image/png");
    echo (new TextToImage())->setDimension(350, 350)->setBackgroundColor(0, 0, 0)->addTexts($text1, $text2, $text3, $text)->render();
}
    
// Or save to a file
(new TextToImage())->setDimension(350, 350)->setBackgroundColor(0, 0, 0)->addTexts($text1, $text2, $text3, $text)->render(__DIR__ . '/tmp.png');

TextToImage 文档

TextToImage::__construct(string $from = '')
描述: 创建 TextToImage 实例。

TextToImage::setDimension(int $width, int $height): TextToImage
描述: 设置背景图像尺寸。 注意:如果向构造函数传递了 $from 参数,则不会评估此操作。

TextToImage::setBackgroundColor(int $r = 255, int $g = 255, int $b = 255, int $a = 255): TextToImage
描述: 设置创建的背景图像的背景颜色。 注意:如果向构造函数传递了 $from 参数,则不会评估此操作。

TextToImage::addTexts(Text $text, Text ...$texts): TextToImage
描述: 向指定的或生成的背景图像添加文本。

可以一次添加多个文本,也可以分步骤添加。

$hello  = Text::from('Hello');
$world  = Text::from('World')->position(0, 15);
$foo    = Text::from('Foo')->position(0, 30);
$bar    = Text::from('Bar')->position(0, 45);
$foobar = Text::from('Foobar')->position(0, 60)->color(255, 0, 0);


$text_image = new TextToImage();
$text_image->addTexts($hello, $world, $foo);
$text_image->addTexts($bar);
echo $text_image->setBackgroundColor(0, 0, 0)->addTexts($foobar)->render();

TextToImage::render(string $save_as = null, string $ext = null): string
描述: 将修改后的图像渲染到文件或返回内容。

Text 文档

Text::__construct(string $from = '')Text::__from(string $text): Text
描述: 创建 Text 实例。

Text::position(int $x, int $y = 0): Text
描述: 设置图像上指定文本的位置。

Text::font(int $size, string $path = null): Text
描述: 设置指定文本的字体/大小。

Text::color(int $r = 255, int $g = 255, int $b = 255, int $a = 255): Text
描述: 设置指定文本的颜色。

Text::shadow(int $position_x = null, int $position_y = null, array $color = []): Text
描述: 向指定文本添加阴影。

Text::from('FooBar')->shadow(1, 1, [0, 0, 255]);
// Create Text with half the max opacity
Text::from('FooBar')->shadow(1, 1, [0, 0, 255, 255 / 2]);

Text::rotate(float $degrees): Text
描述: 将指定文本旋转到特定角度。

Text::update(Closure $closure): Text
描述: 运行时文本更新。

$text = Text::from('FooBar')->shadow(1, 1, [0, 0, 255]);
$text->update(function (TextToImage $text_to_image, Text $text, $image) {
    // Basic centering of text.
    $text->position(intval($text_to_image->getHeight() / 2), intval($text_to_image->getWidth() / 2));
});