首页 > 解决方案 > mxGraph,将XML文件转换为PNG以导出图像的php代码

问题描述

我正在尝试使用 PHP 从 mxGraph 导出图像文件 (PNG)。我成功地将图形的 XML 字符串发送到 PHP 文件,我可以使用 mxUtils::parseXml(); 解析 XML 图书馆的指挥。我收到的输出仍然不可读。这是一个图像文件,但里面什么都没有。你能帮助我吗?这是我正在使用的 php 代码:

$xml = mxUtils::readFile("graphmodel.xml");
$doc = mxUtils::parseXml($xml);

$model = new mxGraphModel();
$graph = new mxGraph($model);

$codec = new mxCodec($doc);
$codec->decode($doc->documentElement, $graph->model);

$image = $graph->createImage(null, "#FFFFFF");

imageInterlace($image, 1);
imageColorTransparent($image, imageColorAllocate($image, 255, 255, 255));

header("Content-Type: image/png");

echo mxUtils::encodeImage($image, 'png');

我不明白问题出在哪里。这是我上传的 XML 文件:

<mxGraphModel dx="876" dy="502" grid="1" gridSize="10" guides="1" tooltips="1" connect="1" arrows="1" fold="1" page="1" pageScale="1" pageWidth="850" pageHeight="1100">
<root>
<mxCell id="0"/>
<mxCell id="1" parent="0"/>
<mxCell id="2" value="CIAO" style="rounded=0;whiteSpace=wrap;html=1;" vertex="1" parent="1">
<mxGeometry x="20" y="20" width="120" height="60" as="geometry"/>
</mxCell>
<mxCell id="4" value="Bella" style="rounded=0;whiteSpace=wrap;html=1;" vertex="1" parent="1">
<mxGeometry x="250" y="20" width="120" height="60" as="geometry"/>
</mxCell>
<mxCell id="5" value="" style="endArrow=classic;startArrow=classic;html=1;entryX=0;entryY=0.5;exitX=1;exitY=0.5;" edge="1" parent="1" source="2" target="4">
<mxGeometry width="50" height="50" relative="1" as="geometry">
<mxPoint x="20" y="150" as="sourcePoint"/>
<mxPoint x="70" y="100" as="targetPoint"/>
</mxGeometry>
</mxCell>
</root>
</mxGraphModel>

感谢你给与我的帮助!

标签: phpxml-parsingmxgraph

解决方案


推荐阅读