PHPOffice / PHPWord

A pure PHP library for reading and writing word processing documents

Home Page:https://phpoffice.github.io/PHPWord/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Replace all image by their alttext attribute

l4nos opened this issue · comments

Essentially the idea is to loop over every image in every document part, if it has an "alt text" attribute set, look in the merge data for either image data or an image url, store it in a temp file, then load that into the relations using the existing functions and update the rid.

I've actually written the code in my own fork of this and tested it and it works brilliantly.

A feature like this i've seen a lot of people on stackoverflow asking for, and I couldn't find a decent solution, one that honoured and reused the original functions of TemplateProcessor as much as possible, therefore I took a stab at it myself.

https://github.com/Lane-Ventures/PHPWord

The developer consuming this package would simply feed in their flattened data array in the same way they do for setValues, and it would work just for images.

        $templateProcessor->setImagesByAltText($flattenedData);

If you aren't willing to add this into the main package, im happy to just keep using this in my own package, I just posted the solution here as I've seen a lot of people hutning for this.

I'm also working on a function to handle images inside repetable table rows which should sit nicely with cloneRowAndSetValues. I'm also looking to incorporate in how to handle preserving the aspect ratio of the source image or the placeholder.

Any questions, queries or crticisms are welcomed.