Replies: 2   Views: 1015
Get image alt text and size
Topic closed:
Please note this is an old forum thread. Information in this post may be out-to-date and/or erroneous.
Every phpdocx version includes new features and improvements. Previously unsupported features may have been added to newer releases, or past issues may have been corrected.
We encourage you to download the current phpdocx version and check the Documentation available.

Posted by bkidd  · 07-06-2020 - 13:04

I am currently using this to get the image alt text:

$referenceNode = array( 'customQuery' => '//w:drawing//wp:docPr', ); $contents = $docx->getDocxPathQueryInfo($referenceNode); foreach ($contents['elements'] as $content) { echo $content->getAttribute('descr'); }

I would also like to know the image dimensions.  I have tried to use:

$images = (new Indexer($this->getPath()))->getOutput()['body']['images'];

which has the dimensions but I don't have a way to match the results.

Any suggestions?  Thanks

Posted by admin  · 07-06-2020 - 16:12


There's no option to match Indexer and getDocxPathQueryInfo output, but you can use the following method to get the image size from getDocxPathQueryInfo

$referenceNode = array( 'customQuery' => '//w:drawing//wp:docPr', );
$contents = $docx->getDocxPathQueryInfo($referenceNode);
foreach ($contents['elements'] as $content) {
        // get descr
        echo $content->getAttribute('descr') . PHP_EOL;;

        // get height and width
        $size = $content->parentNode->getElementsByTagNameNS('', 'extent');
        if ($size->length > 0) {
                echo $size->item(0)->getAttribute('cx') . PHP_EOL; // width
                echo $size->item(0)->getAttribute('cy') . PHP_EOL; // height

Images use EMUs as unit values, you can transform them to inches or cms using the following code:

'height_word_emus' => $heightImage,
'width_word_emus' => $widthImage,
'height_word_inches' => $heightImage/914400,
'width_word_inches' => $widthImage/914400,
'height_word_cms' => $heightImage/360000,
'width_word_cms' => $widthImage/360000,


Posted by bkidd  · 08-06-2020 - 16:25

This works great.  Thanks for the suggestion.