Added wrapping style to images

This commit is contained in:
Gabriel Bull 2013-12-11 14:51:35 -05:00
parent b6c33c8931
commit fa77c4f159
5 changed files with 115 additions and 15 deletions

View File

@ -27,23 +27,35 @@
class PHPWord_Autoloader class PHPWord_Autoloader
{ {
public static function Register() /**
* Register the autoloader
*
* @return void
*/
public static function register()
{ {
return spl_autoload_register(array('PHPWord_Autoloader', 'Load')); spl_autoload_register(array('PHPWord_Autoloader', 'load'));
} }
public static function Load($strObjectName) /**
* Autoloader
*
* @param string $strObjectName
* @return mixed
*/
public static function load($strObjectName)
{ {
if ((class_exists($strObjectName)) || (strpos($strObjectName, 'PHPWord') === false)) { if ((class_exists($strObjectName)) || (strpos($strObjectName, 'PHPWord') === false)) {
return false; return null;
} }
$strObjectFilePath = PHPWORD_BASE_PATH . str_replace('_', '/', $strObjectName) . '.php'; $strObjectFilePath = PHPWORD_BASE_PATH . str_replace('_', '/', $strObjectName) . '.php';
if ((file_exists($strObjectFilePath) === false) || (is_readable($strObjectFilePath) === false)) { if ((file_exists($strObjectFilePath) === false) || (is_readable($strObjectFilePath) === false)) {
return false; return null;
} }
require($strObjectFilePath); require_once $strObjectFilePath;
return true;
} }
} }

View File

@ -92,6 +92,10 @@ class PHPWord_Section_Image
} }
} }
if (isset($style['wrappingStyle'])) {
$this->_style->setWrappingStyle($style['wrappingStyle']);
}
if ($this->_style->getWidth() == null && $this->_style->getHeight() == null) { if ($this->_style->getWidth() == null && $this->_style->getHeight() == null) {
$imgData = getimagesize($this->_src); $imgData = getimagesize($this->_src);
$this->_style->setWidth($imgData[0]); $this->_style->setWidth($imgData[0]);
@ -173,5 +177,4 @@ class PHPWord_Section_Image
{ {
$this->_isWatermark = $pValue; $this->_isWatermark = $pValue;
} }
} }

View File

@ -30,7 +30,16 @@ if (!defined('DATE_W3C')) {
define('DATE_W3C', 'Y-m-d\TH:i:sP'); define('DATE_W3C', 'Y-m-d\TH:i:sP');
} }
/**
* Class PHPWord_Shared_XMLWriter
*
* @category PHPWord
* @package PHPWord_Section
* @copyright Copyright (c) 2011 PHPWord
* @method bool startElement(string $name)
* @method bool writeAttribute(string $name, string $value)
* @method bool endElement()
*/
class PHPWord_Shared_XMLWriter class PHPWord_Shared_XMLWriter
{ {
/** Temporary storage method */ /** Temporary storage method */
@ -145,4 +154,4 @@ class PHPWord_Shared_XMLWriter
return $this->text($text); return $this->text($text);
} }
} }

View File

