【问题标题】:ImageMagick convert a SVG file results a blank imageImageMagick 转换 SVG 文件会导致空白图像
【发布时间】:2015-06-30 09:47:27
【问题描述】:

我正在尝试使用以下命令将带有 xlink:href 的 SVG 文件转换为 ImageMagick 内部的图像:

convert file.png result.png

但这给了我一个空白的png文件,我确定问题来自图像,因为其他说明显示在png文件中。我可以使用以下命令使其工作:

convert msvg:file.svg result.png

有人知道如何在没有“msvg:”部分的情况下完成这项工作吗?因为之后我必须使用 php-Imagick 来执行此操作,并且我无法在我的 php 代码中添加“msvg:”。

SVG 文件:

<?xml version="1.0"?>
<svg id="svg" width="1024" height="1024" preserveAspectRatio="xMinYMin">
    <image id="svg-image" x="0" y="0" width="1024" height="1024" product_id="4" xlink:href="coeur.png">
    </image>
    <rect x="10" y="10" width="80" height="80" fill="red"/>
</svg>

ImageMagick 版本:6.7.7.10-5+deb7u3

“转换列表格式 | grep SVG”的输出:

MSVG  SVG       rw+   ImageMagick's own SVG internal renderer
SVG  SVG       rw+   Scalable Vector Graphics (RSVG 2.36.1)
SVGZ  SVG       rw+   Compressed Scalable Vector Graphics (RSVG 2.36.1)

php代码:

$im = new Imagick();
$draw = new ImagickDraw();
$im->readImageBlob($svgParse->asXML());
$im->setImagePage(0, 0, 0, 0);
$im->scaleImage($viewBox['2'],$viewBox['3'], false);
foreach($ListText as $text){
   $draw->setFont(Mage::getBaseDir('media').DS.'font'.DS.$text['font-family'].".ttf");
   $draw->setFontSize( $text['font-size'] );
   $draw->setFillColor ( $text['fill']);
   $im->annotateImage($draw, $text['x'], $text['y'], 0, $text['text']);
}
/*png settings*/
$im->setImageFormat("png32");
// FB image
$imFB = clone $im;
$imFB->resizeImage(1200,630,Imagick::FILTER_LANCZOS,1);
$imFB->writeImage($filename_FB);
$imFB->clear();
$imFB->destroy();
// TW image
$imTW = clone $im;
$imTW->resizeImage(280,150,Imagick::FILTER_LANCZOS,1);
$imTW->writeImage($filename_TW);
$imTW->clear();
$imTW->destroy();

$im->writeImage($filename);/*(or .jpg)*/

$im->clear();
$im->destroy();

【问题讨论】:

  • 如果您使用的是imagick,能否也发布您当前的PHP代码?
  • 我在帖子中添加了我的 PHP 代码,因为它对于 cmets 来说太长了
  • 为了安全起见,rsvg 委托不加载外部图像资源。在使用 $img-&gt;setFormat('MSVG'); 读取 blob 日期之前尝试设置 MSVG 格式
  • 你的回答解决了我的问题,谢谢你,你是我今天的救星:)

标签: imagemagick


【解决方案1】:

emcconvilutionle 的答案是解决方案,我只需要写

$im->setFormat('MSVG');

之前

$im->readImageBlob($svgParse->asXML());

【讨论】:

  • 很高兴为您提供帮助,欢迎来到 StackOverflow。请接受您自己的答案,以便其他有类似问题的人知道此解决方案有效。
猜你喜欢
  • 2018-12-30
  • 1970-01-01
  • 2012-09-08
  • 1970-01-01
  • 2017-04-22
  • 2020-01-25
  • 2013-05-18
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多