Forum


Replies: 2   Views: 1615
How to display 2 images side by side in a same table cell?
Topic closed:
Please note this is an old forum thread. Information in this post may be out-to-date and/or erroneous.
Every phpdocx version includes new features and improvements. Previously unsupported features may have been added to newer releases, or past issues may have been corrected.
We encourage you to download the current phpdocx version and check the Documentation available.

Posted by Bouillou  · 15-03-2019 - 10:34

Hello,

How to display images side by side in a same table cell?

Here is my test which display the 2 images in the same table cell, but one above the other:

<?php

//path to  the CreateDocx class within your PHPDocX installation
require_once '../../../classes/CreateDocx.inc';

$docx = new CreateDocx();

//we create a few Word fragments to insert rich content in a table

$link = new WordFragment($docx);
$options = array(
    'url' => 'http://www.google.es'
);

$link->addLink('Link to Google', $options);

$image = new WordFragment($docx);
$options = array(
    'src' => '../../img/image.png',
    'imageAlign' => 'left',
    'scaling' => 30,
    'textWrap' => 0
);

// Add 2 images in the WordFragemnt
$image->addImage($options);
$image->addImage($options);

$valuesTable = array(
    array(
        'Title A',
        'Title B',
        'Title C'
    ),
    array(
        'Line A',
        $link,
        $image
    )
);


$paramsTable = array(
    'tableStyle' => 'LightListAccent1PHPDOCX',
    'tableAlign' => 'center',
    'columnWidths' => array(1000, 2500, 3000),
    );

$docx->addTable($valuesTable, $paramsTable);


$docx->createDocx('example_addTable_2');

Best regards,

S├ębastien

Posted by admin  · 15-03-2019 - 15:57

Hello,

You need to generate a text WordFragment and add the images:

$link = new WordFragment($docx);
$options = array(
    'url' => 'http://www.google.es'
);

$link->addLink('Link to Google', $options);

$image = new WordFragment($docx);
$options = array(
    'src' => 'image.png',
    'width' => 50,
    'height' => 50,
);

$image->addImage($options);

$text = array();
$text[] = $image;
$text[] = $image;

$textFragment = new WordFragment($docx);
$textFragment->addText($text);

$valuesTable = array(
    array(
        'Title A',
        'Title B',
        'Title C',
    ),
    array(
        'Line A',
        $link,
        $textFragment,
    )
);


$paramsTable = array(
    'tableStyle' => 'LightListAccent1PHPDOCX',
    'tableAlign' => 'center',
    'columnWidths' => array(1000, 2500, 3000),
    );

$docx->addTable($valuesTable, $paramsTable);

to avoid adding a paragraph for each image. In the file examples/Core/addText/sample_3.php you can see a sample using run of text contents, out of a table but it works in the same way.

Regards.

Posted by Bouillou  · 15-03-2019 - 16:31

Thank you very much for your answer. It is working well.