Forum


Replies: 5   Views: 4662
Replacevariablebyhtml image width percent not applying
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 admin  · 10-06-2016 - 14:22

Hello,

Due to how Word works is not advisable setting image sizes as percent. We recommend you to use px for all sizes, because if you use % you don't know the final sizing until the document is opened.

Anyway, if you want to test a beta patch, please edit the HTML2WordML.inc file and in line 1022 add these lines:

if (isset($nodo['attributes']['width'])) {
  $widthUnit = preg_replace('/\d+/', '', $nodo['attributes']['width']);
    if ($widthUnit == '%') {
      $widthValue = preg_replace('/[^\d]+/', '', $nodo['attributes']['width']);
      $cx = $cx * $widthValue / 100;
    }
  }

  if (isset($nodo['attributes']['height'])) {
    $heightUnit = preg_replace('/\d+/', '', $nodo['attributes']['height']);
    if ($widthUnit == '%') {
      $heightUnit = preg_replace('/[^\d]+/', '', $nodo['attributes']['height']);
      $cy = $cy * $widthValue / 100;
  }
}

Instead of:

$c = $this->getImageDimensions($size, $imageSize);
$cx = $c[0];
$cy = $c[1];

self::$WordML .= $this->generateImageRPr($properties, $cy);

self::$openTags[$depth] = $nodo['nodeName'];

the code must be:

$c = $this->getImageDimensions($size, $imageSize);
$cx = $c[0];
$cy = $c[1];

if (isset($nodo['attributes']['width'])) {
    $widthUnit = preg_replace('/\d+/', '', $nodo['attributes']['width']);
    if ($widthUnit == '%') {
        $widthValue = preg_replace('/[^\d]+/', '', $nodo['attributes']['width']);
        $cx = $cx * $widthValue / 100;
    }
}

if (isset($nodo['attributes']['height'])) {
    $heightUnit = preg_replace('/\d+/', '', $nodo['attributes']['height']);
    if ($widthUnit == '%') {
        $heightUnit = preg_replace('/[^\d]+/', '', $nodo['attributes']['height']);
        $cy = $cy * $widthValue / 100;
    }
}

self::$WordML .= $this->generateImageRPr($properties, $cy);

self::$openTags[$depth] = $nodo['nodeName'];

It's a beta patch but works for HTML such as:

<table width="100%">
    <tr>
        <td width="33%"><img width="33%" src="image1.png"></td>
        <td width="33%"><img width="33%" src="image2.png"></td>
        <td width="33%"><img width="33%" src="image3.png"></td>
    </tr>
</table>

But we highly recommend using fixed sizes or use the addTable method instead of HTML for this kind of tasks.

Regards.