最近、この素晴らしいクラスがここにあるのを見つけて、使ってみました。
ただし、移動、水平線、垂直線などの一部の基本機能でしか機能しません。
--
追加のチェックを追加して(および正規表現を変更して)、この既存のクラスを拡張しようとしました。
public static function fromPath($pathString) {
preg_match_all('/([mlvhzc][^mlvhzc]*)/i', $pathString, $commands);
$pt = array(0, 0);
$bounds = new self();
foreach ($commands[0] as $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;
}
SVG マニュアルを調べたところ、「c」には 6 つのパラメーターしかないことがわかりました。最後の 2 つは曲線が終了する場所であることを知っていたので、それに基づいてポイントを拡張しようとしました...
今のところ、私のテストはこれに基づいています。
<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>
ブラウザーで実行すると、Chrome は幅と高さの比率 (svg には正確なサイズがないことを知っているため) が約 5 から 6 であると報告しますが、スクリプトで比率を見つけると、完全にオフになります。
すべての関数 (C、c、Q、q など) をサポートする別の svg クラスが構築されているかどうかを知りたいです。
ボックスを画像に変換して取得する方法があることは知っていますが、それは効率が悪いと感じています。また、javascript には getBBox がありますが、サーバー上で計算を実行したいと考えています。
読んでくれてありがとう!