为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

PHP
慕姐8265434 2022-07-09 09:55:13
我正在使用PHP 7.1.33和"sybio/image-workshop": "^2.1"- PHPImageWorkshop - 创建/修改图像。我目前正在生成如下图像:<?phprequire_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);我得到以下输出:但是,我想得到:有什么建议如何指定字体并将文本以中间大字体居中?感谢您的回复!
查看完整描述

2 回答

?
qq_笑_17

TA贡献1818条经验 获得超7个赞

您需要先获取图层尺寸,然后将其除以 2


$width = $layer->getWidth(); // in pixel

$height = $layer->getHeight(); // 


$positionX = int($width / 2) ;

$positionY = int($height / 2) ;


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


查看完整回答
反对 回复 2022-07-09
?
烙印99

TA贡献1829条经验 获得超13个赞

创建一个新的文本图层,然后将其放在现有图层的顶部。关键是'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此外,您可以使用变量设置字体大小。


查看完整回答
反对 回复 2022-07-09
  • 2 回答
  • 0 关注
  • 185 浏览

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号