Skip to content

Add svg support for template processor - #2806

Open
geo-fret wants to merge 23 commits into
PHPOffice:masterfrom
geo-fret:feature/template-svg-image
Open

geo-fret wants to merge 23 commits into
PHPOffice:masterfrom
geo-fret:feature/template-svg-image

Conversation

@geo-fret

@geo-fret geo-fret commented Jul 29, 2025 •

Copy link
Copy Markdown

Description

Allow svg images to be set through template processor.

Fixes #2795 partly (only svg part)

Checklist:

  • My CI is 🟢
  • I have covered by unit tests my new code (check build/coverage for coverage report)
  • I have updated the documentation to describe the changes
  • I have updated the changelog

@coveralls

coveralls commented Jul 29, 2025 •

Copy link
Copy Markdown

Coverage Status

coverage: 96.647% (+0.07%) from 96.582% — geo-fret:feature/template-svg-image into PHPOffice:master

@geo-fret
geo-fret marked this pull request as ready for review July 30, 2025 06:59
oleibman added a commit to oleibman/PHPWord that referenced this pull request Dec 9, 2025
Based on PR PHPOffice#2806 by @geo-fret. Fix PHPOffice#2795 (which also requests ico, but geo-fret believes that isn't supported in docx).
Comment thread src/PhpWord/TemplateProcessor.php Outdated
Comment on lines +578 to +579
$actualWidth = $svgAttributes->width;
$actualHeight = $svgAttributes->height;

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is the behavior if the width & height are not defined ?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've changed the behavior regarding to svg standards

Comment thread src/PhpWord/TemplateProcessor.php Outdated
Comment on lines +765 to +784
if (preg_match('/^[+-]?([0-9]+\.?[0-9]*)?(em|ex|%)$/i', $preparedImageAttrs['width'], $matches)) {
$size = (float) ($matches[1]);
$unit = $matches[2];
switch ($unit) {
case 'ex':
$size = $size * 2;

// no break
case 'em':
$width = $size * 152400;

break;
case '%':
$width = Converter::cssToEmu($preparedImageAttrs['originalWidth']) * $size;

break;
}
} else {
$width = Converter::cssToEmu($preparedImageAttrs['originalWidth']);
}

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you put it in a method ?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I moved that part to the converter. Maybe it will need a change here to use the current paragraph font size instead of a fixed one.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you add assertions on presence of tags in XML ?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've added just a very small check, if a drawing and a svg tag is in the XML.

@Progi1984 Progi1984 left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Some feedbacks

@Progi1984

Copy link
Copy Markdown
Member

The CI is 🔴

Comment thread src/PhpWord/TemplateProcessor.php Resolved
@geo-fret
geo-fret requested a review from Progi1984 September 29, 2026 05:47
Comment thread src/PhpWord/Shared/Converter.php Resolved
Comment thread src/PhpWord/Shared/Converter.php Resolved

@Progi1984 Progi1984 left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Last feedbacks

@geo-fret
geo-fret requested a review from Progi1984 October 1, 2026 12:53
@geo-fret
geo-fret force-pushed the feature/template-svg-image branch from 33da539 to 09f9362 Compare December 2, 2026 07:43
@geo-fret
geo-fret requested a review from pwsdotru December 2, 2026 08:01

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Development

Successfully merging this pull request may close these issues.

SVG and ICO image not supported in setImageValue

4 participants