Skip to content

Commit 9c11067

Browse files
author
Bas-Jan 't Jong
committed
Added Image relative and absolute positioning
1 parent e613e13 commit 9c11067

File tree

2 files changed

+263
-8
lines changed

2 files changed

+263
-8
lines changed

src/PhpWord/Style/Image.php

Lines changed: 239 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,32 @@ class Image extends AbstractStyle
1919
const WRAPPING_STYLE_TIGHT = 'tight';
2020
const WRAPPING_STYLE_BEHIND = 'behind';
2121
const WRAPPING_STYLE_INFRONT = 'infront';
22-
22+
const POSITION_HORIZONTAL_LEFT = 'left';
23+
const POSITION_HORIZONTAL_CENTER = 'centered';
24+
const POSITION_HORIZONTAL_RIGHT = 'right';
25+
const POSITION_VERTICAL_TOP = 'top';
26+
const POSITION_VERTICAL_CENTER = 'center';
27+
const POSITION_VERTICAL_BOTTOM = 'bottom';
28+
const POSITION_VERTICAL_INSIDE = 'inside';
29+
const POSITION_VERTICAL_OUTSIDE = 'outside';
30+
const POSITION_HORIZONTAL_RELATIVE_MARGIN = 'margin';
31+
const POSITION_HORIZONTAL_RELATIVE_PAGE = 'page';
32+
const POSITION_HORIZONTAL_RELATIVE_COLUMN = 'column';
33+
const POSITION_HORIZONTAL_RELATIVE_CHAR = 'char';
34+
const POSITION_HORIZONTAL_RELATIVE_LMARGIN = 'left-margin-area';
35+
const POSITION_HORIZONTAL_RELATIVE_RMARGIN = 'right-margin-area';
36+
const POSITION_HORIZONTAL_RELATIVE_IMARGIN = 'inner-margin-area';
37+
const POSITION_HORIZONTAL_RELATIVE_OMARGIN = 'outer-margin-area';
38+
const POSITION_VERTICAL_RELATIVE_MARGIN = 'margin';
39+
const POSITION_VERTICAL_RELATIVE_PAGE = 'page';
40+
const POSITION_VERTICAL_RELATIVE_LINE = 'line';
41+
const POSITION_VERTICAL_RELATIVE_TMARGIN = 'top-margin-area';
42+
const POSITION_VERTICAL_RELATIVE_BMARGIN = 'bottom-margin-area';
43+
const POSITION_VERTICAL_RELATIVE_IMARGIN = 'inner-margin-area';
44+
const POSITION_VERTICAL_RELATIVE_OMARGIN = 'outer-margin-area';
45+
const POSITION_RELATIVE = 'relative';
46+
const POSITION_ABSOLUTE = 'absolute';
47+
2348
/**
2449
* Image width
2550
*
@@ -62,6 +87,42 @@ class Image extends AbstractStyle
6287
*/
6388
private $wrappingStyle;
6489

90+
/**
91+
* Horizontal alignment
92+
*
93+
* @var string
94+
*/
95+
private $posHorizontal;
96+
97+
/**
98+
* Horizontal Relation
99+
*
100+
* @var string
101+
*/
102+
private $posHorizontalRel;
103+
104+
/**
105+
* Vertical alignment
106+
*
107+
* @var string
108+
*/
109+
private $posVertical;
110+
111+
/**
112+
* Vertical Relation
113+
*
114+
* @var string
115+
*/
116+
private $posVerticalRel;
117+
118+
/**
119+
* Positioning type (Relative or Absolute)
120+
*
121+
* @var string
122+
*/
123+
private $positioning;
124+
125+
65126
/**
66127
* Create new image style
67128
*/
@@ -73,6 +134,11 @@ public function __construct()
73134
$this->marginTop = null;
74135
$this->marginLeft = null;
75136
$this->setWrappingStyle(self::WRAPPING_STYLE_INLINE);
137+
$this->setPositioning(self::POSITION_RELATIVE);
138+
$this->setPosHorizontal(self::POSITION_HORIZONTAL_LEFT);
139+
$this->setPosHorizontalRel(self::POSITION_HORIZONTAL_RELATIVE_CHAR);
140+
$this->setPosVertical(self::POSITION_VERTICAL_TOP);
141+
$this->setPosVerticalRel(self::POSITION_VERTICAL_RELATIVE_LINE);
76142
}
77143

78144
/**
@@ -205,4 +271,176 @@ public function getWrappingStyle()
205271
{
206272
return $this->wrappingStyle;
207273
}
274+
275+
/**
276+
* Set positioning type
277+
*
278+
* @param string $positioning
279+
* @throws \InvalidArgumentException
280+
* @return $this
281+
*/
282+
283+
public function setPositioning($positioning)
284+
{
285+
switch ($positioning) {
286+
case self::POSITION_RELATIVE:
287+
case self::POSITION_ABSOLUTE:
288+
$this->positioning = $positioning;
289+
break;
290+
default:
291+
throw new InvalidArgumentException('Positioning does not exists');
292+
break;
293+
}
294+
return $this;
295+
}
296+
297+
/**
298+
* Get positioning type
299+
*
300+
* @return string
301+
*/
302+
public function getPositioning()
303+
{
304+
return $this->positioning;
305+
}
306+
307+
/**
308+
* Set horizontal alignment
309+
*
310+
* @param string $alignment
311+
* @throws \InvalidArgumentException
312+
* @return $this
313+
*/
314+
public function setPosHorizontal($alignment)
315+
{
316+
switch ($alignment) {
317+
case self::POSITION_HORIZONTAL_LEFT:
318+
case self::POSITION_HORIZONTAL_CENTER:
319+
case self::POSITION_HORIZONTAL_RIGHT:
320+
$this->posHorizontal = $alignment;
321+
break;
322+
default:
323+
throw new InvalidArgumentException('Horizontal alignment does not exists');
324+
break;
325+
}
326+
return $this;
327+
}
328+
329+
/**
330+
* Get horizontal alignment
331+
*
332+
* @return string
333+
*/
334+
public function getPosHorizontal()
335+
{
336+
return $this->posHorizontal;
337+
}
338+
339+
/**
340+
* Set vertical alignment
341+
*
342+
* @param string $alignment
343+
* @throws \InvalidArgumentException
344+
* @return $this
345+
*/
346+
347+
public function setPosVertical($alignment)
348+
{
349+
switch ($alignment) {
350+
case self::POSITION_VERTICAL_TOP:
351+
case self::POSITION_VERTICAL_CENTER:
352+
case self::POSITION_VERTICAL_BOTTOM:
353+
case self::POSITION_VERTICAL_INSIDE:
354+
case self::POSITION_VERTICAL_OUTSIDE:
355+
$this->posVertical = $alignment;
356+
break;
357+
default:
358+
throw new InvalidArgumentException('Vertical alignment does not exists');
359+
break;
360+
}
361+
return $this;
362+
}
363+
364+
/**
365+
* Get vertical alignment
366+
*
367+
* @return string
368+
*/
369+
public function getPosVertical()
370+
{
371+
return $this->posVertical;
372+
}
373+
374+
/**
375+
* Set horizontal relation
376+
*
377+
* @param string $relto
378+
* @throws \InvalidArgumentException
379+
* @return $this
380+
*/
381+
public function setPosHorizontalRel($relto)
382+
{
383+
switch ($relto) {
384+
case self::POSITION_HORIZONTAL_RELATIVE_MARGIN:
385+
case self::POSITION_HORIZONTAL_RELATIVE_PAGE:
386+
case self::POSITION_HORIZONTAL_RELATIVE_COLUMN:
387+
case self::POSITION_HORIZONTAL_RELATIVE_CHAR:
388+
case self::POSITION_HORIZONTAL_RELATIVE_LMARGIN:
389+
case self::POSITION_HORIZONTAL_RELATIVE_RMARGIN:
390+
case self::POSITION_HORIZONTAL_RELATIVE_IMARGIN:
391+
case self::POSITION_HORIZONTAL_RELATIVE_OMARGIN:
392+
$this->posHorizontalRel = $relto;
393+
break;
394+
default:
395+
throw new InvalidArgumentException('Horizontal relation does not exists');
396+
break;
397+
}
398+
return $this;
399+
}
400+
401+
/**
402+
* Get horizontal relation
403+
*
404+
* @return string
405+
*/
406+
public function getPosHorizontalRel()
407+
{
408+
return $this->posHorizontalRel;
409+
}
410+
411+
/**
412+
* Set vertical relation
413+
*
414+
* @param string $relto
415+
* @throws \InvalidArgumentException
416+
* @return $this
417+
*/
418+
public function setPosVerticalRel($relto)
419+
{
420+
switch ($relto) {
421+
case self::POSITION_VERTICAL_RELATIVE_MARGIN:
422+
case self::POSITION_VERTICAL_RELATIVE_PAGE:
423+
case self::POSITION_VERTICAL_RELATIVE_LINE:
424+
case self::POSITION_VERTICAL_RELATIVE_TMARGIN:
425+
case self::POSITION_VERTICAL_RELATIVE_BMARGIN:
426+
case self::POSITION_VERTICAL_RELATIVE_IMARGIN:
427+
case self::POSITION_VERTICAL_RELATIVE_OMARGIN:
428+
$this->posVerticalRel = $relto;
429+
break;
430+
default:
431+
throw new InvalidArgumentException('Vertical relation does not exists');
432+
break;
433+
}
434+
return $this;
435+
}
436+
437+
/**
438+
* Get vertical relation
439+
*
440+
* @return string
441+
*/
442+
public function getPosVerticalRel()
443+
{
444+
return $this->posVerticalRel;
445+
}
208446
}

src/PhpWord/Writer/Word2007/Element/Image.php

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ private function writeImage()
4444
$marginTop = $style->getMarginTop();
4545
$marginLeft = $style->getMarginLeft();
4646
$wrappingStyle = $style->getWrappingStyle();
47+
$positioning = $style->getPositioning();
4748
$w10wrapType = null;
4849
$imgStyle = '';
4950
if (null !== $width) {
@@ -53,28 +54,44 @@ private function writeImage()
5354
$imgStyle .= 'height:' . $height . 'px;';
5455
}
5556
if (null !== $marginTop) {
56-
$imgStyle .= 'margin-top:' . $marginTop . 'in;';
57+
$imgStyle .= 'margin-top:' . $marginTop . 'px;';
5758
}
5859
if (null !== $marginLeft) {
59-
$imgStyle .= 'margin-left:' . $marginLeft . 'in;';
60+
$imgStyle .= 'margin-left:' . $marginLeft . 'px;';
6061
}
62+
$imgStyle.='position:absolute;mso-width-percent:0;mso-height-percent:0;mso-width-relative:margin;mso-height-relative:margin;';
63+
switch ($positioning) {
64+
case ImageStyle::POSITION_RELATIVE:
65+
$imgStyle.='mso-position-horizontal:'.$style->getPosHorizontal().';';
66+
$imgStyle.='mso-position-horizontal-relative:'.$style->getPosHorizontalRel().';';
67+
$imgStyle.='mso-position-vertical:'.$style->getPosVertical().';';
68+
$imgStyle.='mso-position-vertical-relative:'.$style->getPosVerticalRel().';';
69+
$imgStyle.='margin-left:0;margin-top:0;';
70+
break;
71+
72+
case ImageStyle::POSITION_ABSOLUTE:
73+
$imgStyle.='mso-position-horizontal-relative:page;';
74+
$imgStyle.='mso-position-vertical-relative:page;';
75+
break;
76+
}
77+
6178
switch ($wrappingStyle) {
6279
case ImageStyle::WRAPPING_STYLE_BEHIND:
63-
$imgStyle .= 'position:absolute;z-index:-251658752;';
80+
$imgStyle .= 'z-index:-251658752;';
6481
break;
6582
case ImageStyle::WRAPPING_STYLE_INFRONT:
66-
$imgStyle .= 'position:absolute;z-index:251659264;mso-position-horizontal:absolute;mso-position-vertical:absolute;';
83+
$imgStyle .= 'z-index:251659264;mso-position-horizontal:absolute;mso-position-vertical:absolute;';
6784
break;
6885
case ImageStyle::WRAPPING_STYLE_SQUARE:
69-
$imgStyle .= 'position:absolute;z-index:251659264;mso-position-horizontal:absolute;mso-position-vertical:absolute;';
86+
$imgStyle .= 'z-index:251659264;mso-position-horizontal:absolute;mso-position-vertical:absolute;';
7087
$w10wrapType = 'square';
7188
break;
7289
case ImageStyle::WRAPPING_STYLE_TIGHT:
73-
$imgStyle .= 'position:absolute;z-index:251659264;mso-position-horizontal:absolute;mso-position-vertical:absolute;';
90+
$imgStyle .= 'z-index:251659264;mso-position-horizontal:absolute;mso-position-vertical:absolute;';
7491
$w10wrapType = 'tight';
7592
break;
7693
}
77-
94+
7895
if (!$this->withoutP) {
7996
$this->xmlWriter->startElement('w:p');
8097
if (!is_null($align)) {

0 commit comments

Comments
 (0)