我试图将SVG转换为PNG,但我对它有困难。Imagick渲染黑色图像
通过此链接,您可以上传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
你有错误还是别的? – j0k
不,您可以尝试上传我发布在链接上的图片... 我认为我的SVG源代码有问题...它是由BonsaiJS生成的,但如果您尝试保存此图片http ://clound.com.br/labs/svg/export/images/cover.svg它不会显示任何内容... –
我尝试了您的代码与此svg文件http://www.w3schools.com/svg/ circle1.svg从函数removeChild()中获取错误,您在哪里使用它? – Mateusz