Forum


Replies: 6   Views: 2253
Get the image size of an image placeholder when using createdocxfromtemplate
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 admin  · 19-06-2019 - 14:16

Hello,

Indexer uses a DOCX as source, not a DOMDocument (https://www.phpdocx.com/api-documentation/docxutilities/indexer-parse-word-documents-with-PHP):

$indexer = new Indexer('document.docx');
$output = $indexer->getOutput();

This class returns a lot of information about images, including sizes.

Also please note that replacePlacehoderImage allows keeping the size of the previous image or use the sizes from the new image with the following options:

width     mixed   The value in cm (float) or 'auto' (use image size).
height  mixed   The value in cm (float) or 'auto' (use image size).
dpi     int     Dots per inch. This parameter is only taken into account if width or height are set to auto.

If you want to get all the information about a single image with a placeholder you can use DOCXPath:

$referenceNode = array(
    'customQuery' => '//w:drawing[descendant::wp:docPr[contains(@descr, "$VAR$")]]',
);
$queryInfo = $docx->getDocxPathQueryInfo($referenceNode);

This code returns the elements of an XPath query (the image that includes $VAR$ as placeholder). So you can read it to get the size; although you can get it from the Indexer class, this the code used to get image sizes:

foreach ($drawingChildNodes as $drawingChildNode) {
    if ($drawingChildNode->tagName == 'wp:extent') {
        if ($drawingChildNode->hasAttribute('cx')) {
            $widthImage = $drawingChildNode->getAttribute('cx');
        }
        if ($drawingChildNode->hasAttribute('cy')) {
            $heightImage = $drawingChildNode->getAttribute('cy');
        }
    }
}

$this->documentStructure[$target]['images'][] = array(
    'content' => $imageString,
    'path' => $imageEntry->getAttribute('Target'),
    '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,
);

Regards.