【发布时间】:2012-12-28 14:10:52
【问题描述】:
我正在尝试将 SVG 转换为 PNG,但我遇到了问题。
通过此链接,您可以上传 SVG,文件将显示结果: http://clound.com.br/labs/svg/export/imagick/index.php
我要上传的 SVG 是这样的: http://clound.com.br/labs/svg/export/images/cover.svg
我在上传页面的代码是这样的:
<?php
if ( $_FILES ){
$target_path = 'output/'. basename( $_FILES['uploadedfile']['name']);
$mime = $_FILES['uploadedfile']['type'];
if ( $_FILES['uploadedfile']['error'] ){
die( 'Error on upload.');
}
if ( move_uploaded_file( $_FILES['uploadedfile']['tmp_name'], $target_path ) ){
echo '<embed style="border:solid 1px gray;" src="'.$target_path.'" type="image/svg+xml" pluginspage="http://www.adobe.com/svg/viewer/install/" /><br / >';
$im = new Imagick();
$svg = file_get_contents($target_path);
/*loop to color each state as needed, something like*/
$dom = new DOMDocument();
$dom->loadXML( $svg );
removeFillRule($dom, 'g');
removeFillRule($dom, 'mask');
removeFillRule($dom, 'path');
removeFillRule($dom, 'image');
$svg = $dom->saveXML();
echo $svg;
$im->readImageBlob($svg);
/*png settings*/
$im->setImageFormat("png32");
$im->writeImage('output/image.png');/*(or .jpg)*/
$im->clear();
$im->destroy();
echo '<img src="output/image.png"/><br/>';
}
else
{
echo "There was an error uploading the file, verify permission and try again!";
}
}
function remove_children(&$node) {
while ($node->firstChild) {
while ($node->firstChild->firstChild) {
remove_children($node->firstChild);
}
$node->removeChild($node->firstChild);
}
}
function removeFillRule($dom, $element){
$path = $dom->getElementsByTagName($element);
foreach ($path as $key=> $value) {
$path->item($key)->removeAttribute('fill-rule');
}
}
我在服务器上的版本是这样的:
ImageMagick 6.7.6-8 2012-05-02 Q16
http://www.imagemagick.org
【问题讨论】:
-
你是有错误还是别的什么?
-
不,您可以尝试上传我在链接上发布的图像...我认为我的 SVG 源代码有问题...它是由 BonsaiJS 生成的,但如果您尝试保存这张图片clound.com.br/labs/svg/export/images/cover.svg 它什么也没有显示...
-
我用这个 svg 文件 w3schools.com/svg/circle1.svg 尝试了你的代码,从你使用它的函数 removeChild() 得到错误?
-
我已经删除了删除导致错误的 defs 标签的代码。再次尝试上传,如您所见,红色圆圈将被上传,而我的封面仍然是黑色图像。
标签: php svg imagemagick imagick