diff --git a/samples/Sample_13_Images.php b/samples/Sample_13_Images.php index 4ef325b8..8acceb73 100644 --- a/samples/Sample_13_Images.php +++ b/samples/Sample_13_Images.php @@ -10,15 +10,26 @@ $section = $phpWord->addSection(); $section->addText('Local image without any styles:'); $section->addImage('resources/_mars.jpg'); $section->addTextBreak(2); -// + $section->addText('Local image with styles:'); $section->addImage('resources/_earth.jpg', array('width' => 210, 'height' => 210, 'align' => 'center')); $section->addTextBreak(2); +// Remote image $source = 'http://php.net/images/logos/php-med-trans-light.gif'; $section->addText("Remote image from: {$source}"); $section->addImage($source); -// End code + +//Wrapping style +$text = str_repeat('Hello World! ', 15); +$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, + 'width' => 80, 'height' => 80, 'wrappingStyle' => $wrappingStyle)); + $section->addText($text); +} // Save file $name = basename(__FILE__, '.php'); diff --git a/src/PhpWord/Writer/Word2007/Base.php b/src/PhpWord/Writer/Word2007/Base.php index b74211a5..de6eb3d8 100644 --- a/src/PhpWord/Writer/Word2007/Base.php +++ b/src/PhpWord/Writer/Word2007/Base.php @@ -427,25 +427,22 @@ class Base extends AbstractWriterPart $marginTop = $style->getMarginTop(); $marginLeft = $style->getMarginLeft(); $wrappingStyle = $style->getWrappingStyle(); + $w10wrapType = null; if (!$withoutP) { $xmlWriter->startElement('w:p'); - if (!is_null($align)) { $xmlWriter->startElement('w:pPr'); $xmlWriter->startElement('w:jc'); $xmlWriter->writeAttribute('w:val', $align); - $xmlWriter->endElement(); - $xmlWriter->endElement(); + $xmlWriter->endElement(); // w:jc + $xmlWriter->endElement(); // w:pPr } } $xmlWriter->startElement('w:r'); - $xmlWriter->startElement('w:pict'); - $xmlWriter->startElement('v:shape'); $xmlWriter->writeAttribute('type', '#_x0000_t75'); - $imgStyle = ''; if (null !== $width) { $imgStyle .= 'width:' . $width . 'px;'; @@ -459,33 +456,38 @@ class Base extends AbstractWriterPart if (null !== $marginLeft) { $imgStyle .= 'margin-left:' . $marginLeft . 'in;'; } - switch ($wrappingStyle) { case ImageStyle::WRAPPING_STYLE_BEHIND: $imgStyle .= 'position:absolute;z-index:-251658752;'; break; - case ImageStyle::WRAPPING_STYLE_SQUARE: + case ImageStyle::WRAPPING_STYLE_INFRONT: $imgStyle .= 'position:absolute;z-index:251659264;mso-position-horizontal:absolute;mso-position-vertical:absolute;'; break; - case ImageStyle::WRAPPING_STYLE_TIGHT: - $imgStyle .= 'position:absolute;z-index:251659264;mso-wrap-edited:f;mso-position-horizontal:absolute;mso-position-vertical:absolute'; + case ImageStyle::WRAPPING_STYLE_SQUARE: + $imgStyle .= 'position:absolute;z-index:251659264;mso-position-horizontal:absolute;mso-position-vertical:absolute;'; + $w10wrapType = 'square'; break; - case ImageStyle::WRAPPING_STYLE_INFRONT: - $imgStyle .= 'position:absolute;zz-index:251659264;mso-position-horizontal:absolute;mso-position-vertical:absolute;'; + case ImageStyle::WRAPPING_STYLE_TIGHT: + $imgStyle .= 'position:absolute;z-index:251659264;mso-position-horizontal:absolute;mso-position-vertical:absolute;'; + $w10wrapType = 'tight'; break; } - $xmlWriter->writeAttribute('style', $imgStyle); $xmlWriter->startElement('v:imagedata'); $xmlWriter->writeAttribute('r:id', 'rId' . $rId); $xmlWriter->writeAttribute('o:title', ''); - $xmlWriter->endElement(); - $xmlWriter->endElement(); + $xmlWriter->endElement(); // v:imagedata - $xmlWriter->endElement(); + if (!is_null($w10wrapType)) { + $xmlWriter->startElement('w10:wrap'); + $xmlWriter->writeAttribute('type', $w10wrapType); + $xmlWriter->endElement(); // w10:wrap + } - $xmlWriter->endElement(); + $xmlWriter->endElement(); // v:shape + $xmlWriter->endElement(); // w:pict + $xmlWriter->endElement(); // w:r if (!$withoutP) { $xmlWriter->endElement(); // w:p diff --git a/tests/PhpWord/Tests/Writer/Word2007/BaseTest.php b/tests/PhpWord/Tests/Writer/Word2007/BaseTest.php index d9d1b3c5..904f45bd 100644 --- a/tests/PhpWord/Tests/Writer/Word2007/BaseTest.php +++ b/tests/PhpWord/Tests/Writer/Word2007/BaseTest.php @@ -28,7 +28,7 @@ class BaseTest extends \PHPUnit_Framework_TestCase } /** - * covers ::_writeText + * Test write text element */ public function testWriteText() { @@ -49,7 +49,7 @@ class BaseTest extends \PHPUnit_Framework_TestCase } /** - * covers ::_writeTextRun + * Test write textrun element */ public function testWriteTextRun() { @@ -74,7 +74,7 @@ class BaseTest extends \PHPUnit_Framework_TestCase } /** - * covers ::_writeLink + * Test write link element */ public function testWriteLink() { @@ -97,7 +97,7 @@ class BaseTest extends \PHPUnit_Framework_TestCase } /** - * covers ::_writePreserveText + * Test write preserve text element */ public function testWritePreserveText() { @@ -121,7 +121,7 @@ class BaseTest extends \PHPUnit_Framework_TestCase } /** - * covers ::_writeTextBreak + * Test write text break */ public function testWriteTextBreak() { @@ -146,30 +146,93 @@ class BaseTest extends \PHPUnit_Framework_TestCase } /** - * covers ::_writeParagraphStyle + * covers ::_writeImage */ - public function testWriteParagraphStyleAlign() + public function testWriteImage() { $phpWord = new PhpWord(); + $styles = array('align' => 'left', 'width' => 40, 'height' => 40, 'marginTop' => -1, 'marginLeft' => -1); + $wraps = array('inline', 'behind', 'infront', 'square', 'tight'); $section = $phpWord->addSection(); - - $section->addText('This is my text', null, array('align' => 'right')); + foreach ($wraps as $wrap) { + $styles['wrappingStyle'] = $wrap; + $section->addImage(__DIR__ . "/../../_files/images/earth.jpg", $styles); + } $doc = TestHelperDOCX::getDocument($phpWord); - $element = $doc->getElement('/w:document/w:body/w:p/w:pPr/w:jc'); - $this->assertEquals('right', $element->getAttribute('w:val')); + // behind + $element = $doc->getElement('/w:document/w:body/w:p[2]/w:r/w:pict/v:shape'); + $style = $element->getAttribute('style'); + $this->assertRegExp('/z\-index:\-[0-9]*/', $style); + + // square + $element = $doc->getElement('/w:document/w:body/w:p[4]/w:r/w:pict/v:shape/w10:wrap'); + $this->assertEquals('square', $element->getAttribute('type')); + } + + /** + * covers ::_writeWatermark + */ + public function testWriteWatermark() + { + $imageSrc = __DIR__ . "/../../_files/images/earth.jpg"; + + $phpWord = new PhpWord(); + $section = $phpWord->addSection(); + $header = $section->addHeader(); + $header->addWatermark($imageSrc); + $doc = TestHelperDOCX::getDocument($phpWord); + + $element = $doc->getElement("/w:document/w:body/w:sectPr/w:headerReference"); + $this->assertStringStartsWith("rId", $element->getAttribute('r:id')); + } + + /** + * covers ::_writeTitle + */ + public function testWriteTitle() + { + $phpWord = new PhpWord(); + $phpWord->addTitleStyle(1, array('bold' => true), array('spaceAfter' => 240)); + $phpWord->addSection()->addTitle('Test', 1); + $doc = TestHelperDOCX::getDocument($phpWord); + + $element = "/w:document/w:body/w:p/w:pPr/w:pStyle"; + $this->assertEquals('Heading1', $doc->getElementAttribute($element, 'w:val')); + $element = "/w:document/w:body/w:p/w:r/w:fldChar"; + $this->assertEquals('end', $doc->getElementAttribute($element, 'w:fldCharType')); + } + + /** + * covers ::_writeCheckbox + */ + public function testWriteCheckbox() + { + $rStyle = 'rStyle'; + $pStyle = 'pStyle'; + + $phpWord = new PhpWord(); + $phpWord->addFontStyle($rStyle, array('bold' => true)); + $phpWord->addParagraphStyle($pStyle, array('hanging' => 120, 'indent' => 120)); + $section = $phpWord->addSection(); + $section->addCheckbox('Check1', 'Test', $rStyle, $pStyle); + $doc = TestHelperDOCX::getDocument($phpWord); + + $element = '/w:document/w:body/w:p/w:r/w:fldChar/w:ffData/w:name'; + $this->assertEquals('Check1', $doc->getElementAttribute($element, 'w:val')); } /** * covers ::_writeParagraphStyle */ - public function testWriteParagraphStylePagination() + public function testWriteParagraphStyle() { // Create the doc $phpWord = new PhpWord(); $section = $phpWord->addSection(); $attributes = array( + 'align' => 'right', 'widowControl' => false, 'keepNext' => true, 'keepLines' => true, @@ -184,10 +247,13 @@ class BaseTest extends \PHPUnit_Framework_TestCase $i = 0; foreach ($attributes as $key => $value) { $i++; - $path = "/w:document/w:body/w:p[{$i}]/w:pPr/w:{$key}"; + $nodeName = ($key == 'align') ? 'jc' : $key; + $path = "/w:document/w:body/w:p[{$i}]/w:pPr/w:{$nodeName}"; + if ($key != 'align') { + $value = $value ? 1 : 0; + } $element = $doc->getElement($path); - $expected = $value ? 1 : 0; - $this->assertEquals($expected, $element->getAttribute('w:val')); + $this->assertEquals($value, $element->getAttribute('w:val')); } } @@ -316,81 +382,4 @@ class BaseTest extends \PHPUnit_Framework_TestCase $this->assertEquals(5, $element->getAttribute('w:val')); } - - /** - * covers ::_writeImage - */ - public function testWriteImagePosition() - { - $phpWord = new PhpWord(); - $section = $phpWord->addSection(); - $section->addImage( - __DIR__ . "/../../_files/images/earth.jpg", - array( - 'marginTop' => -1, - 'marginLeft' => -1, - 'wrappingStyle' => 'behind' - ) - ); - - $doc = TestHelperDOCX::getDocument($phpWord); - $element = $doc->getElement('/w:document/w:body/w:p/w:r/w:pict/v:shape'); - - $style = $element->getAttribute('style'); - - $this->assertRegExp('/z\-index:\-[0-9]*/', $style); - $this->assertRegExp('/position:absolute;/', $style); - } - - /** - * covers ::_writeWatermark - */ - public function testWriteWatermark() - { - $imageSrc = __DIR__ . "/../../_files/images/earth.jpg"; - - $phpWord = new PhpWord(); - $section = $phpWord->addSection(); - $header = $section->addHeader(); - $header->addWatermark($imageSrc); - $doc = TestHelperDOCX::getDocument($phpWord); - - $element = $doc->getElement("/w:document/w:body/w:sectPr/w:headerReference"); - $this->assertStringStartsWith("rId", $element->getAttribute('r:id')); - } - - /** - * covers ::_writeTitle - */ - public function testWriteTitle() - { - $phpWord = new PhpWord(); - $phpWord->addTitleStyle(1, array('bold' => true), array('spaceAfter' => 240)); - $phpWord->addSection()->addTitle('Test', 1); - $doc = TestHelperDOCX::getDocument($phpWord); - - $element = "/w:document/w:body/w:p/w:pPr/w:pStyle"; - $this->assertEquals('Heading1', $doc->getElementAttribute($element, 'w:val')); - $element = "/w:document/w:body/w:p/w:r/w:fldChar"; - $this->assertEquals('end', $doc->getElementAttribute($element, 'w:fldCharType')); - } - - /** - * covers ::_writeCheckbox - */ - public function testWriteCheckbox() - { - $rStyle = 'rStyle'; - $pStyle = 'pStyle'; - - $phpWord = new PhpWord(); - $phpWord->addFontStyle($rStyle, array('bold' => true)); - $phpWord->addParagraphStyle($pStyle, array('hanging' => 120, 'indent' => 120)); - $section = $phpWord->addSection(); - $section->addCheckbox('Check1', 'Test', $rStyle, $pStyle); - $doc = TestHelperDOCX::getDocument($phpWord); - - $element = '/w:document/w:body/w:p/w:r/w:fldChar/w:ffData/w:name'; - $this->assertEquals('Check1', $doc->getElementAttribute($element, 'w:val')); - } }