【问题标题】:ImageMagick - "unable to draw image" - using SVGImageMagick - “无法绘制图像” - 使用 SVG
【发布时间】:2017-03-10 01:38:41
【问题描述】:

我想使用 SVG 字符串创建矢量 PDF。但是 ImageMagick 绘制函数总是返回 'unable to draw image' 500 错误。

    $image = new \Imagick();
    $draw = new \ImagickDraw();
    $draw->setVectorGraphics('<root>'.$svg.'</root>');
    $image->newImage(1052, 697, $bg);
    $image->setColorspace(\Imagick::COLORSPACE_CMYK);

    $image->setImageResolution(300,300);
    $image->setImageFormat('pdf');

    $image->drawImage($draw);

    file_put_contents('image.pdf', $image);

我只尝试了简单的绘图,而不是来自 SVG 源,这也不起作用。

希望有人可以提供帮助。谢谢

我的 SVG:

<?xml version="1.0" encoding="UTF-8" standalone="no" ?>
<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">
<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" version="1.1" width="1052" height="697" style="background-color: #fff" viewBox="0 0 1052 697" xml:space="preserve">
<desc>Created with Fabric.js 1.6.6</desc>
<defs></defs>
<line x1="NaN" y1="NaN" x2="NaN" y2="NaN" style="stroke: none; stroke-width: 1; stroke-dasharray: none; stroke-linecap: butt; stroke-linejoin: miter; stroke-miterlimit: 10; fill: rgb(0,0,0); fill-rule: nonzero; opacity: 1;" transform="translate(NaN NaN)"/>
<rect x="-50" y="-50" rx="0" ry="0" width="100" height="100" style="stroke: none; stroke-width: 1; stroke-dasharray: none; stroke-linecap: butt; stroke-linejoin: miter; stroke-miterlimit: 10; fill: rgb(155,89,182); fill-rule: nonzero; opacity: 1;" transform="translate(576.5 399)"/>
<polygon points="-50 50,0 -50,50 50" style="stroke: none; stroke-width: 1; stroke-dasharray: none; stroke-linecap: butt; stroke-linejoin: miter; stroke-miterlimit: 10; fill: rgb(241,196,15); fill-rule: nonzero; opacity: 1;" transform="translate(236.5 257)"/></svg>

【问题讨论】:

    标签: php svg imagick


    【解决方案1】:

    我想知道您的 SVG 字符串是否与 setVectorGraphics() 兼容。我将 getVectorGraphics() 与用法示例 here at php.net imagickdraw setvectorgraphics 一起使用,并使用生成的 $SVG,我能够使您的 sn-p 工作:

    $svg = "<drawing-wand/><clip-path/><clip-units>Undefined</clip-units><decorate>None</decorate><encoding/><fill>#00000000FFFF</fill><fill-opacity>1</fill-opacity><fill-rule>Evenodd</fill-rule><font/><font-family/><font-size>12</font-size><font-stretch>Undefined</font-stretch><font-style>Undefined</font-style><font-weight>0</font-weight><gravity>Undefined</gravity><stroke>#FFFFFFFFFFFF</stroke><stroke-antialias>1</stroke-antialias><stroke-dasharray/><stroke-dashoffset>0</stroke-dashoffset><stroke-linecap>Butt</stroke-linecap><stroke-linejoin>Miter</stroke-linejoin><stroke-miterlimit>10</stroke-miterlimit><stroke-opacity>0</stroke-opacity><stroke-width>1</stroke-width><text-align>Undefined</text-align><text-antialias>1</text-antialias><text-undercolor>#000000000000</text-undercolor><vector-graphics>fill '#FFFF00000000'
    circle 20,20 50,50
    fill '#00000000FFFF'
    circle 50,70 50,50
    rectangle 50,120 80,150
    </vector-graphics>";
    $bg = new ImagickPixel("white");
    
    $image = new \Imagick();
    $draw = new \ImagickDraw();
    $draw->setVectorGraphics("<root>$svg</root>");
    
    $image->newImage(1052, 697, $bg);
    $image->setColorspace(\Imagick::COLORSPACE_CMYK);
    
    $image->setImageResolution(300,300);
    $image->setImageFormat('pdf');
    
    $image->drawImage($draw);
    
    file_put_contents('image.pdf', $image);
    

    【讨论】:

    • 我认为它不兼容。我使用 toSVG() 从 fabric.js 传递它我添加了我的 svg 字符串
    • 由于您的代码 sn-p 有效,如果我已经回答了您的问题,您能否将其标记为解决方案?
    • 是的,对不起。你知道如何将 SVG 字符串转换为另一种格式吗?
    • 嗯,是的,这可能是一个不同的问题。我看到函数 setVectorGraphics 仍然是新的,所以我们必须找到关于它在寻找什么的其他信息。
    猜你喜欢
    • 2019-03-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-10-16
    • 2010-12-27
    相关资源
    最近更新 更多