Skip to content

Commit d5da80b

Browse files
SailorMaxtroosan
authored andcommitted
Support adding images in Templates (#1170)
* setImageValue() + fix adding files via ZipArchive * fix phpdoc variable name * Changed logic that determines extension image file extension for document to depend on MIME type. This same logic is used in Element/Image.php * support <w:t> tags with arguments * allow setup size of image into template variable * support of 'ratio' replace attribute + documentation
1 parent e6496bf commit d5da80b

File tree

7 files changed

+462
-15
lines changed

7 files changed

+462
-15
lines changed

.travis.yml

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,6 @@ matrix:
2424
- php: 5.3
2525
- php: 7.0
2626
- php: 7.3
27-
allow_failures:
28-
- php: 7.3
2927

3028
cache:
3129
directories:

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ v0.16.0 (xx dec 2018)
1010
- Add setting Chart Title and Legend visibility @Tom-Magill #1433
1111
- Add ability to pass a Style object in Section constructor @ndench #1416
1212
- Add support for hidden text @Alexmg86 #1527
13+
- Add support for setting images in TemplateProcessor @SailorMax #1170
1314

1415
### Fixed
1516
- Fix regex in `cloneBlock` function @nicoder #1269

docs/templates-processing.rst

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,32 @@ You can create an OOXML document template with included search-patterns (macros)
77

88
To deal with a template file, use ``new TemplateProcessor`` statement. After TemplateProcessor instance creation the document template is copied into the temporary directory. Then you can use ``TemplateProcessor::setValue`` method to change the value of a search pattern. The search-pattern model is: ``${search-pattern}``.
99

10+
The search-pattern model for images can be like:
11+
- ``${search-image-pattern}``
12+
- ``${search-image-pattern:[width]:[height]:[ratio]}``
13+
- ``${search-image-pattern:[width]x[height]}``
14+
- ``${search-image-pattern:size=[width]x[height]}``
15+
- ``${search-image-pattern:width=[width]:height=[height]:ratio=false}``
16+
Where:
17+
- [width] and [height] can be just numbers or numbers with measure, which supported by Word (cm|mm|in|pt|pc|px|%|em|ex)
18+
- [ratio] uses only for ``false``, ``-`` or ``f`` to turn off respect aspect ration of image. By default template image size uses as 'container' size.
19+
1020
Example:
1121

22+
.. code-block:: doc
23+
24+
${CompanyLogo}
25+
${UserLogo:50:50} ${Name} - ${City} - ${Street}
26+
1227
.. code-block:: php
1328
1429
$templateProcessor = new TemplateProcessor('Template.docx');
1530
$templateProcessor->setValue('Name', 'John Doe');
1631
$templateProcessor->setValue(array('City', 'Street'), array('Detroit', '12th Street'));
1732
33+
$templateProcessor->setImageValue('CompanyLogo', 'path/to/company/logo.png');
34+
$templateProcessor->setImageValue('UserLogo', array('path' => 'path/to/logo.png', 'width' => 100, 'height' => 100, 'ratio' => false));
35+
1836
It is not possible to directly add new OOXML elements to the template file being processed, but it is possible to transform headers, main document part, and footers of the template using XSLT (see ``TemplateProcessor::applyXslStyleSheet``).
1937

2038
See ``Sample_07_TemplateCloneRow.php`` for example on how to create

src/PhpWord/Shared/ZipArchive.php

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,7 @@ public function open($filename, $flags = null)
129129
{
130130
$result = true;
131131
$this->filename = $filename;
132+
$this->tempDir = Settings::getTempDir();
132133

133134
if (!$this->usePclzip) {
134135
$zip = new \ZipArchive();
@@ -139,7 +140,6 @@ public function open($filename, $flags = null)
139140
$this->numFiles = $zip->numFiles;
140141
} else {
141142
$zip = new \PclZip($this->filename);
142-
$this->tempDir = Settings::getTempDir();
143143
$zipContent = $zip->listContent();
144144
$this->numFiles = is_array($zipContent) ? count($zipContent) : 0;
145145
}
@@ -245,7 +245,13 @@ public function pclzipAddFile($filename, $localname = null)
245245
$pathRemoved = $filenameParts['dirname'];
246246
$pathAdded = $localnameParts['dirname'];
247247

248-
$res = $zip->add($filename, PCLZIP_OPT_REMOVE_PATH, $pathRemoved, PCLZIP_OPT_ADD_PATH, $pathAdded);
248+
if (!$this->usePclzip) {
249+
$pathAdded = $pathAdded . '/' . ltrim(str_replace('\\', '/', substr($filename, strlen($pathRemoved))), '/');
250+
//$res = $zip->addFile($filename, $pathAdded);
251+
$res = $zip->addFromString($pathAdded, file_get_contents($filename)); // addFile can't use subfolders in some cases
252+
} else {
253+
$res = $zip->add($filename, PCLZIP_OPT_REMOVE_PATH, $pathRemoved, PCLZIP_OPT_ADD_PATH, $pathAdded);
254+
}
249255

250256
if ($tempFile) {
251257
// Remove temp file, if created

0 commit comments

Comments
 (0)