php-svg
php-svg copied to clipboard
How to set fonts for rasterization on existing documents?
when rasterizing svg, all the texts are missing in the png file. is it implemented at all to rasterize texts ?
sorry, was just missing a font. You can delete this issue. works with this, ->setFont(new SVGFont('arial', './arial.ttf'))
Dear Developers and Users,
I have the similar problem and I couldn't figure how to use setFont()
from outside.
How can I inject my font name (and location on server) into $params['font_path']
?
My texts have font-family
attribute in original SVG $svg
source:
$image = \SVG\SVG::fromString($svg);
$doc = $image->getDocument();
$rasterImage = $image->toRasterImage($doc->getWidth(), $doc->getHeight());
ob_start();
imagepng($rasterImage);
$string = ob_get_clean(); // raw image string for base64 encoding
Do you have any suggestions? Thank you for reading!
@r3sist Did you find a solution to this? Is it at all possible to use custom fonts with this library?
@notflip No, sorry. I switched to Imagick with inkscape engine for rasterization process. Inkscape is important for SVG font rendering. It also handles rotated texts.
$image = new Imagick();
$image->setFont(__DIR__ . '/Sans-Serif.ttf');
$image->readImageBlob($svgString);
$image->setImageFormat('png');
$image->getImageBlob()
I was using Imagick as well for rendering the image! Did not know about the setFont() method, with this, do you now have a working png generator with fonts? Did you have to any special settings to Imagick? Thanks!
Yes I have a working project. Comparison example - SVG left
As far as I know, Imagick will use automatically Inkscape if it's installed.
In my project notes I found (for Ubuntua 20.04):
Imagick
readImageBlob()
needs:apt-get install ghostscript apt-get install libgraphicsmagick1-dev libmagickcore-dev libmagickcore-6.q16-2 libmagickcore-6.q16-2-extr
Replace built-in SVG processor with inkscape:
apt-get install inkscape
Thanks! This is of great help, the only thing I don't get is if you define the font with setFont in Imagick, do you still need to set the font using this library's methods for doing that, for example
$text = (new SVGText('Hello World', '50%', '50%'))
->setFont(new SVGFont('Montserrat-ExtraBoldItalic', $fontPath));
I think you should test it. I have font attrinbutes in raw SVG, so I guess yes. However I use only generic CSS font types like Sans-Serif (font-family="Sans-Serif"
in SVG source) and I have a .ttf file called like this (see above comment). (Disclamier: I don't always use this lib for generating SVG)
I've implemented a new system for registering fonts statically on the SVG
class. The font to use for rendering is then automatically chosen as the best match. See PR #191 and please let me know what you think!