@ -35,10 +35,16 @@
*/ */
class PHPWord_Style_Image class PHPWord_Style_Image
{ {
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';
private $_width; private $_width;
private $_height; private $_height;
private $_align; private $_align;
private $wrappingStyle;
/** /**
* Margin Top * Margin Top
@ -61,6 +67,7 @@ class PHPWord_Style_Image
$this->_align = null; $this->_align = null;
$this->_marginTop = null; $this->_marginTop = null;
$this->_marginLeft = null; $this->_marginLeft = null;
$this->setWrappingStyle(self::WRAPPING_STYLE_INLINE);
} }
public function setStyleValue($key, $value) public function setStyleValue($key, $value)
@ -112,6 +119,7 @@ class PHPWord_Style_Image
* Set Margin Top * Set Margin Top
* *
* @param int $pValue * @param int $pValue
* @return $this
*/ */
public function setMarginTop($pValue = null) public function setMarginTop($pValue = null)
{ {
@ -133,10 +141,41 @@ class PHPWord_Style_Image
* Set Margin Left * Set Margin Left
* *
* @param int $pValue * @param int $pValue
* @return $this
*/ */
public function setMarginLeft($pValue = null) public function setMarginLeft($pValue = null)
{ {
$this->_marginLeft = $pValue; $this->_marginLeft = $pValue;
return $this; return $this;
} }
}
/**
* @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');
break;
}
return $this;
}
/**
* @return string
*/
public function getWrappingStyle()
{
return $this->wrappingStyle;
}
}

View File

@ -608,7 +608,11 @@ class PHPWord_Writer_Word2007_Base extends PHPWord_Writer_Word2007_WriterPart
} }
} }
protected function _writeImage(PHPWord_Shared_XMLWriter $objWriter = null, $image) /**
* @param \PHPWord_Shared_XMLWriter $objWriter
* @param \PHPWord_Section_Image $image
*/
protected function _writeImage(PHPWord_Shared_XMLWriter $objWriter = null, PHPWord_Section_Image $image)
{ {
$rId = $image->getRelationId(); $rId = $image->getRelationId();
@ -616,6 +620,9 @@ class PHPWord_Writer_Word2007_Base extends PHPWord_Writer_Word2007_WriterPart
$width = $style->getWidth(); $width = $style->getWidth();
$height = $style->getHeight(); $height = $style->getHeight();
$align = $style->getAlign(); $align = $style->getAlign();
$marginTop = $style->getMarginTop();
$marginLeft = $style->getMarginLeft();
$wrappingStyle = $style->getWrappingStyle();
$objWriter->startElement('w:p'); $objWriter->startElement('w:p');
@ -633,7 +640,37 @@ class PHPWord_Writer_Word2007_Base extends PHPWord_Writer_Word2007_WriterPart
$objWriter->startElement('v:shape'); $objWriter->startElement('v:shape');
$objWriter->writeAttribute('type', '#_x0000_t75'); $objWriter->writeAttribute('type', '#_x0000_t75');
$objWriter->writeAttribute('style', 'width:' . $width . 'px;height:' . $height . 'px');
$imgStyle = '';
if (null !== $width) {
$imgStyle .= 'width:' . $width . 'px;';
}
if (null !== $height) {
$imgStyle .= 'height:' . $height . 'px;';
}
if (null !== $marginTop) {
$imgStyle .= 'margin-top:' . $marginTop . 'in;';
}
if (null !== $marginLeft) {
$imgStyle .= 'margin-left:' . $marginLeft . 'in;';
}
switch ($wrappingStyle) {
case PHPWord_Style_Image::WRAPPING_STYLE_BEHIND:
$imgStyle .= 'position:absolute;z-index:-251658752;';
break;
case PHPWord_Style_Image::WRAPPING_STYLE_SQUARE:
$imgStyle .= 'position:absolute;z-index:251659264;mso-position-horizontal:absolute;mso-position-vertical:absolute;';
break;
case PHPWord_Style_Image::WRAPPING_STYLE_TIGHT:
$imgStyle .= 'position:absolute;z-index:251659264;mso-wrap-edited:f;mso-position-horizontal:absolute;mso-position-vertical:absolute';
break;
case PHPWord_Style_Image::WRAPPING_STYLE_INFRONT:
$imgStyle .= 'position:absolute;zz-index:251659264;mso-position-horizontal:absolute;mso-position-vertical:absolute;';
break;
}
$objWriter->writeAttribute('style', $imgStyle);
$objWriter->startElement('v:imagedata'); $objWriter->startElement('v:imagedata');
$objWriter->writeAttribute('r:id', 'rId' . $rId); $objWriter->writeAttribute('r:id', 'rId' . $rId);
@ -733,4 +770,4 @@ class PHPWord_Writer_Word2007_Base extends PHPWord_Writer_Word2007_WriterPart
$objWriter->endElement(); $objWriter->endElement();
} }
} }