我正在使用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);

烙印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此外,您可以使用變量設置字體大小。
- 2 回答
- 0 關注
- 163 瀏覽
添加回答
舉報
0/150
提交
取消