php-svg icon indicating copy to clipboard operation
php-svg copied to clipboard

Text center not working for rasterized image

Open notflip opened this issue 4 years ago • 4 comments

I'm using the following code to rasterize an SVG image that has text

image

This is my code

        $image = new SVG(280, 280);
        $doc = $image->getDocument();

        $background = new SVGRect(0, 0, 280, 280);
        $background->setStyle('fill', '#ff4444');
        $doc->addChild($background);

        $text = (new SVGText($initials, '50%', '50%'))
            ->setFont(new SVGFont('Raleway', 'fonts/raleway.ttf'))
            ->setSize(120);

        $text->setStyle('line-height', 1);
        $text->setAttribute('text-anchor', 'middle');
        $text->setAttribute('alignment-baseline', 'middle');

        $doc->addChild($text);

I'm using 50% for the SVG text but it seems like the left bottom bounding box point is being used, instead of the center of the text node. Am I missing something?

Could it be that the following properties are ignored by the rasterize method?

$text->setAttribute('text-anchor', 'middle');
$text->setAttribute('alignment-baseline', 'middle');

Thank you!

notflip avatar Nov 07 '20 07:11 notflip

text-anchor added via #97. alignment-baseline still pending.

meyfa avatar Dec 04 '20 20:12 meyfa

hi, beside from this setFont(new SVGFont('Raleway', 'fonts/raleway.ttf')), what else need to do to make the font working? I tried it but seem the correct font is not applied.

bogart0324 avatar Dec 07 '20 02:12 bogart0324

@bogart0324 It's probably in the wrong folder? My fonts folder is located in my public folder, from which my php script (index.php) is being ran.

notflip avatar Dec 07 '20 15:12 notflip

yes same with me, I manage to make it work by replacing sprintf to printf in SVGFont class constructor, see image attached, I hope I din't mess it up :) [image: image.png]

On Mon, Dec 7, 2020 at 11:44 PM Miguel Stevens [email protected] wrote:

@bogart0324 https://github.com/bogart0324 It's probably in the wrong folder? My fonts folder is located in my public folder, from which my php script (index.php) is being ran.

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/meyfa/php-svg/issues/111#issuecomment-739999574, or unsubscribe https://github.com/notifications/unsubscribe-auth/ABVGDQPWZ3XWHXH2AMGINHLSTTZ6LANCNFSM4TNPHJ4Q .

bogart0324 avatar Dec 07 '20 15:12 bogart0324