I am trying to change the alignment of the image added into the docx file, but the alignment is not working. I have tried as below:
$templateProcessor->setImageValue($tag->template_tag,
array(
'path' => $filePath,
'positioning' => \PhpOffice\PhpWord\Style\Image::POSITION_ABSOLUTE,
'alignment' => \PhpOffice\PhpWord\SimpleType\Jc::END,
)
);
Though the image is rendered in exported docx file, however the image alignment is always left. How can I change the alignment of image to either center or end?
You are using positionning settings reserved when you create a Word document from scratch using the library.
However, you are using PHPWord templating processing capability : that means the layout is driven by your Word document itself.
So, the placeholder itself must be centered in the template to be replaced by a centered image.
To illustrate, consider the code below (placed in any PHP code where TemplateProcessor
class is available) :
$templateProcessor = new TemplateProcessor('Template.docx');
$templateProcessor->setValue('mc', 'Word MC');
$templateProcessor->setImageValue(
'advisory',
[
'path' => 'https://upload.wikimedia.org/wikipedia/commons/thumb/3/33/Parental_Advisory_label.svg/300px-Parental_Advisory_label.svg.png'
]
);
$templateProcessor->saveAs('Replaced.docx');
With the following Word template (Template.docx
in the code)...
...you got the following replacement (
Replaced.docx
in the code)
If you center the image placeholder...
...the replace image is centered !
The second argument, named $replace
, of the method setImageValue
of the TemplateProcessor
class (added in the 0.16 version) can be an associative array but it only supports 3 values (quoted from the docBlock of the method) :
@param mixed $replace Path to image, or array("path" => xx, "width" => yy, "height" => zz)