猿问

Image-Workshop:图像中的中心文本

我正在使用PHP 7.1.33和"sybio/image-workshop": "^2.1"- PHPImageWorkshop - 创建/修改图像。


我目前正在生成如下图像:


<?php


require_once 'vendor/autoload.php';


use PHPImageWorkshop\ImageWorkshop;


$layer = ImageWorkshop::initFromPath(__DIR__.'\images\bank.jpg');


$layer->applyFilter(IMG_FILTER_CONTRAST, 40, null, null, null, true); // constrast

$layer->applyFilter(IMG_FILTER_BRIGHTNESS, 10, null, null, null, true); // brightness


// apply text

$text = "This is a test text";

$date = date("Y-m-d");


$font = 5; // Internal font number (http://php.net/manual/en/function.imagestring.php)

$color = "ffffff";

$positionX = 0;

$positionY = 0;

$align = "horizontal";


$layer->writeText($text, $font, $color, $positionX, $positionY, $align);


// Saving / showing...

$layer->save(__DIR__.'/output/', 'bank.jpg', true, null, 95);


我得到以下输出:

但是,我想得到:

http://img1.mukewang.com/62c8e0260001b56108540568.jpg

有什么建议如何指定字体并将文本以中间大字体居中?

感谢您的回复!


慕姐8265434
浏览 186回答 2
2回答

qq_笑_17

您需要先获取图层尺寸,然后将其除以 2$width = $layer->getWidth(); // in pixel$height = $layer->getHeight(); //&nbsp;$positionX = int($width / 2) ;$positionY = int($height / 2) ;$layer->writeText($text, $font, $color, $positionX, $positionY, $align);

烙印99

创建一个新的文本图层,然后将其放在现有图层的顶部。关键是'MM'进行居中的参数。$font = '/path/to/font.ttf';$size = 100;$color = "ffffff";$align = "horizontal";$textLayer = ImageWorkshop::initTextLayer($text, $font, $size, $color, 0, 0);$layer->addLayer(1, $textLayer, 0, 0, 'MM');$size此外,您可以使用变量设置字体大小。
随时随地看视频慕课网APP
我要回答