Skip to content

Commit 7959651

Browse files
committed
support of 'ratio' replace attribute + documentation
1 parent 5038b3b commit 7959651

File tree

2 files changed

+26
-1
lines changed

2 files changed

+26
-1
lines changed

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/TemplateProcessor.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -367,6 +367,7 @@ private function prepareImageAttrs($replaceImage, $varInlineArgs)
367367
// get image path and size
368368
$width = null;
369369
$height = null;
370+
$ratio = null;
370371
if (is_array($replaceImage) && isset($replaceImage['path'])) {
371372
$imgPath = $replaceImage['path'];
372373
if (isset($replaceImage['width'])) {
@@ -375,6 +376,9 @@ private function prepareImageAttrs($replaceImage, $varInlineArgs)
375376
if (isset($replaceImage['height'])) {
376377
$height = $replaceImage['height'];
377378
}
379+
if (isset($replaceImage['ratio'])) {
380+
$ratio = $replaceImage['ratio'];
381+
}
378382
} else {
379383
$imgPath = $replaceImage;
380384
}
@@ -389,7 +393,10 @@ private function prepareImageAttrs($replaceImage, $varInlineArgs)
389393
list($actualWidth, $actualHeight, $imageType) = $imageData;
390394

391395
// fix aspect ratio (by default)
392-
if (empty($varInlineArgs['ratio']) || $varInlineArgs['ratio'] !== 'f') {
396+
if (is_null($ratio) && isset($varInlineArgs['ratio'])) {
397+
$ratio = $varInlineArgs['ratio'];
398+
}
399+
if (is_null($ratio) || !in_array(strtolower($ratio), array('', '-', 'f', 'false'))) {
393400
$this->fixImageWidthHeightRatio($width, $height, $actualWidth, $actualHeight);
394401
}
395402

0 commit comments

Comments
 (0)