php-svg
php-svg copied to clipboard
SVG not being generated.
Hello guys, I am creating svg based on text and creating it with font ttf file. But it need y axis to be set around 90, 150 etc. Text svg is not being generated properly. Can anyone help me to resolve this?
$f_size = 228;
$size = "16 CM x 39 CM";
$svg_width = 608; //16*38
$svg_height = 1482; //39*38 CM to px
$font_size = $f_size ."px";
$font_name = "PT Sans";
$font_path = "fonts/".str_replace(" ","-",$font_name).".ttf";
$texts = "This#Is#User#Test";
$texts = explode("#",$texts);
$font_color = "#fcd90e";
$font_weight = "normal";
$textanchor = "center";
$y = 90; // HERE HELP NEEDED
$transform = "";
if($transform == "mirror"){
$transform = "scale(-1, 1)";
if($textanchor == "center"){
$x = -intval($svg_width/2)."px";
$Tanchor = "middle";
}elseif($textanchor == "right"){
$x = -10 ."px";
$Tanchor = "end";
}else{
$x = -intval($svg_width) + 10 ."px";
$Tanchor = "start";
}
}else{
if($textanchor == "center"){
$x = intval($svg_width/2)."px";
$Tanchor = "middle";
}elseif($textanchor == "right"){
$x = intval($svg_width)."px";
$Tanchor = "end";
}else{
$x = '10px';
$Tanchor = "start";
}
}
$svg = new \SVG\SVG($svg_width, $svg_height);
$font = new \SVG\Nodes\Structures\SVGFont($font_name, $font_path);
$svg->getDocument()->addChild($font);
foreach($texts as $text){
$svg->getDocument()->addChild(
(new \SVG\Nodes\Texts\SVGText($text, $x, $y, $transform) )
->setFont($font)
->setSize($font_size)
->setColor($font_color)
->setWeight($font_weight)
->setTextAnchor($Tanchor)
);
$y = intval($y) + intval($font_size) + 5;
}
$image = SVG::fromString($svg);
header('Content-Type: image/svg+xml');
echo $image;
Here is the code I am referring to. Setting up $y is main problem, Sometimes text can be in 3 lines and sometimes those can be in 4 lines. Any idea how to make it. ?