Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/267.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中使用Imagick将svg转换为png时出现奇怪的裁剪问题_Php_Svg_Png_Imagick_Inkscape - Fatal编程技术网

在PHP中使用Imagick将svg转换为png时出现奇怪的裁剪问题

在PHP中使用Imagick将svg转换为png时出现奇怪的裁剪问题,php,svg,png,imagick,inkscape,Php,Svg,Png,Imagick,Inkscape,我正在尝试将一组svg图像转换为png。下面是Imagick部分的片段: $im = new Imagick(); $im->setBackgroundColor(new ImagickPixel($options->iconsBgColor)); //$options->iconsBgColor is like #00ff00 $im->readImageBlob( $svg ); $im->setImageResolution(288,288); $im->

我正在尝试将一组svg图像转换为png。下面是Imagick部分的片段:

$im = new Imagick();
$im->setBackgroundColor(new ImagickPixel($options->iconsBgColor)); //$options->iconsBgColor is like #00ff00
$im->readImageBlob( $svg );
$im->setImageResolution(288,288);
$im->resizeImage(512, 512, imagick::FILTER_LANCZOS, 1);
$im->setImageFormat( "png32" );
$im->setImageCompression(\Imagick::COMPRESSION_UNDEFINED);
$im->setImageCompressionQuality(0);
$im->writeImage( $imageSrcPngDir );
$im->clear();
$im->destroy();
这基本上和预期的一样有效。但是在图像的顶部和底部有一个很大的空白区域的图像中,它会在png文件的底部被裁剪。如以下示例所示:

以下是上述png的svg:

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<svg filter="url(#shadow)" style="width:512px; height:512px;" xmlns="http://www.w3.org/2000/svg" viewBox="-215 -215 942 942">
    <defs>
        <style type="text/css">.cls-1{fill:none;stroke:#0000ff;stroke-miterlimit:10;stroke-width:18px;}</style>
        <filter id="shadow" x="0" y="0" width="200%" height="200%">
            <feGaussianBlur in="SourceAlpha" stdDeviation="11.636363636364"/>
            <feOffset dx="17.454545454546" dy="17.454545454546" result="offsetblur"/>
            <feFlood flood-color="#04040C" flood-opacity="1"/>
            <feComposite in2="offsetblur" operator="in"/>
            <feMerge>
                <feMergeNode/>
                <feMergeNode in="SourceGraphic"/>
            </feMerge>
        </filter>
    </defs>
    <title>imdb-icon</title>
    <g id="imdb-icon">
        <g id="imdb-icon-2" data-name="imdb-icon">
            <path class="cls-1" d="M506,327.03028v-92.721c-4.04258-27.411-33.82293-27.27624-33.82293-27.27624-15.82225-.146-26.0971,6.27726-32.20589,12.24007V157.67979H386.74382v193.606h48.7356a24.443,24.443,0,0,1,1.97638-11.88071c5.6147,6.513,16.4735,15.08106,34.72127,14.91266C472.17707,354.31776,506,354.32222,506,327.03028Zm-48.91524-15.96816a12.39325,12.39325,0,0,1-12.39722,12.39722H437.9499V236.13963h6.73764a12.39321,12.39321,0,0,1,12.39722,12.39723Z"/>
            <path class="cls-1" d="M339.32932,157.6843H250.7142V351.31552h88.61512a31.23974,31.23974,0,0,0,31.2418-31.2418V188.92611A31.23971,31.23971,0,0,0,339.32932,157.6843ZM320.94825,303.87277a15.40325,15.40325,0,0,1-15.40086,15.41087h-3.80027V191.73627h3.80027a15.40328,15.40328,0,0,1,15.40086,15.41086Z"/>
            <polygon class="cls-1" points="233.833 157.68 233.833 351.286 189.062 351.286 189.062 228.346 170.151 351.286 139.978 351.286 120.944 227.538 120.944 351.286 74.185 351.286 74.185 157.68 141.191 157.68 154.666 245.28 168.141 157.68 233.833 157.68"/>
            <rect class="cls-1" x="6" y="157.67984" width="53.15995" height="193.63967"/>
        </g>
    </g>
</svg>

.cls-1{填充:无;笔划:#0000ff;笔划斜接限制:10;笔划宽度:18px;}
imdb图标
现在,当我通过操纵viewBox属性增加图形的大小来减少顶部和底部的填充时,图像会按预期进行渲染,如下所示

下面是上述png的svg(这正按预期工作):


.cls-1{填充:无;笔划:#0000ff;笔划斜接限制:10;笔划宽度:18px;}
imdb图标
我比较了两个svg文件,viewBox是唯一不同的地方。


知道如何解决这个问题吗?好的,我对修改后的viewBox的怀疑是没有根据的。看起来不错


但是你添加的过滤效果是怎么回事?你没提那件事。它基本上是一个未配置/配置错误的阴影过滤器。但它缺少很多属性。例如,
滤芯中未设置
标准偏差
。ImageMagick可能不喜欢这样。当您从SVG中移除过滤器时,它是否仍然会被剪裁(即移除
filter=“url(#shadow)”
)?

过滤器的高度从200%增加到300%解决了问题

<filter id="shadow" x="0" y="0" width="200%" height="300%">


我的第一个猜测是,当您使用“regex操作viewBox”时,您将使viewBox小于实际文档内容。但是,除非您为顶部图像提供修改过的SVG代码,否则我们无法真正判断。@PaulLeBeau,谢谢。给我一点时间,我将用修改后的svg更新问题。@PaulLeBeau我已经用实际呈现的svg更新了问题。您好,很抱歉回复太晚。您是对的,
是导致问题的原因,没有它渲染工作正常。但实际上,我也让用户添加一个可选的阴影。在这种情况下,问题仍然存在。我用新的信息更新了整个问题。请看一看。@Roy我认为这可能仍然与ImageMagicks处理过滤器有关。或者ImageMagick用于SVG渲染的任何内容。你可能想向他们提交一个bug——如果还没有的话。
<filter id="shadow" x="0" y="0" width="200%" height="300%">