Skip to content

Commit fa77c4f

Browse files
author
Gabriel Bull
committed
Added wrapping style to images
1 parent b6c33c8 commit fa77c4f

File tree

5 files changed

+115
-15
lines changed

5 files changed

+115
-15
lines changed

src/PHPWord/Autoloader.php

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -27,23 +27,35 @@
2727

2828
class PHPWord_Autoloader
2929
{
30-
public static function Register()
30+
/**
31+
* Register the autoloader
32+
*
33+
* @return void
34+
*/
35+
public static function register()
3136
{
32-
return spl_autoload_register(array('PHPWord_Autoloader', 'Load'));
37+
spl_autoload_register(array('PHPWord_Autoloader', 'load'));
3338
}
3439

35-
public static function Load($strObjectName)
40+
/**
41+
* Autoloader
42+
*
43+
* @param string $strObjectName
44+
* @return mixed
45+
*/
46+
public static function load($strObjectName)
3647
{
3748
if ((class_exists($strObjectName)) || (strpos($strObjectName, 'PHPWord') === false)) {
38-
return false;
49+
return null;
3950
}
4051

4152
$strObjectFilePath = PHPWORD_BASE_PATH . str_replace('_', '/', $strObjectName) . '.php';
4253

4354
if ((file_exists($strObjectFilePath) === false) || (is_readable($strObjectFilePath) === false)) {
44-
return false;
55+
return null;
4556
}
4657

47-
require($strObjectFilePath);
58+
require_once $strObjectFilePath;
59+
return true;
4860
}
49-
}
61+
}

src/PHPWord/Section/Image.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,10 @@ public function __construct($src, $style = null, $isWatermark = false)
9292
}
9393
}
9494

95+
if (isset($style['wrappingStyle'])) {
96+
$this->_style->setWrappingStyle($style['wrappingStyle']);
97+
}
98+
9599
if ($this->_style->getWidth() == null && $this->_style->getHeight() == null) {
96100
$imgData = getimagesize($this->_src);
97101
$this->_style->setWidth($imgData[0]);
@@ -173,5 +177,4 @@ public function setIsWatermark($pValue)
173177
{
174178
$this->_isWatermark = $pValue;
175179
}
176-
}
177-
180+
}

src/PHPWord/Shared/XMLWriter.php

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,16 @@
3030
define('DATE_W3C', 'Y-m-d\TH:i:sP');
3131
}
3232

33-
33+
/**
34+
* Class PHPWord_Shared_XMLWriter
35+
*
36+
* @category PHPWord
37+
* @package PHPWord_Section
38+
* @copyright Copyright (c) 2011 PHPWord
39+
* @method bool startElement(string $name)
40+
* @method bool writeAttribute(string $name, string $value)
41+
* @method bool endElement()
42+
*/
3443
class PHPWord_Shared_XMLWriter
3544
{
3645
/** Temporary storage method */
@@ -145,4 +154,4 @@ public function writeRaw($text)
145154

146155
return $this->text($text);
147156
}
148-
}
157+
}

src/PHPWord/Style/Image.php

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,16 @@
3535
*/
3636
class PHPWord_Style_Image
3737
{
38+
const WRAPPING_STYLE_INLINE = 'inline';
39+
const WRAPPING_STYLE_SQUARE = 'square';
40+
const WRAPPING_STYLE_TIGHT = 'tight';
41+
const WRAPPING_STYLE_BEHIND = 'behind';
42+
const WRAPPING_STYLE_INFRONT = 'infront';
3843

3944
private $_width;
4045
private $_height;
4146
private $_align;
47+
private $wrappingStyle;
4248

4349
/**
4450
* Margin Top
@@ -61,6 +67,7 @@ public function __construct()
6167
$this->_align = null;
6268
$this->_marginTop = null;
6369
$this->_marginLeft = null;
70+
$this->setWrappingStyle(self::WRAPPING_STYLE_INLINE);
6471
}
6572

6673
public function setStyleValue($key, $value)
@@ -112,6 +119,7 @@ public function getMarginTop()
112119
* Set Margin Top
113120
*
114121
* @param int $pValue
122+
* @return $this
115123
*/
116124
public function setMarginTop($pValue = null)
117125
{
@@ -133,10 +141,41 @@ public function getMarginLeft()
133141
* Set Margin Left
134142
*
135143
* @param int $pValue
144+
* @return $this
136145
*/
137146
public function setMarginLeft($pValue = null)
138147
{
139148
$this->_marginLeft = $pValue;
140149
return $this;
141150
}
142-
}
151+
152+
/**
153+
* @param string $wrappingStyle
154+
* @throws InvalidArgumentException
155+
* @return $this
156+
*/
157+
public function setWrappingStyle($wrappingStyle)
158+
{
159+
switch ($wrappingStyle) {
160+
case self::WRAPPING_STYLE_BEHIND:
161+
case self::WRAPPING_STYLE_INFRONT:
162+
case self::WRAPPING_STYLE_INLINE:
163+
case self::WRAPPING_STYLE_SQUARE:
164+
case self::WRAPPING_STYLE_TIGHT:
165+
$this->wrappingStyle = $wrappingStyle;
166+
break;
167+
default:
168+
throw new InvalidArgumentException('Wrapping style does not exists');
169+
break;
170+
}
171+
return $this;
172+
}
173+
174+
/**
175+
* @return string
176+
*/
177+
public function getWrappingStyle()
178+
{
179+
return $this->wrappingStyle;
180+
}
181+
}

