想象一下渲染黑色图像

时间:2012-12-28 14:10:53

标签: php svg imagemagick imagick

我正在尝试将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

1 个答案:

答案 0 :(得分:0)

删除填充规则?这会破坏一些svgs,只是说。我很好奇,这是imagemagick中某些bug的解决方法吗?

无论如何,猜测是你的问题是imagemagick可能不支持填充/描边中的rgba语法 - 默认情况下会使填充变黑。