用 php-vips 覆盖文本
Overlay text with php-vips
我正在尝试使用 php-vips 库编写一个 PHP 函数来将文本叠加到图像上。查看文档,我在 libvips 文档 here and the php-vips documentation here doesn't provide a ton of detail and seems to just direct you to use the libvips documentation. I found a snippet in one of php-vips issues (this) 中找不到绘制文本的函数,但它使用了当前 php-vips 库中不存在的文本函数。有谁知道是否可以使用 php-vips 将文本绘制到图像上,如果可以的话是如何完成的?作为参考,我的用例是在通过 PDF 下载时在照片上绘制照片的时间戳。
我给你做了一个演示程序:
#!/usr/bin/php
<?php
require __DIR__ . '/vendor/autoload.php';
use Jcupitt\Vips;
$image = Vips\Image::newFromFile($argv[1], ['access' => 'sequential']);
// this renders the text to a one-band image ... set width to the pixels across
// of the area we want to render to to have it break lines for you
$text = Vips\Image::text('Hello world!', [
'font' => 'sans 120',
'width' => $image->width - 100
]);
// make a constant image the size of $text, but with every pixel red ... tag it
// as srgb
$red = $text->newFromImage([255, 0, 0])->copy(['interpretation' => 'srgb']);
// use the text mask as the alpha for the constant red image
$overlay = $red->bandjoin($text);
// composite the text on the image
$out = $image->composite($overlay, "over", ['x' => 100, 'y' => 100]);
$out->writeToFile($argv[2]);
我可以运行这样:
$ ./render_text.php ~/pics/tiny_marble.jpg x.jpg
制作:
文本方法的文档在这里:
https://libvips.github.io/php-vips/docs/classes/Jcupitt.Vips.ImageAutodoc.html#method_text
不幸的是,phpdoc 标记不允许我们为选项生成文档。您需要在此处参考完整的 libvips 文档:
https://libvips.github.io/libvips/API/current/libvips-create.html#vips-text
我正在尝试使用 php-vips 库编写一个 PHP 函数来将文本叠加到图像上。查看文档,我在 libvips 文档 here and the php-vips documentation here doesn't provide a ton of detail and seems to just direct you to use the libvips documentation. I found a snippet in one of php-vips issues (this) 中找不到绘制文本的函数,但它使用了当前 php-vips 库中不存在的文本函数。有谁知道是否可以使用 php-vips 将文本绘制到图像上,如果可以的话是如何完成的?作为参考,我的用例是在通过 PDF 下载时在照片上绘制照片的时间戳。
我给你做了一个演示程序:
#!/usr/bin/php
<?php
require __DIR__ . '/vendor/autoload.php';
use Jcupitt\Vips;
$image = Vips\Image::newFromFile($argv[1], ['access' => 'sequential']);
// this renders the text to a one-band image ... set width to the pixels across
// of the area we want to render to to have it break lines for you
$text = Vips\Image::text('Hello world!', [
'font' => 'sans 120',
'width' => $image->width - 100
]);
// make a constant image the size of $text, but with every pixel red ... tag it
// as srgb
$red = $text->newFromImage([255, 0, 0])->copy(['interpretation' => 'srgb']);
// use the text mask as the alpha for the constant red image
$overlay = $red->bandjoin($text);
// composite the text on the image
$out = $image->composite($overlay, "over", ['x' => 100, 'y' => 100]);
$out->writeToFile($argv[2]);
我可以运行这样:
$ ./render_text.php ~/pics/tiny_marble.jpg x.jpg
制作:
文本方法的文档在这里:
https://libvips.github.io/php-vips/docs/classes/Jcupitt.Vips.ImageAutodoc.html#method_text
不幸的是,phpdoc 标记不允许我们为选项生成文档。您需要在此处参考完整的 libvips 文档:
https://libvips.github.io/libvips/API/current/libvips-create.html#vips-text