src/PHPWord/Writer/Word2007/Base.php

Lines changed: 40 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -608,14 +608,21 @@ protected function _writeCellStyle(PHPWord_Shared_XMLWriter $objWriter = null, P
608608
}
609609
}
610610

611-
protected function _writeImage(PHPWord_Shared_XMLWriter $objWriter = null, $image)
611+
/**
612+
* @param \PHPWord_Shared_XMLWriter $objWriter
613+
* @param \PHPWord_Section_Image $image
614+
*/
615+
protected function _writeImage(PHPWord_Shared_XMLWriter $objWriter = null, PHPWord_Section_Image $image)
612616
{
613617
$rId = $image->getRelationId();
614618

615619
$style = $image->getStyle();
616620
$width = $style->getWidth();
617621
$height = $style->getHeight();
618622
$align = $style->getAlign();
623+
$marginTop = $style->getMarginTop();
624+
$marginLeft = $style->getMarginLeft();
625+
$wrappingStyle = $style->getWrappingStyle();
619626

620627
$objWriter->startElement('w:p');
621628

@@ -633,7 +640,37 @@ protected function _writeImage(PHPWord_Shared_XMLWriter $objWriter = null, $imag
633640

634641
$objWriter->startElement('v:shape');
635642
$objWriter->writeAttribute('type', '#_x0000_t75');
636-
$objWriter->writeAttribute('style', 'width:' . $width . 'px;height:' . $height . 'px');
643+
644+
$imgStyle = '';
645+
if (null !== $width) {
646+
$imgStyle .= 'width:' . $width . 'px;';
647+
}
648+
if (null !== $height) {
649+
$imgStyle .= 'height:' . $height . 'px;';
650+
}
651+
if (null !== $marginTop) {
652+
$imgStyle .= 'margin-top:' . $marginTop . 'in;';
653+
}
654+
if (null !== $marginLeft) {
655+
$imgStyle .= 'margin-left:' . $marginLeft . 'in;';
656+
}
657+
658+
switch ($wrappingStyle) {
659+
case PHPWord_Style_Image::WRAPPING_STYLE_BEHIND:
660+
$imgStyle .= 'position:absolute;z-index:-251658752;';
661+
break;
662+
case PHPWord_Style_Image::WRAPPING_STYLE_SQUARE:
663+
$imgStyle .= 'position:absolute;z-index:251659264;mso-position-horizontal:absolute;mso-position-vertical:absolute;';
664+
break;
665+
case PHPWord_Style_Image::WRAPPING_STYLE_TIGHT:
666+
$imgStyle .= 'position:absolute;z-index:251659264;mso-wrap-edited:f;mso-position-horizontal:absolute;mso-position-vertical:absolute';
667+
break;
668+
case PHPWord_Style_Image::WRAPPING_STYLE_INFRONT:
669+
$imgStyle .= 'position:absolute;zz-index:251659264;mso-position-horizontal:absolute;mso-position-vertical:absolute;';
670+
break;
671+
}
672+
673+
$objWriter->writeAttribute('style', $imgStyle);
637674

638675
$objWriter->startElement('v:imagedata');
639676
$objWriter->writeAttribute('r:id', 'rId' . $rId);
@@ -733,4 +770,4 @@ protected function _writeTitle(PHPWord_Shared_XMLWriter $objWriter = null, PHPWo
733770

734771
$objWriter->endElement();
735772
}
736-
}
773+
}

0 commit comments

Comments
 (0)