xslt中的php getimagesize

xslt中的php getimagesize,php,xslt,Php,Xslt,使用xslt时,我有点不自在 我想得到图像的高度和宽度之间的正比例。但我甚至在获取参数方面都有困难 我试过这个: <xsl:value-of select="php:functionString('getimagesize', image)"/></xsl:element> 但这当然只是输出“数组”。 有没有一种方法可以“打破”类似于$size[1]的数组?您可以在PHP代码中创建一个DOMDocument或文档片段,其中包含要返回的数据,然后可以在XSLT端使用X

使用xslt时,我有点不自在

我想得到图像的高度和宽度之间的正比例。但我甚至在获取参数方面都有困难

我试过这个:

<xsl:value-of select="php:functionString('getimagesize', image)"/></xsl:element>

但这当然只是输出“数组”。
有没有一种方法可以“打破”类似于$size[1]的数组?

您可以在PHP代码中创建一个DOMDocument或文档片段,其中包含要返回的数据,然后可以在XSLT端使用XPath来选择数据,下面是一个示例:

<?php



function getDims($url) {
    $info = getimagesize($url);
    $doc = new DOMDocument();
    $root = $doc->appendChild($doc->createElement('dimensions'));
    $doc->appendChild($root);
    $width = $doc->createElement('width', $info[0]);
    $root->appendChild($width);
    $height = $doc->createElement('height', $info[1]);
    $root->appendChild($height);
    return $doc;
}


$xml = <<<'EOB'
<root>
  <image>foo.gif</image>
</root>
EOB;

$doc = new DOMDocument();
$doc->loadXML($xml);

$xsl = <<<'EOB'
<xsl:stylesheet version="1.0" 
     xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
     xmlns:exsl="http://exslt.org/common"
     xmlns:php="http://php.net/xsl"
     exclude-result-prefixes="exsl php">

<xsl:output method="html" encoding="utf-8" indent="yes"/>


 <xsl:template match="image">
   <xsl:variable name="dimensions" select="php:function('getDims', string(.))/*"/>
   <img width="{$dimensions/width}" height="{$dimensions/height}" src="{.}"/>
 </xsl:template>
</xsl:stylesheet>
EOB;

$xsldoc = new DOMDocument();
$xsldoc->loadXML($xsl);

$proc = new XSLTProcessor();
$proc->registerPHPFunctions();

$proc->importStyleSheet($xsldoc);
echo $proc->transformToXML($doc);


?>