Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/295.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用PHP计算曲线的SVG边界框_Php_Svg_Bounding - Fatal编程技术网

使用PHP计算曲线的SVG边界框

使用PHP计算曲线的SVG边界框,php,svg,bounding,Php,Svg,Bounding,我最近发现了这个惊人的类,并尝试使用它 但是,它仅适用于一些基本功能,如移动、水平线和垂直线 -- 我尝试通过添加额外的检查(并更改正则表达式)来扩展这个现有类 公共静态函数fromPath($pathString){ preg_match_all('/([mlvhzc][^mlvhzc]*)/i',$pathString,$commands); $pt=数组(0,0); $bounds=新的self(); foreach($commands[0]作为$command){ pregêu matc

我最近发现了这个惊人的类,并尝试使用它

但是,它仅适用于一些基本功能,如移动、水平线和垂直线

--

我尝试通过添加额外的检查(并更改正则表达式)来扩展这个现有类

公共静态函数fromPath($pathString){
preg_match_all('/([mlvhzc][^mlvhzc]*)/i',$pathString,$commands);
$pt=数组(0,0);
$bounds=新的self();
foreach($commands[0]作为$command){
pregêu matchêu all('/(\+|-)?\d+(\.\d+)(e(+\+|-)?\d+)/i',$command,$matches);
$i=0;
而($iextend($pt[0],$pt[1]);
}
}
返回$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,但我希望在服务器上执行计算


谢谢你的阅读

这里是一个使用imagick的示例,实际上它是一个两个示例的组合,因为它们不能同时运行,同时取消注释一个:

$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 the unecessary blank space.

//This block gets the dimensions (comment this block before uncommenting the second example bellow)
$dimension = $im->getImageGeometry();
print_r('<pre>');
print_r($dimension);
die();


/*//Uncomment this block to view thw jpeg version of the svg
$im->setImageFormat("jpeg");
header("Content-Type: image/jpeg");
$thumbnail = $im->getImageBlob();
echo $thumbnail;
$im->clear();
$im->destroy();
//*/
$svg=”
';
$im=新的Imagick();
$im->readImageBlob($svg);
$im->trimImage(0)//这会修剪不必要的空白。
//此块获取维度(在取消注释下面的第二个示例之前注释此块)
$dimension=$im->getImageGeometry();
印刷品(“”);
打印(尺寸);
模具();
/*//取消对该块的注释以查看svg的jpeg版本
$im->setImageFormat(“jpeg”);
标题(“内容类型:图像/jpeg”);
$thumbnail=$im->getImageBlob();
回声$缩略图;
$im->clear();
$im->destroy();
//*/
对我帮助很大,但实际上并没有修剪SVG。以下是我根据他的回答得出的第一个解决方案:

function getTrimmedSvg( $filePath )
{
    $xml = simplexml_load_file( $filePath );

    $xml["viewBox"] = shell_exec( "cat $filePath | svg-bounding-box" );

    return $xml->asXML();
}
虽然这种方法大部分时间都有效,但并非所有时间都有效。在尝试修复边缘情况的大量徒劳工作之后,我转而使用名为()的nodejs命令行实用程序。这解决了我所有的边缘案件。在服务器和/或开发环境上全局安装svg边界框后,您可以在代码中以以下方式使用它:


你有没有尝试过imagick(),它不会将图像渲染为png/jpg?如果可能的话,我想用数学来做这些计算。它可以处理任何图像类型,使用它你可以很容易地准确地得到边界框,假设$image是一个imagick对象,函数$image->readImage('./some/path/image.svg');允许您读取和加载svg,您可以使用其他可用功能处理它们,您可以将其(加载的图像)转换为png/jpg或保留为svg,这取决于您。阅读php imagick,它将使您的生活更轻松。我正在从数据库加载svg,不保存文件,如何将图像导入imagick类,您可以尝试$image->readImageBlob(String imagestring);PHPImagick是一个非常强大的工具,您应该了解它。我相信这会帮你省去很多头痛。嗨!Imagick的响应是:宽度和高度为109,如果我从svg中删除这些属性,它将产生一个错误。它响应数组(“宽度”=>109,“高度”=>109)。您不是在寻找svg的宽度和高度吗?边界框,它被认为是svg在没有剪辑的情况下可以绘制的最小空间量。
function getTrimmedSvg( $filePath )
{
    $image = new Imagick();
    $image->readImage( $filePath );
    $image->trimImage( 0 );

    $imagePage = $image->getImagePage();
    $dimensions = $image->getImageGeometry();

    $minXOut = $imagePage['x'];
    $minYOut = $imagePage['y'];
    $widthOut = $dimensions["width"];
    $heightOut = $dimensions["height"];

    $xml = simplexml_load_file( $filePath );

    $xml["viewBox"] = "$minXOut $minYOut $widthOut $heightOut";

    return $xml->asXML();
}
function getTrimmedSvg( $filePath )
{
    $xml = simplexml_load_file( $filePath );

    $xml["viewBox"] = shell_exec( "cat $filePath | svg-bounding-box" );

    return $xml->asXML();
}