PHPWord icon indicating copy to clipboard operation
PHPWord copied to clipboard

Add two image in a same line left and right alignment

Open ghost opened this issue 6 years ago • 1 comments

I'm trying to add two images which is left and right align in a same line(extreme left and extreme right) i tried everyhing but nothing works , even margin is not working also.

` $table = $section->addTable(); $table->addRow();

$table->addCell(2000, $cellRowSpan)->addImage('pearson1.png',array('width' => '70','height' => '70', 'valign' => 'center', 'marginLeft' => '4')); $table->addCell(2000, $cellRowSpan)->addImage('genesis2.png',array('width' => '130','height' => '40', 'valign' => 'center', 'marginLeft' => '99'));`

ghost avatar Jul 16 '19 08:07 ghost

Now it's 2024 and I also had to find a way to place several pictures next to each other. Using a table was too complicated for me. I was able to solve it with TextRun. Simple - once you get the idea, so I would like to share it here:

Before starting the image output, we create a TextRun: $ImageTextRun = $section->addTextRun(['align' => 'left']);

Then we add each image individually to the TextRun. I specify the width of the image and the height:

$ImageTextRun ->addImage(
				$NamePNGFile,
				array(
						'width' => round(\PhpOffice\PhpWord\Shared\Converter::cmToPoint($Figurwidth)),
						'height' => round(\PhpOffice\PhpWord\Shared\Converter::cmToPoint($Figurheight * ($height/$width))),
						'marginTop'     => -1,
						'marginLeft'    => -1,
						'wrappingStyle' => 'behind'
					)
			);

To leave the free space in the centre, I would now use a white PNG and calculate its width exactly beforehand - works perfectly for me.

SarahTrees avatar May 15 '24 10:05 SarahTrees

I was able to solve this by floating one image to the left and floating the other image to the right with the code below, it was a simple solution:

`$imageStyleLeftLogo = array( 'width' => 'auto', 'height' => 30, 'wrappingStyle' => 'inline', 'positioning' => 'absolute', 'posHorizontalRel' => 'margin', 'posVerticalRel' => 'line', );

$section->addImage('../logo1.png', $imageStyleLeftLogo);

$imageStyleRightLogo = array( 'width' => 'auto', 'height' => 30, 'wrappingStyle' => 'inline', 'positioning' => 'absolute', 'posHorizontal' => \PhpOffice\PhpWord\Style\Image::POSITION_HORIZONTAL_RIGHT, 'posHorizontalRel' => 'margin', 'posVerticalRel' => 'margin', );

$section->addImage('../logo2.png', $imageStyleRightLogo);`

NyanchulaSauti avatar Nov 27 '24 10:11 NyanchulaSauti