Forum


Replies: 7   Views: 2253
How to get an image's alt text
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  · 04-06-2020 - 14:59

Hello,

The title attribute is a caption value, not the alternative text:

http://officeopenxml.com/drwPic-nvPicPr.php

The id is an integer and specifies a unique identifier. The name is a string and typically stores the original file name. The title is a string that specifies the caption. The descr is alternative text used for assistive technologies which do not display the picture.

We think the user hasn't added the alt text to the correct place.

If you need to customize Word contents/attributes, you can use customizeWordContents (https://www.phpdocx.com/api-documentation/docxcustomizer/customize-docx-Word-documents-PHP). As descr isn't one of the supported attributes, you need to use the customAttributes option and the same XPath query than the one used to get the images.

Maybe the easiest solution for your case is allowing both attributes to be used with replacePlaceholderImage. If you edit CreateDocxFromTemplate.php, and go to the Image4Image method, you can find this line:

if ($domImages->item($i)->getAttribute('descr') == self::$_templateSymbol . $variable . self::$_templateSymbol && $imageCounter == 0) {

that can be replaced by:

if (($domImages->item($i)->getAttribute('descr') == self::$_templateSymbol . $variable . self::$_templateSymbol || $domImages->item($i)->getAttribute('title') == self::$_templateSymbol . $variable . self::$_templateSymbol) && $imageCounter == 0) {

We have forwarded the topic to the dev team to be checked and include the same change to the stable release.

Regards.