php-imagine / Imagine

PHP Object Oriented image manipulation library
https://imagine.readthedocs.io
Other
4.42k stars 529 forks source link

Hello! How can i set textAlign? #724

Closed kaunnikov closed 3 years ago

kaunnikov commented 5 years ago

I use: `

    $imagine = new Imagine();

    $fontContent = $imagine->font($fontPathContent, 50, $palette->color('#000'));

    $image = $imagine->open(Yii::getAlias('@common/files/templates/1.png'));

    $image->draw()->text($value, $fontContent, $positionContent, 0, 1650);

`

ausi commented 5 years ago

Setting a text align doesn’t seem possible I think.

You can use $fontContent->wrapText(…); to split the text into lines and position every single line separately.

$lines = explode("\n", $fontContent->wrapText($value, 1650, 0));
foreach ($lines as $lineNumber => $line) {
    $image->draw()->text(
        $line, 
        $fontContent, 
        new Point(
            1650 - $fontContent->box($line)->getWidth(),
            $lineNumber * 50
        )
    );
}