[英]Imagick rendering a black image
我正在嘗試將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。 我很好奇,這是imagemagick中一些錯誤的解決方法嗎?
無論如何,一個猜測是您的問題是imagemagick可能在填充/描邊中不支持rgba語法-默認情況下會使填充變黑。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.