【发布时间】:2012-03-26 15:20:55
【问题描述】:
我有 SVG 图像,右侧和底部有很多空白。如何使用 PHP 和 imagick 裁剪图像(给定固定大小,所有图像将具有相同大小),并将它们保存回相同的文件?
【问题讨论】:
-
您可以使用 PHP 库 contao/imagine-svg 来裁剪和调整 SVG 图像的大小。
我有 SVG 图像,右侧和底部有很多空白。如何使用 PHP 和 imagick 裁剪图像(给定固定大小,所有图像将具有相同大小),并将它们保存回相同的文件?
【问题讨论】:
您根本不需要 imagick。由于 SVG 是一种 XML 格式,您可以将文档加载到 DOMDocument 对象中并更改 svg 标签的宽度和高度属性。
这是一个例子(svg 文件是从jenkov.com借来的):
<?php
$svg = <<< EOF
<svg xmlns="http://www.w3.org/2000/svg"
xmlns:xlink="http://www.w3.org/1999/xlink">
<rect x="50" y="50" height="110" width="110"
style="stroke:#ff0000; fill: #ccccff"
transform="translate(30) rotate(45 50 50)"
>
</rect>
<text x="70" y="100"
transform="translate(30) rotate(45 50 50)"
>Hello World</text>
</svg>
EOF;
$myWidth = 100;
$myHeight = 150;
$dom = new DOMDocument();
$dom->loadXML($svg);
$svg = $dom->getElementsByTagName('svg');
$svg->item(0)->setAttribute('width', $myWidth);
$svg->item(0)->setAttribute('height', $myHeight);
print $dom->saveXML($svg->item(0))."\n";
【讨论】:
【讨论】: