From 9c11067720f905486550c7aed4cc6530cb1d4980 Mon Sep 17 00:00:00 2001 From: Bas-Jan 't Jong Date: Sat, 3 May 2014 10:08:39 +0200 Subject: [PATCH 1/5] Added Image relative and absolute positioning --- src/PhpWord/Style/Image.php | 240 +++++++++++++++++- src/PhpWord/Writer/Word2007/Element/Image.php | 31 ++- 2 files changed, 263 insertions(+), 8 deletions(-) diff --git a/src/PhpWord/Style/Image.php b/src/PhpWord/Style/Image.php index f21e6674..7aabdeb2 100644 --- a/src/PhpWord/Style/Image.php +++ b/src/PhpWord/Style/Image.php @@ -19,7 +19,32 @@ class Image extends AbstractStyle const WRAPPING_STYLE_TIGHT = 'tight'; const WRAPPING_STYLE_BEHIND = 'behind'; const WRAPPING_STYLE_INFRONT = 'infront'; - + const POSITION_HORIZONTAL_LEFT = 'left'; + const POSITION_HORIZONTAL_CENTER = 'centered'; + const POSITION_HORIZONTAL_RIGHT = 'right'; + const POSITION_VERTICAL_TOP = 'top'; + const POSITION_VERTICAL_CENTER = 'center'; + const POSITION_VERTICAL_BOTTOM = 'bottom'; + const POSITION_VERTICAL_INSIDE = 'inside'; + const POSITION_VERTICAL_OUTSIDE = 'outside'; + const POSITION_HORIZONTAL_RELATIVE_MARGIN = 'margin'; + const POSITION_HORIZONTAL_RELATIVE_PAGE = 'page'; + const POSITION_HORIZONTAL_RELATIVE_COLUMN = 'column'; + const POSITION_HORIZONTAL_RELATIVE_CHAR = 'char'; + const POSITION_HORIZONTAL_RELATIVE_LMARGIN = 'left-margin-area'; + const POSITION_HORIZONTAL_RELATIVE_RMARGIN = 'right-margin-area'; + const POSITION_HORIZONTAL_RELATIVE_IMARGIN = 'inner-margin-area'; + const POSITION_HORIZONTAL_RELATIVE_OMARGIN = 'outer-margin-area'; + const POSITION_VERTICAL_RELATIVE_MARGIN = 'margin'; + const POSITION_VERTICAL_RELATIVE_PAGE = 'page'; + const POSITION_VERTICAL_RELATIVE_LINE = 'line'; + const POSITION_VERTICAL_RELATIVE_TMARGIN = 'top-margin-area'; + const POSITION_VERTICAL_RELATIVE_BMARGIN = 'bottom-margin-area'; + const POSITION_VERTICAL_RELATIVE_IMARGIN = 'inner-margin-area'; + const POSITION_VERTICAL_RELATIVE_OMARGIN = 'outer-margin-area'; + const POSITION_RELATIVE = 'relative'; + const POSITION_ABSOLUTE = 'absolute'; + /** * Image width * @@ -62,6 +87,42 @@ class Image extends AbstractStyle */ private $wrappingStyle; + /** + * Horizontal alignment + * + * @var string + */ + private $posHorizontal; + + /** + * Horizontal Relation + * + * @var string + */ + private $posHorizontalRel; + + /** + * Vertical alignment + * + * @var string + */ + private $posVertical; + + /** + * Vertical Relation + * + * @var string + */ + private $posVerticalRel; + + /** + * Positioning type (Relative or Absolute) + * + * @var string + */ + private $positioning; + + /** * Create new image style */ @@ -73,6 +134,11 @@ class Image extends AbstractStyle $this->marginTop = null; $this->marginLeft = null; $this->setWrappingStyle(self::WRAPPING_STYLE_INLINE); + $this->setPositioning(self::POSITION_RELATIVE); + $this->setPosHorizontal(self::POSITION_HORIZONTAL_LEFT); + $this->setPosHorizontalRel(self::POSITION_HORIZONTAL_RELATIVE_CHAR); + $this->setPosVertical(self::POSITION_VERTICAL_TOP); + $this->setPosVerticalRel(self::POSITION_VERTICAL_RELATIVE_LINE); } /** @@ -205,4 +271,176 @@ class Image extends AbstractStyle { return $this->wrappingStyle; } + + /** + * Set positioning type + * + * @param string $positioning + * @throws \InvalidArgumentException + * @return $this + */ + + public function setPositioning($positioning) + { + switch ($positioning) { + case self::POSITION_RELATIVE: + case self::POSITION_ABSOLUTE: + $this->positioning = $positioning; + break; + default: + throw new InvalidArgumentException('Positioning does not exists'); + break; + } + return $this; + } + + /** + * Get positioning type + * + * @return string + */ + public function getPositioning() + { + return $this->positioning; + } + + /** + * Set horizontal alignment + * + * @param string $alignment + * @throws \InvalidArgumentException + * @return $this + */ + public function setPosHorizontal($alignment) + { + switch ($alignment) { + case self::POSITION_HORIZONTAL_LEFT: + case self::POSITION_HORIZONTAL_CENTER: + case self::POSITION_HORIZONTAL_RIGHT: + $this->posHorizontal = $alignment; + break; + default: + throw new InvalidArgumentException('Horizontal alignment does not exists'); + break; + } + return $this; + } + + /** + * Get horizontal alignment + * + * @return string + */ + public function getPosHorizontal() + { + return $this->posHorizontal; + } + + /** + * Set vertical alignment + * + * @param string $alignment + * @throws \InvalidArgumentException + * @return $this + */ + + public function setPosVertical($alignment) + { + switch ($alignment) { + case self::POSITION_VERTICAL_TOP: + case self::POSITION_VERTICAL_CENTER: + case self::POSITION_VERTICAL_BOTTOM: + case self::POSITION_VERTICAL_INSIDE: + case self::POSITION_VERTICAL_OUTSIDE: + $this->posVertical = $alignment; + break; + default: + throw new InvalidArgumentException('Vertical alignment does not exists'); + break; + } + return $this; + } + + /** + * Get vertical alignment + * + * @return string + */ + public function getPosVertical() + { + return $this->posVertical; + } + + /** + * Set horizontal relation + * + * @param string $relto + * @throws \InvalidArgumentException + * @return $this + */ + public function setPosHorizontalRel($relto) + { + switch ($relto) { + case self::POSITION_HORIZONTAL_RELATIVE_MARGIN: + case self::POSITION_HORIZONTAL_RELATIVE_PAGE: + case self::POSITION_HORIZONTAL_RELATIVE_COLUMN: + case self::POSITION_HORIZONTAL_RELATIVE_CHAR: + case self::POSITION_HORIZONTAL_RELATIVE_LMARGIN: + case self::POSITION_HORIZONTAL_RELATIVE_RMARGIN: + case self::POSITION_HORIZONTAL_RELATIVE_IMARGIN: + case self::POSITION_HORIZONTAL_RELATIVE_OMARGIN: + $this->posHorizontalRel = $relto; + break; + default: + throw new InvalidArgumentException('Horizontal relation does not exists'); + break; + } + return $this; + } + + /** + * Get horizontal relation + * + * @return string + */ + public function getPosHorizontalRel() + { + return $this->posHorizontalRel; + } + + /** + * Set vertical relation + * + * @param string $relto + * @throws \InvalidArgumentException + * @return $this + */ + public function setPosVerticalRel($relto) + { + switch ($relto) { + case self::POSITION_VERTICAL_RELATIVE_MARGIN: + case self::POSITION_VERTICAL_RELATIVE_PAGE: + case self::POSITION_VERTICAL_RELATIVE_LINE: + case self::POSITION_VERTICAL_RELATIVE_TMARGIN: + case self::POSITION_VERTICAL_RELATIVE_BMARGIN: + case self::POSITION_VERTICAL_RELATIVE_IMARGIN: + case self::POSITION_VERTICAL_RELATIVE_OMARGIN: + $this->posVerticalRel = $relto; + break; + default: + throw new InvalidArgumentException('Vertical relation does not exists'); + break; + } + return $this; + } + + /** + * Get vertical relation + * + * @return string + */ + public function getPosVerticalRel() + { + return $this->posVerticalRel; + } } diff --git a/src/PhpWord/Writer/Word2007/Element/Image.php b/src/PhpWord/Writer/Word2007/Element/Image.php index e944b7af..80b71121 100644 --- a/src/PhpWord/Writer/Word2007/Element/Image.php +++ b/src/PhpWord/Writer/Word2007/Element/Image.php @@ -44,6 +44,7 @@ class Image extends Element $marginTop = $style->getMarginTop(); $marginLeft = $style->getMarginLeft(); $wrappingStyle = $style->getWrappingStyle(); + $positioning = $style->getPositioning(); $w10wrapType = null; $imgStyle = ''; if (null !== $width) { @@ -53,28 +54,44 @@ class Image extends Element $imgStyle .= 'height:' . $height . 'px;'; } if (null !== $marginTop) { - $imgStyle .= 'margin-top:' . $marginTop . 'in;'; + $imgStyle .= 'margin-top:' . $marginTop . 'px;'; } if (null !== $marginLeft) { - $imgStyle .= 'margin-left:' . $marginLeft . 'in;'; + $imgStyle .= 'margin-left:' . $marginLeft . 'px;'; } + $imgStyle.='position:absolute;mso-width-percent:0;mso-height-percent:0;mso-width-relative:margin;mso-height-relative:margin;'; + switch ($positioning) { + case ImageStyle::POSITION_RELATIVE: + $imgStyle.='mso-position-horizontal:'.$style->getPosHorizontal().';'; + $imgStyle.='mso-position-horizontal-relative:'.$style->getPosHorizontalRel().';'; + $imgStyle.='mso-position-vertical:'.$style->getPosVertical().';'; + $imgStyle.='mso-position-vertical-relative:'.$style->getPosVerticalRel().';'; + $imgStyle.='margin-left:0;margin-top:0;'; + break; + + case ImageStyle::POSITION_ABSOLUTE: + $imgStyle.='mso-position-horizontal-relative:page;'; + $imgStyle.='mso-position-vertical-relative:page;'; + break; + } + switch ($wrappingStyle) { case ImageStyle::WRAPPING_STYLE_BEHIND: - $imgStyle .= 'position:absolute;z-index:-251658752;'; + $imgStyle .= 'z-index:-251658752;'; break; case ImageStyle::WRAPPING_STYLE_INFRONT: - $imgStyle .= 'position:absolute;z-index:251659264;mso-position-horizontal:absolute;mso-position-vertical:absolute;'; + $imgStyle .= 'z-index:251659264;mso-position-horizontal:absolute;mso-position-vertical:absolute;'; break; case ImageStyle::WRAPPING_STYLE_SQUARE: - $imgStyle .= 'position:absolute;z-index:251659264;mso-position-horizontal:absolute;mso-position-vertical:absolute;'; + $imgStyle .= 'z-index:251659264;mso-position-horizontal:absolute;mso-position-vertical:absolute;'; $w10wrapType = 'square'; break; case ImageStyle::WRAPPING_STYLE_TIGHT: - $imgStyle .= 'position:absolute;z-index:251659264;mso-position-horizontal:absolute;mso-position-vertical:absolute;'; + $imgStyle .= 'z-index:251659264;mso-position-horizontal:absolute;mso-position-vertical:absolute;'; $w10wrapType = 'tight'; break; } - + if (!$this->withoutP) { $this->xmlWriter->startElement('w:p'); if (!is_null($align)) { From 2187954b565acf4ae661c0715892136897125618 Mon Sep 17 00:00:00 2001 From: Bas-Jan 't Jong Date: Sun, 4 May 2014 10:16:28 +0200 Subject: [PATCH 2/5] Fixed bug in header, wherein all images were assigned to the first header in a section. This resulted in a corrupt DOCX --- src/PhpWord/Element/AbstractContainer.php | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/PhpWord/Element/AbstractContainer.php b/src/PhpWord/Element/AbstractContainer.php index 8af70d42..b90f6d72 100644 --- a/src/PhpWord/Element/AbstractContainer.php +++ b/src/PhpWord/Element/AbstractContainer.php @@ -249,7 +249,6 @@ abstract class AbstractContainer extends AbstractElement $rId = Media::addElement($elementDocPart, 'image', $src, $image); $image->setRelationId($rId); $this->addElement($image); - return $image; } @@ -413,7 +412,7 @@ abstract class AbstractContainer extends AbstractElement $docPart = $isCellTextrun ? $this->getDocPart() : $this->container; $docPartId = $isCellTextrun ? $this->getDocPartId() : $this->sectionId; $inHeaderFooter = ($docPart == 'header' || $docPart == 'footer'); - + $docPartId = $inHeaderFooter ? $this->getDocPartId() : $docPartId; return $inHeaderFooter ? $docPart . $docPartId : $docPart; } From 15dcb384c64167fb33a6ac5f2989fc7bf513d134 Mon Sep 17 00:00:00 2001 From: Bas-Jan 't Jong Date: Sun, 4 May 2014 10:34:40 +0200 Subject: [PATCH 3/5] Messed up something. Resetting --- src/PhpWord/Element/AbstractContainer.php | 1 - src/PhpWord/Style/Image.php | 281 ++++++++++-------- src/PhpWord/Writer/Word2007/Element/Image.php | 30 +- 3 files changed, 169 insertions(+), 143 deletions(-) diff --git a/src/PhpWord/Element/AbstractContainer.php b/src/PhpWord/Element/AbstractContainer.php index b90f6d72..ea3450a5 100644 --- a/src/PhpWord/Element/AbstractContainer.php +++ b/src/PhpWord/Element/AbstractContainer.php @@ -412,7 +412,6 @@ abstract class AbstractContainer extends AbstractElement $docPart = $isCellTextrun ? $this->getDocPart() : $this->container; $docPartId = $isCellTextrun ? $this->getDocPartId() : $this->sectionId; $inHeaderFooter = ($docPart == 'header' || $docPart == 'footer'); - $docPartId = $inHeaderFooter ? $this->getDocPartId() : $docPartId; return $inHeaderFooter ? $docPart . $docPartId : $docPart; } diff --git a/src/PhpWord/Style/Image.php b/src/PhpWord/Style/Image.php index 7aabdeb2..a035be6e 100644 --- a/src/PhpWord/Style/Image.php +++ b/src/PhpWord/Style/Image.php @@ -1,4 +1,5 @@ width = null; - $this->height = null; - $this->align = null; - $this->marginTop = null; - $this->marginLeft = null; - $this->setWrappingStyle(self::WRAPPING_STYLE_INLINE); - $this->setPositioning(self::POSITION_RELATIVE); - $this->setPosHorizontal(self::POSITION_HORIZONTAL_LEFT); - $this->setPosHorizontalRel(self::POSITION_HORIZONTAL_RELATIVE_CHAR); - $this->setPosVertical(self::POSITION_VERTICAL_TOP); - $this->setPosVerticalRel(self::POSITION_VERTICAL_RELATIVE_LINE); + $this->width = null; + $this->height = null; + $this->align = null; + $this->marginTop = null; + $this->marginLeft = null; + $this->setWrappingStyle(self::WRAPPING_STYLE_INLINE); + $this->setPositioning(self::POSITION_RELATIVE); + $this->setPosHorizontal(self::POSITION_HORIZONTAL_LEFT); + $this->setPosHorizontalRel(self::POSITION_HORIZONTAL_RELATIVE_CHAR); + $this->setPosVertical(self::POSITION_VERTICAL_TOP); + $this->setPosVerticalRel(self::POSITION_VERTICAL_RELATIVE_LINE); } /** @@ -146,17 +175,17 @@ class Image extends AbstractStyle */ public function getWidth() { - return $this->width; + return $this->width; } /** * Set width * - * @param int $pValue + * @param int $pValue */ public function setWidth($pValue = null) { - $this->width = $pValue; + $this->width = $pValue; } /** @@ -164,17 +193,17 @@ class Image extends AbstractStyle */ public function getHeight() { - return $this->height; + return $this->height; } /** * Set height * - * @param int $pValue + * @param int $pValue */ public function setHeight($pValue = null) { - $this->height = $pValue; + $this->height = $pValue; } /** @@ -182,17 +211,17 @@ class Image extends AbstractStyle */ public function getAlign() { - return $this->align; + return $this->align; } /** * Set alignment * - * @param string $pValue + * @param string $pValue */ public function setAlign($pValue = null) { - $this->align = $pValue; + $this->align = $pValue; } /** @@ -202,19 +231,19 @@ class Image extends AbstractStyle */ public function getMarginTop() { - return $this->marginTop; + return $this->marginTop; } /** * Set Margin Top * - * @param int $pValue + * @param int $pValue * @return $this */ public function setMarginTop($pValue = null) { - $this->marginTop = $pValue; - return $this; + $this->marginTop = $pValue; + return $this; } /** @@ -224,25 +253,25 @@ class Image extends AbstractStyle */ public function getMarginLeft() { - return $this->marginLeft; + return $this->marginLeft; } /** * Set Margin Left * - * @param int $pValue + * @param int $pValue * @return $this */ public function setMarginLeft($pValue = null) { - $this->marginLeft = $pValue; - return $this; + $this->marginLeft = $pValue; + return $this; } /** * Set wrapping style * - * @param string $wrappingStyle + * @param string $wrappingStyle * @throws \InvalidArgumentException * @return $this */ @@ -269,178 +298,176 @@ class Image extends AbstractStyle */ public function getWrappingStyle() { - return $this->wrappingStyle; + return $this->wrappingStyle; } /** * Set positioning type * - * @param string $positioning + * @param string $positioning * @throws \InvalidArgumentException * @return $this */ - public function setPositioning($positioning) { - switch ($positioning) { - case self::POSITION_RELATIVE: - case self::POSITION_ABSOLUTE: - $this->positioning = $positioning; - break; - default: - throw new InvalidArgumentException('Positioning does not exists'); - break; - } - return $this; + switch ($positioning) { + case self::POSITION_RELATIVE: + case self::POSITION_ABSOLUTE: + $this->positioning = $positioning; + break; + default: + throw new InvalidArgumentException('Positioning does not exists'); + break; + } + return $this; } - + /** * Get positioning type - * + * * @return string */ public function getPositioning() { - return $this->positioning; + return $this->positioning; } /** * Set horizontal alignment * - * @param string $alignment + * @param string $alignment * @throws \InvalidArgumentException * @return $this */ public function setPosHorizontal($alignment) { - switch ($alignment) { - case self::POSITION_HORIZONTAL_LEFT: - case self::POSITION_HORIZONTAL_CENTER: - case self::POSITION_HORIZONTAL_RIGHT: - $this->posHorizontal = $alignment; - break; - default: - throw new InvalidArgumentException('Horizontal alignment does not exists'); - break; - } - return $this; + switch ($alignment) { + case self::POSITION_HORIZONTAL_LEFT: + case self::POSITION_HORIZONTAL_CENTER: + case self::POSITION_HORIZONTAL_RIGHT: + $this->posHorizontal = $alignment; + break; + default: + throw new InvalidArgumentException('Horizontal alignment does not exists'); + break; + } + return $this; } - + /** * Get horizontal alignment - * + * * @return string */ public function getPosHorizontal() { - return $this->posHorizontal; + return $this->posHorizontal; } /** * Set vertical alignment * - * @param string $alignment + * @param string $alignment * @throws \InvalidArgumentException * @return $this */ - public function setPosVertical($alignment) { - switch ($alignment) { - case self::POSITION_VERTICAL_TOP: - case self::POSITION_VERTICAL_CENTER: - case self::POSITION_VERTICAL_BOTTOM: - case self::POSITION_VERTICAL_INSIDE: - case self::POSITION_VERTICAL_OUTSIDE: - $this->posVertical = $alignment; - break; - default: - throw new InvalidArgumentException('Vertical alignment does not exists'); - break; - } - return $this; + switch ($alignment) { + case self::POSITION_VERTICAL_TOP: + case self::POSITION_VERTICAL_CENTER: + case self::POSITION_VERTICAL_BOTTOM: + case self::POSITION_VERTICAL_INSIDE: + case self::POSITION_VERTICAL_OUTSIDE: + $this->posVertical = $alignment; + break; + default: + throw new InvalidArgumentException('Vertical alignment does not exists'); + break; + } + return $this; } - + /** * Get vertical alignment - * + * * @return string */ public function getPosVertical() { - return $this->posVertical; + return $this->posVertical; } - + /** * Set horizontal relation * - * @param string $relto + * @param string $relto * @throws \InvalidArgumentException * @return $this */ public function setPosHorizontalRel($relto) { - switch ($relto) { - case self::POSITION_HORIZONTAL_RELATIVE_MARGIN: - case self::POSITION_HORIZONTAL_RELATIVE_PAGE: - case self::POSITION_HORIZONTAL_RELATIVE_COLUMN: - case self::POSITION_HORIZONTAL_RELATIVE_CHAR: - case self::POSITION_HORIZONTAL_RELATIVE_LMARGIN: - case self::POSITION_HORIZONTAL_RELATIVE_RMARGIN: - case self::POSITION_HORIZONTAL_RELATIVE_IMARGIN: - case self::POSITION_HORIZONTAL_RELATIVE_OMARGIN: - $this->posHorizontalRel = $relto; - break; - default: - throw new InvalidArgumentException('Horizontal relation does not exists'); - break; - } - return $this; + switch ($relto) { + case self::POSITION_HORIZONTAL_RELATIVE_MARGIN: + case self::POSITION_HORIZONTAL_RELATIVE_PAGE: + case self::POSITION_HORIZONTAL_RELATIVE_COLUMN: + case self::POSITION_HORIZONTAL_RELATIVE_CHAR: + case self::POSITION_HORIZONTAL_RELATIVE_LMARGIN: + case self::POSITION_HORIZONTAL_RELATIVE_RMARGIN: + case self::POSITION_HORIZONTAL_RELATIVE_IMARGIN: + case self::POSITION_HORIZONTAL_RELATIVE_OMARGIN: + $this->posHorizontalRel = $relto; + break; + default: + throw new InvalidArgumentException('Horizontal relation does not exists'); + break; + } + return $this; } - + /** * Get horizontal relation - * + * * @return string */ public function getPosHorizontalRel() { - return $this->posHorizontalRel; + return $this->posHorizontalRel; } - + /** * Set vertical relation * - * @param string $relto + * @param string $relto * @throws \InvalidArgumentException * @return $this */ public function setPosVerticalRel($relto) { - switch ($relto) { - case self::POSITION_VERTICAL_RELATIVE_MARGIN: - case self::POSITION_VERTICAL_RELATIVE_PAGE: - case self::POSITION_VERTICAL_RELATIVE_LINE: - case self::POSITION_VERTICAL_RELATIVE_TMARGIN: - case self::POSITION_VERTICAL_RELATIVE_BMARGIN: - case self::POSITION_VERTICAL_RELATIVE_IMARGIN: - case self::POSITION_VERTICAL_RELATIVE_OMARGIN: - $this->posVerticalRel = $relto; - break; - default: - throw new InvalidArgumentException('Vertical relation does not exists'); - break; - } - return $this; + switch ($relto) { + case self::POSITION_VERTICAL_RELATIVE_MARGIN: + case self::POSITION_VERTICAL_RELATIVE_PAGE: + case self::POSITION_VERTICAL_RELATIVE_LINE: + case self::POSITION_VERTICAL_RELATIVE_TMARGIN: + case self::POSITION_VERTICAL_RELATIVE_BMARGIN: + case self::POSITION_VERTICAL_RELATIVE_IMARGIN: + case self::POSITION_VERTICAL_RELATIVE_OMARGIN: + $this->posVerticalRel = $relto; + break; + default: + throw new InvalidArgumentException('Vertical relation does not exists'); + break; + } + return $this; } - + /** * Get vertical relation - * + * * @return string */ public function getPosVerticalRel() { - return $this->posVerticalRel; + return $this->posVerticalRel; } } diff --git a/src/PhpWord/Writer/Word2007/Element/Image.php b/src/PhpWord/Writer/Word2007/Element/Image.php index 80b71121..eb73536d 100644 --- a/src/PhpWord/Writer/Word2007/Element/Image.php +++ b/src/PhpWord/Writer/Word2007/Element/Image.php @@ -59,21 +59,21 @@ class Image extends Element if (null !== $marginLeft) { $imgStyle .= 'margin-left:' . $marginLeft . 'px;'; } - $imgStyle.='position:absolute;mso-width-percent:0;mso-height-percent:0;mso-width-relative:margin;mso-height-relative:margin;'; - switch ($positioning) { - case ImageStyle::POSITION_RELATIVE: - $imgStyle.='mso-position-horizontal:'.$style->getPosHorizontal().';'; - $imgStyle.='mso-position-horizontal-relative:'.$style->getPosHorizontalRel().';'; - $imgStyle.='mso-position-vertical:'.$style->getPosVertical().';'; - $imgStyle.='mso-position-vertical-relative:'.$style->getPosVerticalRel().';'; - $imgStyle.='margin-left:0;margin-top:0;'; - break; - - case ImageStyle::POSITION_ABSOLUTE: - $imgStyle.='mso-position-horizontal-relative:page;'; - $imgStyle.='mso-position-vertical-relative:page;'; - break; - } + $imgStyle.='position:absolute;mso-width-percent:0;mso-height-percent:0;'; + $imgStyle.='mso-width-relative:margin;mso-height-relative:margin;'; + switch ($positioning) { + case ImageStyle::POSITION_RELATIVE: + $imgStyle.='mso-position-horizontal:'.$style->getPosHorizontal().';'; + $imgStyle.='mso-position-horizontal-relative:'.$style->getPosHorizontalRel().';'; + $imgStyle.='mso-position-vertical:'.$style->getPosVertical().';'; + $imgStyle.='mso-position-vertical-relative:'.$style->getPosVerticalRel().';'; + $imgStyle.='margin-left:0;margin-top:0;'; + break; + case ImageStyle::POSITION_ABSOLUTE: + $imgStyle.='mso-position-horizontal-relative:page;'; + $imgStyle.='mso-position-vertical-relative:page;'; + break; + } switch ($wrappingStyle) { case ImageStyle::WRAPPING_STYLE_BEHIND: From 1a544cb2fa3c20015892fc19f5f5bce9448f5324 Mon Sep 17 00:00:00 2001 From: Bas-Jan 't Jong Date: Sun, 4 May 2014 10:36:13 +0200 Subject: [PATCH 4/5] Fixed bug in header / footer, in which media elements were all assigned to the first header element. This resulted in a corrupted DOCX, when a media element was assigned to a second header in the same section. --- src/PhpWord/Element/AbstractContainer.php | 1 + 1 file changed, 1 insertion(+) diff --git a/src/PhpWord/Element/AbstractContainer.php b/src/PhpWord/Element/AbstractContainer.php index ea3450a5..b90f6d72 100644 --- a/src/PhpWord/Element/AbstractContainer.php +++ b/src/PhpWord/Element/AbstractContainer.php @@ -412,6 +412,7 @@ abstract class AbstractContainer extends AbstractElement $docPart = $isCellTextrun ? $this->getDocPart() : $this->container; $docPartId = $isCellTextrun ? $this->getDocPartId() : $this->sectionId; $inHeaderFooter = ($docPart == 'header' || $docPart == 'footer'); + $docPartId = $inHeaderFooter ? $this->getDocPartId() : $docPartId; return $inHeaderFooter ? $docPart . $docPartId : $docPart; } From 42c712e334a25f0893f74a9f5562c288d12132ff Mon Sep 17 00:00:00 2001 From: Ivan Lanin Date: Sun, 4 May 2014 21:16:12 +0700 Subject: [PATCH 5/5] Refactor new image styles and create `Word2007/Style/Image` class --- CHANGELOG.md | 9 +- samples/Sample_13_Images.php | 2 +- src/PhpWord/Style/Image.php | 349 ++++++++---------- src/PhpWord/Writer/Word2007/Element/Image.php | 87 +---- src/PhpWord/Writer/Word2007/Style/Image.php | 140 +++++++ 5 files changed, 316 insertions(+), 271 deletions(-) create mode 100644 src/PhpWord/Writer/Word2007/Style/Image.php diff --git a/CHANGELOG.md b/CHANGELOG.md index 57c1b700..ff717ebb 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,19 +8,16 @@ PHPWord license is changed from LGPL 2.1 to LGPL 3 in this release. ### Features -- +- Image: Ability to define relative and absolute positioning - @basjan GH-217 ### Bugfixes -- - -### Deprecated - -- +- ... ### Miscellaneous - License: Change the project license from LGPL 2.1 into LGPL 3.0 - GH-211 +- Word2007 Writer: New `Style\Image` class - @ivanlanin ## 0.10.0 - 4 May 2014 diff --git a/samples/Sample_13_Images.php b/samples/Sample_13_Images.php index 65e38bf5..78423065 100644 --- a/samples/Sample_13_Images.php +++ b/samples/Sample_13_Images.php @@ -26,7 +26,7 @@ $wrappingStyles = array('inline', 'behind', 'infront', 'square', 'tight'); foreach ($wrappingStyles as $wrappingStyle) { $section->addTextBreak(5); $section->addText('Wrapping style ' . $wrappingStyle); - $section->addImage('resources/_earth.jpg', array('marginTop' => -1, 'marginLeft' => 1, + $section->addImage('resources/_earth.jpg', array('positioning' => 'relative', 'marginTop' => -1, 'marginLeft' => 1, 'width' => 80, 'height' => 80, 'wrappingStyle' => $wrappingStyle)); $section->addText($text); } diff --git a/src/PhpWord/Style/Image.php b/src/PhpWord/Style/Image.php index cece4407..b258fa08 100644 --- a/src/PhpWord/Style/Image.php +++ b/src/PhpWord/Style/Image.php @@ -14,66 +14,61 @@ namespace PhpOffice\PhpWord\Style; */ class Image extends AbstractStyle { - + /** + * Wrapping styles + * + * @const string + */ const WRAPPING_STYLE_INLINE = 'inline'; - const WRAPPING_STYLE_SQUARE = 'square'; - const WRAPPING_STYLE_TIGHT = 'tight'; - const WRAPPING_STYLE_BEHIND = 'behind'; - const WRAPPING_STYLE_INFRONT = 'infront'; + /** + * Horizontal alignment + * + * @const string + */ const POSITION_HORIZONTAL_LEFT = 'left'; - const POSITION_HORIZONTAL_CENTER = 'centered'; - const POSITION_HORIZONTAL_RIGHT = 'right'; + /** + * Vertical alignment + * + * @const string + */ const POSITION_VERTICAL_TOP = 'top'; - const POSITION_VERTICAL_CENTER = 'center'; - const POSITION_VERTICAL_BOTTOM = 'bottom'; - const POSITION_VERTICAL_INSIDE = 'inside'; - const POSITION_VERTICAL_OUTSIDE = 'outside'; - const POSITION_HORIZONTAL_RELATIVE_MARGIN = 'margin'; - - const POSITION_HORIZONTAL_RELATIVE_PAGE = 'page'; - - const POSITION_HORIZONTAL_RELATIVE_COLUMN = 'column'; - - const POSITION_HORIZONTAL_RELATIVE_CHAR = 'char'; - - const POSITION_HORIZONTAL_RELATIVE_LMARGIN = 'left-margin-area'; - - const POSITION_HORIZONTAL_RELATIVE_RMARGIN = 'right-margin-area'; - - const POSITION_HORIZONTAL_RELATIVE_IMARGIN = 'inner-margin-area'; - - const POSITION_HORIZONTAL_RELATIVE_OMARGIN = 'outer-margin-area'; - - const POSITION_VERTICAL_RELATIVE_MARGIN = 'margin'; - - const POSITION_VERTICAL_RELATIVE_PAGE = 'page'; - - const POSITION_VERTICAL_RELATIVE_LINE = 'line'; - - const POSITION_VERTICAL_RELATIVE_TMARGIN = 'top-margin-area'; - - const POSITION_VERTICAL_RELATIVE_BMARGIN = 'bottom-margin-area'; - - const POSITION_VERTICAL_RELATIVE_IMARGIN = 'inner-margin-area'; - - const POSITION_VERTICAL_RELATIVE_OMARGIN = 'outer-margin-area'; - - const POSITION_RELATIVE = 'relative'; + /** + * Position relative to + * + * @const string + */ + const POSITION_RELATIVE_TO_MARGIN = 'margin'; + const POSITION_RELATIVE_TO_PAGE = 'page'; + const POSITION_RELATIVE_TO_COLUMN = 'column'; // horizontal only + const POSITION_RELATIVE_TO_CHAR = 'char'; // horizontal only + const POSITION_RELATIVE_TO_LINE = 'line'; // vertical only + const POSITION_RELATIVE_TO_LMARGIN = 'left-margin-area'; // horizontal only + const POSITION_RELATIVE_TO_RMARGIN = 'right-margin-area'; // horizontal only + const POSITION_RELATIVE_TO_TMARGIN = 'top-margin-area'; // vertical only + const POSITION_RELATIVE_TO_BMARGIN = 'bottom-margin-area'; // vertical only + const POSITION_RELATIVE_TO_IMARGIN = 'inner-margin-area'; + const POSITION_RELATIVE_TO_OMARGIN = 'outer-margin-area'; + /** + * Position type, relative/absolute + * + * @const string + */ const POSITION_ABSOLUTE = 'absolute'; + const POSITION_RELATIVE = 'relative'; /** * Image width @@ -117,6 +112,13 @@ class Image extends AbstractStyle */ private $wrappingStyle; + /** + * Positioning type (relative or absolute) + * + * @var string + */ + private $positioning; + /** * Horizontal alignment * @@ -145,29 +147,16 @@ class Image extends AbstractStyle */ private $posVerticalRel; - /** - * Positioning type (Relative or Absolute) - * - * @var string - */ - private $positioning; - /** * Create new image style */ public function __construct() { - $this->width = null; - $this->height = null; - $this->align = null; - $this->marginTop = null; - $this->marginLeft = null; - $this->setWrappingStyle(self::WRAPPING_STYLE_INLINE); - $this->setPositioning(self::POSITION_RELATIVE); - $this->setPosHorizontal(self::POSITION_HORIZONTAL_LEFT); - $this->setPosHorizontalRel(self::POSITION_HORIZONTAL_RELATIVE_CHAR); - $this->setPosVertical(self::POSITION_VERTICAL_TOP); - $this->setPosVerticalRel(self::POSITION_VERTICAL_RELATIVE_LINE); + $this->setWrappingStyle(self::WRAPPING_STYLE_INLINE); + $this->setPosHorizontal(self::POSITION_HORIZONTAL_LEFT); + $this->setPosHorizontalRel(self::POSITION_RELATIVE_TO_CHAR); + $this->setPosVertical(self::POSITION_VERTICAL_TOP); + $this->setPosVerticalRel(self::POSITION_RELATIVE_TO_LINE); } /** @@ -175,17 +164,17 @@ class Image extends AbstractStyle */ public function getWidth() { - return $this->width; + return $this->width; } /** * Set width * - * @param int $pValue + * @param int $value */ - public function setWidth($pValue = null) + public function setWidth($value = null) { - $this->width = $pValue; + $this->width = $value; } /** @@ -193,17 +182,17 @@ class Image extends AbstractStyle */ public function getHeight() { - return $this->height; + return $this->height; } /** * Set height * - * @param int $pValue + * @param int $value */ - public function setHeight($pValue = null) + public function setHeight($value = null) { - $this->height = $pValue; + $this->height = $value; } /** @@ -211,17 +200,17 @@ class Image extends AbstractStyle */ public function getAlign() { - return $this->align; + return $this->align; } /** * Set alignment * - * @param string $pValue + * @param string $value */ - public function setAlign($pValue = null) + public function setAlign($value = null) { - $this->align = $pValue; + $this->align = $value; } /** @@ -231,19 +220,19 @@ class Image extends AbstractStyle */ public function getMarginTop() { - return $this->marginTop; + return $this->marginTop; } /** * Set Margin Top * - * @param int $pValue - * @return $this + * @param int $value + * @return self */ - public function setMarginTop($pValue = null) + public function setMarginTop($value = null) { - $this->marginTop = $pValue; - return $this; + $this->marginTop = $value; + return $this; } /** @@ -253,41 +242,18 @@ class Image extends AbstractStyle */ public function getMarginLeft() { - return $this->marginLeft; + return $this->marginLeft; } /** * Set Margin Left * - * @param int $pValue - * @return $this + * @param int $value + * @return self */ - public function setMarginLeft($pValue = null) + public function setMarginLeft($value = null) { - $this->marginLeft = $pValue; - return $this; - } - - /** - * Set wrapping style - * - * @param string $wrappingStyle - * @throws \InvalidArgumentException - * @return $this - */ - public function setWrappingStyle($wrappingStyle) - { - switch ($wrappingStyle) { - case self::WRAPPING_STYLE_BEHIND: - case self::WRAPPING_STYLE_INFRONT: - case self::WRAPPING_STYLE_INLINE: - case self::WRAPPING_STYLE_SQUARE: - case self::WRAPPING_STYLE_TIGHT: - $this->wrappingStyle = $wrappingStyle; - break; - default: - throw new \InvalidArgumentException('Wrapping style does not exists'); - } + $this->marginLeft = $value; return $this; } @@ -298,27 +264,27 @@ class Image extends AbstractStyle */ public function getWrappingStyle() { - return $this->wrappingStyle; + return $this->wrappingStyle; } /** - * Set positioning type + * Set wrapping style * - * @param string $positioning + * @param string $wrappingStyle * @throws \InvalidArgumentException - * @return $this + * @return self */ - public function setPositioning($positioning) + public function setWrappingStyle($wrappingStyle) { - switch ($positioning) { - case self::POSITION_RELATIVE: - case self::POSITION_ABSOLUTE: - $this->positioning = $positioning; - break; - default: - throw new InvalidArgumentException('Positioning does not exists'); - break; + $enum = array(self::WRAPPING_STYLE_INLINE, self::WRAPPING_STYLE_INFRONT, self::WRAPPING_STYLE_BEHIND, + self::WRAPPING_STYLE_SQUARE, self::WRAPPING_STYLE_TIGHT); + + if (in_array($wrappingStyle, $enum)) { + $this->wrappingStyle = $wrappingStyle; + } else { + throw new \InvalidArgumentException('Invalid wrapping style.'); } + return $this; } @@ -329,28 +295,26 @@ class Image extends AbstractStyle */ public function getPositioning() { - return $this->positioning; + return $this->positioning; } /** - * Set horizontal alignment + * Set positioning type * - * @param string $alignment + * @param string $positioning * @throws \InvalidArgumentException - * @return $this + * @return self */ - public function setPosHorizontal($alignment) + public function setPositioning($positioning) { - switch ($alignment) { - case self::POSITION_HORIZONTAL_LEFT: - case self::POSITION_HORIZONTAL_CENTER: - case self::POSITION_HORIZONTAL_RIGHT: - $this->posHorizontal = $alignment; - break; - default: - throw new InvalidArgumentException('Horizontal alignment does not exists'); - break; + $enum = array(self::POSITION_RELATIVE, self::POSITION_ABSOLUTE); + + if (in_array($positioning, $enum)) { + $this->positioning = $positioning; + } else { + throw new \InvalidArgumentException('Invalid positioning.'); } + return $this; } @@ -361,30 +325,27 @@ class Image extends AbstractStyle */ public function getPosHorizontal() { - return $this->posHorizontal; + return $this->posHorizontal; } /** - * Set vertical alignment + * Set horizontal alignment * - * @param string $alignment + * @param string $alignment * @throws \InvalidArgumentException - * @return $this + * @return self */ - public function setPosVertical($alignment) + public function setPosHorizontal($alignment) { - switch ($alignment) { - case self::POSITION_VERTICAL_TOP: - case self::POSITION_VERTICAL_CENTER: - case self::POSITION_VERTICAL_BOTTOM: - case self::POSITION_VERTICAL_INSIDE: - case self::POSITION_VERTICAL_OUTSIDE: - $this->posVertical = $alignment; - break; - default: - throw new InvalidArgumentException('Vertical alignment does not exists'); - break; + $enum = array(self::POSITION_HORIZONTAL_LEFT, self::POSITION_HORIZONTAL_CENTER, + self::POSITION_HORIZONTAL_RIGHT); + + if (in_array($alignment, $enum)) { + $this->posHorizontal = $alignment; + } else { + throw new \InvalidArgumentException('Invalid horizontal alignment.'); } + return $this; } @@ -395,33 +356,27 @@ class Image extends AbstractStyle */ public function getPosVertical() { - return $this->posVertical; + return $this->posVertical; } /** - * Set horizontal relation + * Set vertical alignment * - * @param string $relto + * @param string $alignment * @throws \InvalidArgumentException - * @return $this + * @return self */ - public function setPosHorizontalRel($relto) + public function setPosVertical($alignment) { - switch ($relto) { - case self::POSITION_HORIZONTAL_RELATIVE_MARGIN: - case self::POSITION_HORIZONTAL_RELATIVE_PAGE: - case self::POSITION_HORIZONTAL_RELATIVE_COLUMN: - case self::POSITION_HORIZONTAL_RELATIVE_CHAR: - case self::POSITION_HORIZONTAL_RELATIVE_LMARGIN: - case self::POSITION_HORIZONTAL_RELATIVE_RMARGIN: - case self::POSITION_HORIZONTAL_RELATIVE_IMARGIN: - case self::POSITION_HORIZONTAL_RELATIVE_OMARGIN: - $this->posHorizontalRel = $relto; - break; - default: - throw new InvalidArgumentException('Horizontal relation does not exists'); - break; + $enum = array(self::POSITION_VERTICAL_TOP, self::POSITION_VERTICAL_CENTER, + self::POSITION_VERTICAL_BOTTOM, self::POSITION_VERTICAL_INSIDE, self::POSITION_VERTICAL_OUTSIDE); + + if (in_array($alignment, $enum)) { + $this->posVertical = $alignment; + } else { + throw new \InvalidArgumentException('Invalid vertical alignment.'); } + return $this; } @@ -432,32 +387,29 @@ class Image extends AbstractStyle */ public function getPosHorizontalRel() { - return $this->posHorizontalRel; + return $this->posHorizontalRel; } /** - * Set vertical relation + * Set horizontal relation * - * @param string $relto + * @param string $relto * @throws \InvalidArgumentException - * @return $this + * @return self */ - public function setPosVerticalRel($relto) + public function setPosHorizontalRel($relto) { - switch ($relto) { - case self::POSITION_VERTICAL_RELATIVE_MARGIN: - case self::POSITION_VERTICAL_RELATIVE_PAGE: - case self::POSITION_VERTICAL_RELATIVE_LINE: - case self::POSITION_VERTICAL_RELATIVE_TMARGIN: - case self::POSITION_VERTICAL_RELATIVE_BMARGIN: - case self::POSITION_VERTICAL_RELATIVE_IMARGIN: - case self::POSITION_VERTICAL_RELATIVE_OMARGIN: - $this->posVerticalRel = $relto; - break; - default: - throw new InvalidArgumentException('Vertical relation does not exists'); - break; + $enum = array(self::POSITION_RELATIVE_TO_MARGIN, self::POSITION_RELATIVE_TO_PAGE, + self::POSITION_RELATIVE_TO_COLUMN, self::POSITION_RELATIVE_TO_CHAR, + self::POSITION_RELATIVE_TO_LMARGIN, self::POSITION_RELATIVE_TO_RMARGIN, + self::POSITION_RELATIVE_TO_IMARGIN, self::POSITION_RELATIVE_TO_OMARGIN); + + if (in_array($relto, $enum)) { + $this->posHorizontalRel = $relto; + } else { + throw new \InvalidArgumentException('Invalid relative horizontal alignment.'); } + return $this; } @@ -468,6 +420,29 @@ class Image extends AbstractStyle */ public function getPosVerticalRel() { - return $this->posVerticalRel; + return $this->posVerticalRel; + } + + /** + * Set vertical relation + * + * @param string $relto + * @throws \InvalidArgumentException + * @return self + */ + public function setPosVerticalRel($relto) + { + $enum = array(self::POSITION_RELATIVE_TO_MARGIN, self::POSITION_RELATIVE_TO_PAGE, + self::POSITION_RELATIVE_TO_LINE, + self::POSITION_RELATIVE_TO_TMARGIN, self::POSITION_RELATIVE_TO_BMARGIN, + self::POSITION_RELATIVE_TO_IMARGIN, self::POSITION_RELATIVE_TO_OMARGIN); + + if (in_array($relto, $enum)) { + $this->posVerticalRel = $relto; + } else { + throw new \InvalidArgumentException('Invalid relative vertical alignment.'); + } + + return $this; } } diff --git a/src/PhpWord/Writer/Word2007/Element/Image.php b/src/PhpWord/Writer/Word2007/Element/Image.php index 60baaadf..b7d24cb5 100644 --- a/src/PhpWord/Writer/Word2007/Element/Image.php +++ b/src/PhpWord/Writer/Word2007/Element/Image.php @@ -10,6 +10,7 @@ namespace PhpOffice\PhpWord\Writer\Word2007\Element; use PhpOffice\PhpWord\Style\Image as ImageStyle; +use PhpOffice\PhpWord\Writer\Word2007\Style\Image as ImageStyleWriter; /** * Image element writer @@ -36,86 +37,30 @@ class Image extends Element private function writeImage() { $rId = $this->element->getRelationId() + ($this->element->isInSection() ? 6 : 0); - $style = $this->element->getStyle(); - $width = $style->getWidth(); - $height = $style->getHeight(); - $align = $style->getAlign(); - $marginTop = $style->getMarginTop(); - $marginLeft = $style->getMarginLeft(); - $wrappingStyle = $style->getWrappingStyle(); - $positioning = $style->getPositioning(); - $w10wrapType = null; - $imgStyle = ''; - if (null !== $width) { - $imgStyle .= 'width:' . $width . 'px;'; - } - if (null !== $height) { - $imgStyle .= 'height:' . $height . 'px;'; - } - if (null !== $marginTop) { - $imgStyle .= 'margin-top:' . $marginTop . 'px;'; - } - if (null !== $marginLeft) { - $imgStyle .= 'margin-left:' . $marginLeft . 'px;'; - } - $imgStyle.='position:absolute;mso-width-percent:0;mso-height-percent:0;'; - $imgStyle.='mso-width-relative:margin;mso-height-relative:margin;'; - switch ($positioning) { - case ImageStyle::POSITION_RELATIVE: - $imgStyle.='mso-position-horizontal:'.$style->getPosHorizontal().';'; - $imgStyle.='mso-position-horizontal-relative:'.$style->getPosHorizontalRel().';'; - $imgStyle.='mso-position-vertical:'.$style->getPosVertical().';'; - $imgStyle.='mso-position-vertical-relative:'.$style->getPosVerticalRel().';'; - $imgStyle.='margin-left:0;margin-top:0;'; - break; - case ImageStyle::POSITION_ABSOLUTE: - $imgStyle.='mso-position-horizontal-relative:page;'; - $imgStyle.='mso-position-vertical-relative:page;'; - break; - } + $styleWriter = new ImageStyleWriter($this->xmlWriter, $style); - switch ($wrappingStyle) { - case ImageStyle::WRAPPING_STYLE_BEHIND: - $imgStyle .= 'z-index:-251658752;'; - break; - case ImageStyle::WRAPPING_STYLE_INFRONT: - $imgStyle .= 'z-index:251659264;mso-position-horizontal:absolute;mso-position-vertical:absolute;'; - break; - case ImageStyle::WRAPPING_STYLE_SQUARE: - $imgStyle .= 'z-index:251659264;mso-position-horizontal:absolute;mso-position-vertical:absolute;'; - $w10wrapType = 'square'; - break; - case ImageStyle::WRAPPING_STYLE_TIGHT: - $imgStyle .= 'z-index:251659264;mso-position-horizontal:absolute;mso-position-vertical:absolute;'; - $w10wrapType = 'tight'; - break; - } - if (!$this->withoutP) { $this->xmlWriter->startElement('w:p'); - if (!is_null($align)) { + if (!is_null($style->getAlign())) { $this->xmlWriter->startElement('w:pPr'); $this->xmlWriter->startElement('w:jc'); - $this->xmlWriter->writeAttribute('w:val', $align); + $this->xmlWriter->writeAttribute('w:val', $style->getAlign()); $this->xmlWriter->endElement(); // w:jc $this->xmlWriter->endElement(); // w:pPr } } + $this->xmlWriter->startElement('w:r'); $this->xmlWriter->startElement('w:pict'); $this->xmlWriter->startElement('v:shape'); $this->xmlWriter->writeAttribute('type', '#_x0000_t75'); - $this->xmlWriter->writeAttribute('style', $imgStyle); + $styleWriter->write(); $this->xmlWriter->startElement('v:imagedata'); $this->xmlWriter->writeAttribute('r:id', 'rId' . $rId); $this->xmlWriter->writeAttribute('o:title', ''); $this->xmlWriter->endElement(); // v:imagedata - if (!is_null($w10wrapType)) { - $this->xmlWriter->startElement('w10:wrap'); - $this->xmlWriter->writeAttribute('type', $w10wrapType); - $this->xmlWriter->endElement(); // w10:wrap - } + $styleWriter->writeW10Wrap(); $this->xmlWriter->endElement(); // v:shape $this->xmlWriter->endElement(); // w:pict $this->xmlWriter->endElement(); // w:r @@ -130,28 +75,16 @@ class Image extends Element private function writeWatermark() { $rId = $this->element->getRelationId(); - $style = $this->element->getStyle(); - $width = $style->getWidth(); - $height = $style->getHeight(); - $marginLeft = $style->getMarginLeft(); - $marginTop = $style->getMarginTop(); - $strStyle = 'position:absolute;'; - $strStyle .= ' width:' . $width . 'px;'; - $strStyle .= ' height:' . $height . 'px;'; - if (!is_null($marginTop)) { - $strStyle .= ' margin-top:' . $marginTop . 'px;'; - } - if (!is_null($marginLeft)) { - $strStyle .= ' margin-left:' . $marginLeft . 'px;'; - } + $style->setPositioning('absolute'); + $styleWriter = new ImageStyleWriter($this->xmlWriter, $style); $this->xmlWriter->startElement('w:p'); $this->xmlWriter->startElement('w:r'); $this->xmlWriter->startElement('w:pict'); $this->xmlWriter->startElement('v:shape'); $this->xmlWriter->writeAttribute('type', '#_x0000_t75'); - $this->xmlWriter->writeAttribute('style', $strStyle); + $styleWriter->write(); $this->xmlWriter->startElement('v:imagedata'); $this->xmlWriter->writeAttribute('r:id', 'rId' . $rId); $this->xmlWriter->writeAttribute('o:title', ''); diff --git a/src/PhpWord/Writer/Word2007/Style/Image.php b/src/PhpWord/Writer/Word2007/Style/Image.php new file mode 100644 index 00000000..e93e58cd --- /dev/null +++ b/src/PhpWord/Writer/Word2007/Style/Image.php @@ -0,0 +1,140 @@ +style instanceof \PhpOffice\PhpWord\Style\Image)) { + return; + } + + $wrapping = $this->style->getWrappingStyle(); + $positioning = $this->style->getPositioning(); + + // Default style array + $styleArray = array( + 'mso-width-percent' => '0', + 'mso-height-percent' => '0', + 'mso-width-relative' => 'margin', + 'mso-height-relative' => 'margin', + ); + $styleArray = array_merge($styleArray, $this->getElementStyle()); + + // Absolute/relative positioning + $styleArray['position'] = $positioning; + if ($positioning == ImageStyle::POSITION_ABSOLUTE) { + $styleArray['mso-position-horizontal-relative'] = 'page'; + $styleArray['mso-position-vertical-relative'] = 'page'; + } elseif ($positioning == ImageStyle::POSITION_RELATIVE) { + $styleArray['mso-position-horizontal'] = $this->style->getPosHorizontal(); + $styleArray['mso-position-vertical'] = $this->style->getPosVertical(); + $styleArray['mso-position-horizontal-relative'] = $this->style->getPosHorizontalRel(); + $styleArray['mso-position-vertical-relative'] = $this->style->getPosVerticalRel(); + $styleArray['margin-left'] = 0; + $styleArray['margin-top'] = 0; + } + + // Wrapping style + if ($wrapping == ImageStyle::WRAPPING_STYLE_INLINE) { + // Nothing to do when inline + } elseif ($wrapping == ImageStyle::WRAPPING_STYLE_BEHIND) { + $styleArray['z-index'] = -251658752; + } else { + $styleArray['z-index'] = 251659264; + $styleArray['mso-position-horizontal'] = 'absolute'; + $styleArray['mso-position-vertical'] = 'absolute'; + } + + // w10 wrapping + if ($wrapping == ImageStyle::WRAPPING_STYLE_SQUARE) { + $this->w10wrap = 'square'; + } elseif ($wrapping == ImageStyle::WRAPPING_STYLE_TIGHT) { + $this->w10wrap = 'tight'; + } + + $imageStyle = $this->assembleStyle($styleArray); + + $this->xmlWriter->writeAttribute('style', $imageStyle); + } + + /** + * Write w10 wrapping + * + * @return array + */ + public function writeW10Wrap() + { + if (!is_null($this->w10wrap)) { + $this->xmlWriter->startElement('w10:wrap'); + $this->xmlWriter->writeAttribute('type', $this->w10wrap); + $this->xmlWriter->endElement(); // w10:wrap + } + } + + /** + * Get element style + * + * @return array + */ + private function getElementStyle() + { + $styles = array(); + $styleValues = array( + 'width' => $this->style->getWidth(), + 'height' => $this->style->getHeight(), + 'margin-top' => $this->style->getMarginTop(), + 'margin-left' => $this->style->getMarginLeft() + ); + foreach ($styleValues as $key => $value) { + if (!is_null($value) && $value != '') { + $styles[$key] = $value . 'px'; + } + } + + return $styles; + } + + /** + * Assemble style array into style string + * + * @param array $styles + * @return string + */ + private function assembleStyle($styles = array()) + { + $style = ''; + foreach ($styles as $key => $value) { + if (!is_null($value) && $value != '') { + $style .= "{$key}:{$value}; "; + } + } + + return trim($style); + } +}