Calculating SVG bounding box using PHP with respect to curves -
i found amazing class located here, , tried using it.
however, works of basic functions such move, horizontal line, , vertical line.
--
i have tried extending existing class adding additional checks (and changing regex).
public static function frompath($pathstring) { preg_match_all('/([mlvhzc][^mlvhzc]*)/i', $pathstring, $commands); $pt = array(0, 0); $bounds = new self(); foreach ($commands[0] $command) { preg_match_all('/((\+|-)?\d+(\.\d+)?(e(\+|-)?\d+)?)/i', $command, $matches); $i = 0; while ($i < count($matches[1])) { switch ($command[0]) { case 'm' : case 'l' : $pt[0] += $matches[1][$i++]; $pt[1] += $matches[1][$i++]; break; case 'm' : case 'l' : $pt[0] = $matches[1][$i++]; $pt[1] = $matches[1][$i++]; $last=$pt; break; case 'v' : $pt[1] += $matches[1][$i++]; break; case 'v' : $pt[1] = $matches[1][$i++]; $last[1]=$pt[1]; break; case 'h' : $pt[0] += $matches[1][$i++]; break; case 'h' : $pt[0] = $matches[1][$i++]; $last[0]=$pt[0]; break; case 'z' : case 'z' : break; case 'c': $pt[0] = $last[0]+$matches[1][4]; $pt[1] = $last[1]+$matches[1][5]; $last=$pt; $i=count($matches[1]); break; default : throw new runtimeexception("unhandled path command: " . $command[0]); } $bounds->extend($pt[0], $pt[1]); } } return $bounds; }
i checked out svg manual , found out 'c' has 6 parameters, knowing last 2 curve end at, tried extend points based on that...
for moment, tests based around this:
<svg xmlns="http://www.w3.org/2000/svg" width="109" height="109" viewbox="0 0 109 109"> <g style="fill:none;stroke:#000000;stroke-width:3;stroke-linecap:round;stroke-linejoin:round;"> <path d="m32.25,41c1.25,0.62,3.12,0.67,5.5,0.5c7.12-0.5,19.12-2.5,24-3c0.99-0.1,2.62-0.25,3.75,0" /> </g>
when run in browser, chrome reports it's width height ratio (because know svg doesn't have sizes), around 5 6, however, when find ratio script, it's off.
i'd know if there's svg class built supports functions (c,c,q,q,etc).
i know there's way box converting image, feel inefficient, there's getbbox in javascript, i'd perform calculations on server.
thanks reading!
here example using imagick, 2 example in 1 since cannot run @ same time, uncomment 1 @ time:
$svg = '<?xml version="1.0" encoding="utf-8" standalone="no"?> <svg xmlns="http://www.w3.org/2000/svg" width="109" height="109" viewbox="0 0 109 109"> <g style="fill:none;stroke:#000000;stroke-width:3;stroke-linecap:round;stroke-linejoin:round;"> <path d="m32.25,41c1.25,0.62,3.12,0.67,5.5,0.5c7.12-0.5,19.12-2.5,24-3c0.99-0.1,2.62-0.25,3.75,0" /> </g> </svg>'; $im = new imagick(); $im->readimageblob($svg); $im->trimimage (0);//this trims unecessary blank space. //this block gets dimensions (comment block before uncommenting second example bellow) $dimension = $im->getimagegeometry(); print_r('<pre>'); print_r($dimension); die(); /*//uncomment block view thw jpeg version of svg $im->setimageformat("jpeg"); header("content-type: image/jpeg"); $thumbnail = $im->getimageblob(); echo $thumbnail; $im->clear(); $im->destroy(); //*/
Comments
Post a Comment