From fbce1c8fc92817a77bc793c735135cab78613bff Mon Sep 17 00:00:00 2001 From: Ivan Lanin Date: Wed, 21 May 2014 23:53:08 +0700 Subject: [PATCH] #244: Enable "image float left" --- CHANGELOG.md | 1 + samples/Sample_13_Images.php | 3 +++ src/PhpWord/Style/Image.php | 19 ++++++++++--------- src/PhpWord/Writer/Word2007/Style/Image.php | 7 +------ 4 files changed, 15 insertions(+), 15 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 58dd0affbb..4a9b172f90 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -27,6 +27,7 @@ This release marked the change of PHPWord license from LGPL 2.1 to LGPL 3; new r - ODT Writer: Enable title element and custom document properties - @ivanlanin - ODT Reader: Ability to read standard and custom document properties - @ivanlanin - Word2007 Writer: Enable the missing custom document properties writer - @ivanlanin +- Image: Enable "image float left" - @ivanlanin GH-244 ### Bugfixes diff --git a/samples/Sample_13_Images.php b/samples/Sample_13_Images.php index dd5209bf6d..dd0c88012d 100644 --- a/samples/Sample_13_Images.php +++ b/samples/Sample_13_Images.php @@ -40,6 +40,9 @@ 'width' => \PhpOffice\PhpWord\Shared\Drawing::centimetersToPixels(3), 'height' => \PhpOffice\PhpWord\Shared\Drawing::centimetersToPixels(3), 'positioning' => \PhpOffice\PhpWord\Style\Image::POSITION_ABSOLUTE, + 'posHorizontal' => \PhpOffice\PhpWord\Style\Image::POSITION_HORIZONTAL_RIGHT, + 'posHorizontalRel' => \PhpOffice\PhpWord\Style\Image::POSITION_RELATIVE_TO_PAGE, + 'posVerticalRel' => \PhpOffice\PhpWord\Style\Image::POSITION_RELATIVE_TO_PAGE, 'marginLeft' => \PhpOffice\PhpWord\Shared\Drawing::centimetersToPixels(15.5), 'marginTop' => \PhpOffice\PhpWord\Shared\Drawing::centimetersToPixels(1.55) ) diff --git a/src/PhpWord/Style/Image.php b/src/PhpWord/Style/Image.php index fc9f963071..33c85ddf23 100644 --- a/src/PhpWord/Style/Image.php +++ b/src/PhpWord/Style/Image.php @@ -61,6 +61,7 @@ class Image extends AbstractStyle 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_TEXT = 'text'; // vertical 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 @@ -103,14 +104,14 @@ class Image extends AbstractStyle * * @var int */ - private $marginTop; + private $marginTop = 0; /** * Margin Left * * @var int */ - private $marginLeft; + private $marginLeft = 0; /** * Wrapping style @@ -247,9 +248,9 @@ public function getMarginTop() * @param int $value * @return self */ - public function setMarginTop($value = null) + public function setMarginTop($value = 0) { - $this->marginTop = $value; + $this->marginTop = $this->setIntVal($value, 0); return $this; } @@ -270,9 +271,9 @@ public function getMarginLeft() * @param int $value * @return self */ - public function setMarginLeft($value = null) + public function setMarginLeft($value = 0) { - $this->marginLeft = $value; + $this->marginLeft = $this->setIntVal($value, 0); return $this; } @@ -352,7 +353,7 @@ public function setPosHorizontal($alignment) { $enum = array( self::POSITION_HORIZONTAL_LEFT, self::POSITION_HORIZONTAL_CENTER, - self::POSITION_HORIZONTAL_RIGHT, + self::POSITION_HORIZONTAL_RIGHT, self::POSITION_ABSOLUTE ); $this->posHorizontal = $this->setEnumVal($alignment, $enum, $this->posHorizontal); @@ -381,7 +382,7 @@ public function setPosVertical($alignment) $enum = array( self::POSITION_VERTICAL_TOP, self::POSITION_VERTICAL_CENTER, self::POSITION_VERTICAL_BOTTOM, self::POSITION_VERTICAL_INSIDE, - self::POSITION_VERTICAL_OUTSIDE, + self::POSITION_VERTICAL_OUTSIDE, self::POSITION_ABSOLUTE ); $this->posVertical = $this->setEnumVal($alignment, $enum, $this->posVertical); @@ -439,7 +440,7 @@ 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_TEXT, 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, ); diff --git a/src/PhpWord/Writer/Word2007/Style/Image.php b/src/PhpWord/Writer/Word2007/Style/Image.php index 36b308f159..b6b46535c2 100644 --- a/src/PhpWord/Writer/Word2007/Style/Image.php +++ b/src/PhpWord/Writer/Word2007/Style/Image.php @@ -65,16 +65,11 @@ protected function writeStyle(ImageStyle $style) // Absolute/relative positioning $positioning = $style->getPositioning(); $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) { + if ($positioning !== null) { $styleArray['mso-position-horizontal'] = $style->getPosHorizontal(); $styleArray['mso-position-vertical'] = $style->getPosVertical(); $styleArray['mso-position-horizontal-relative'] = $style->getPosHorizontalRel(); $styleArray['mso-position-vertical-relative'] = $style->getPosVerticalRel(); - $styleArray['margin-left'] = 0; - $styleArray['margin-top'] = 0; } // Wrapping style