Skip to content

Commit 89f4288

Browse files
committed
Merge branch 'master' of https://github.com/brunocasado/PHPWord into 'develop'
2 parents 9bb5655 + ad58e72 commit 89f4288

File tree

3 files changed

+111
-109
lines changed

3 files changed

+111
-109
lines changed

samples/Sample_13_Images.php

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,26 @@
1010
$section->addText('Local image without any styles:');
1111
$section->addImage('resources/_mars.jpg');
1212
$section->addTextBreak(2);
13-
//
13+
1414
$section->addText('Local image with styles:');
1515
$section->addImage('resources/_earth.jpg', array('width' => 210, 'height' => 210, 'align' => 'center'));
1616
$section->addTextBreak(2);
1717

18+
// Remote image
1819
$source = 'http://php.net/images/logos/php-med-trans-light.gif';
1920
$section->addText("Remote image from: {$source}");
2021
$section->addImage($source);
21-
// End code
22+
23+
//Wrapping style
24+
$text = str_repeat('Hello World! ', 15);
25+
$wrappingStyles = array('inline', 'behind', 'infront', 'square', 'tight');
26+
foreach ($wrappingStyles as $wrappingStyle) {
27+
$section->addTextBreak(5);
28+
$section->addText('Wrapping style ' . $wrappingStyle);
29+
$section->addImage('resources/_earth.jpg', array('marginTop' => -1, 'marginLeft' => 1,
30+
'width' => 80, 'height' => 80, 'wrappingStyle' => $wrappingStyle));
31+
$section->addText($text);
32+
}
2233

2334
// Save file
2435
$name = basename(__FILE__, '.php');

src/PhpWord/Writer/Word2007/Base.php

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -427,25 +427,22 @@ protected function writeImage(XMLWriter $xmlWriter, Image $image, $withoutP = fa
427427
$marginTop = $style->getMarginTop();
428428
$marginLeft = $style->getMarginLeft();
429429
$wrappingStyle = $style->getWrappingStyle();
430+
$w10wrapType = null;
430431

431432
if (!$withoutP) {
432433
$xmlWriter->startElement('w:p');
433-
434434
if (!is_null($align)) {
435435
$xmlWriter->startElement('w:pPr');
436436
$xmlWriter->startElement('w:jc');
437437
$xmlWriter->writeAttribute('w:val', $align);
438-
$xmlWriter->endElement();
439-
$xmlWriter->endElement();
438+
$xmlWriter->endElement(); // w:jc
439+
$xmlWriter->endElement(); // w:pPr
440440
}
441441
}
442442
$xmlWriter->startElement('w:r');
443-
444443
$xmlWriter->startElement('w:pict');
445-
446444
$xmlWriter->startElement('v:shape');
447445
$xmlWriter->writeAttribute('type', '#_x0000_t75');
448-
449446
$imgStyle = '';
450447
if (null !== $width) {
451448
$imgStyle .= 'width:' . $width . 'px;';
@@ -459,33 +456,38 @@ protected function writeImage(XMLWriter $xmlWriter, Image $image, $withoutP = fa
459456
if (null !== $marginLeft) {
460457
$imgStyle .= 'margin-left:' . $marginLeft . 'in;';
461458
}
462-
463459
switch ($wrappingStyle) {
464460
case ImageStyle::WRAPPING_STYLE_BEHIND:
465461
$imgStyle .= 'position:absolute;z-index:-251658752;';
466462
break;
463+
case ImageStyle::WRAPPING_STYLE_INFRONT:
464+
$imgStyle .= 'position:absolute;z-index:251659264;mso-position-horizontal:absolute;mso-position-vertical:absolute;';
465+
break;
467466
case ImageStyle::WRAPPING_STYLE_SQUARE:
468467
$imgStyle .= 'position:absolute;z-index:251659264;mso-position-horizontal:absolute;mso-position-vertical:absolute;';
468+
$w10wrapType = 'square';
469469
break;
470470
case ImageStyle::WRAPPING_STYLE_TIGHT:
471-
$imgStyle .= 'position:absolute;z-index:251659264;mso-wrap-edited:f;mso-position-horizontal:absolute;mso-position-vertical:absolute';
472-
break;
473-
case ImageStyle::WRAPPING_STYLE_INFRONT:
474-
$imgStyle .= 'position:absolute;zz-index:251659264;mso-position-horizontal:absolute;mso-position-vertical:absolute;';
471+
$imgStyle .= 'position:absolute;z-index:251659264;mso-position-horizontal:absolute;mso-position-vertical:absolute;';
472+
$w10wrapType = 'tight';
475473
break;
476474
}
477-
478475
$xmlWriter->writeAttribute('style', $imgStyle);
479476

480477
$xmlWriter->startElement('v:imagedata');
481478
$xmlWriter->writeAttribute('r:id', 'rId' . $rId);
482479
$xmlWriter->writeAttribute('o:title', '');
483-
$xmlWriter->endElement();
484-
$xmlWriter->endElement();
480+
$xmlWriter->endElement(); // v:imagedata
485481

486-
$xmlWriter->endElement();
482+
if (!is_null($w10wrapType)) {
483+
$xmlWriter->startElement('w10:wrap');
484+
$xmlWriter->writeAttribute('type', $w10wrapType);
485+
$xmlWriter->endElement(); // w10:wrap
486+
}
487487

488-
$xmlWriter->endElement();
488+
$xmlWriter->endElement(); // v:shape
489+
$xmlWriter->endElement(); // w:pict
490+
$xmlWriter->endElement(); // w:r
489491

490492
if (!$withoutP) {
491493
$xmlWriter->endElement(); // w:p

tests/PhpWord/Tests/Writer/Word2007/BaseTest.php

Lines changed: 80 additions & 91 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ public function tearDown()
2828
}
2929

3030
/**
31-
* covers ::_writeText
31+
* Test write text element
3232
*/
3333
public function testWriteText()
3434
{
@@ -49,7 +49,7 @@ public function testWriteText()
4949
}
5050

5151
/**
52-
* covers ::_writeTextRun
52+
* Test write textrun element
5353
*/
5454
public function testWriteTextRun()
5555
{
@@ -74,7 +74,7 @@ public function testWriteTextRun()
7474
}
7575

7676
/**
77-
* covers ::_writeLink
77+
* Test write link element
7878
*/
7979
public function testWriteLink()
8080
{
@@ -97,7 +97,7 @@ public function testWriteLink()
9797
}
9898

9999
/**
100-
* covers ::_writePreserveText
100+
* Test write preserve text element
101101
*/
102102
public function testWritePreserveText()
103103
{
@@ -121,7 +121,7 @@ public function testWritePreserveText()
121121
}
122122

123123
/**
124-
* covers ::_writeTextBreak
124+
* Test write text break
125125
*/
126126
public function testWriteTextBreak()
127127
{
@@ -146,30 +146,93 @@ public function testWriteTextBreak()
146146
}
147147

148148
/**
149-
* covers ::_writeParagraphStyle
149+
* covers ::_writeImage
150150
*/
151-
public function testWriteParagraphStyleAlign()
151+
public function testWriteImage()
152152
{
153153
$phpWord = new PhpWord();
154+
$styles = array('align' => 'left', 'width' => 40, 'height' => 40, 'marginTop' => -1, 'marginLeft' => -1);
155+
$wraps = array('inline', 'behind', 'infront', 'square', 'tight');
154156
$section = $phpWord->addSection();
157+
foreach ($wraps as $wrap) {
158+
$styles['wrappingStyle'] = $wrap;
159+
$section->addImage(__DIR__ . "/../../_files/images/earth.jpg", $styles);
160+
}
161+
162+
$doc = TestHelperDOCX::getDocument($phpWord);
163+
164+
// behind
165+
$element = $doc->getElement('/w:document/w:body/w:p[2]/w:r/w:pict/v:shape');
166+
$style = $element->getAttribute('style');
167+
$this->assertRegExp('/z\-index:\-[0-9]*/', $style);
168+
169+
// square
170+
$element = $doc->getElement('/w:document/w:body/w:p[4]/w:r/w:pict/v:shape/w10:wrap');
171+
$this->assertEquals('square', $element->getAttribute('type'));
172+
}
173+
174+
/**
175+
* covers ::_writeWatermark
176+
*/
177+
public function testWriteWatermark()
178+
{
179+
$imageSrc = __DIR__ . "/../../_files/images/earth.jpg";
180+
181+
$phpWord = new PhpWord();
182+
$section = $phpWord->addSection();
183+
$header = $section->addHeader();
184+
$header->addWatermark($imageSrc);
185+
$doc = TestHelperDOCX::getDocument($phpWord);
186+
187+
$element = $doc->getElement("/w:document/w:body/w:sectPr/w:headerReference");
188+
$this->assertStringStartsWith("rId", $element->getAttribute('r:id'));
189+
}
190+
191+
/**
192+
* covers ::_writeTitle
193+
*/
194+
public function testWriteTitle()
195+
{
196+
$phpWord = new PhpWord();
197+
$phpWord->addTitleStyle(1, array('bold' => true), array('spaceAfter' => 240));
198+
$phpWord->addSection()->addTitle('Test', 1);
199+
$doc = TestHelperDOCX::getDocument($phpWord);
155200

156-
$section->addText('This is my text', null, array('align' => 'right'));
201+
$element = "/w:document/w:body/w:p/w:pPr/w:pStyle";
202+
$this->assertEquals('Heading1', $doc->getElementAttribute($element, 'w:val'));
203+
$element = "/w:document/w:body/w:p/w:r/w:fldChar";
204+
$this->assertEquals('end', $doc->getElementAttribute($element, 'w:fldCharType'));
205+
}
157206

207+
/**
208+
* covers ::_writeCheckbox
209+
*/
210+
public function testWriteCheckbox()
211+
{
212+
$rStyle = 'rStyle';
213+
$pStyle = 'pStyle';
214+
215+
$phpWord = new PhpWord();
216+
$phpWord->addFontStyle($rStyle, array('bold' => true));
217+
$phpWord->addParagraphStyle($pStyle, array('hanging' => 120, 'indent' => 120));
218+
$section = $phpWord->addSection();
219+
$section->addCheckbox('Check1', 'Test', $rStyle, $pStyle);
158220
$doc = TestHelperDOCX::getDocument($phpWord);
159-
$element = $doc->getElement('/w:document/w:body/w:p/w:pPr/w:jc');
160221

161-
$this->assertEquals('right', $element->getAttribute('w:val'));
222+
$element = '/w:document/w:body/w:p/w:r/w:fldChar/w:ffData/w:name';
223+
$this->assertEquals('Check1', $doc->getElementAttribute($element, 'w:val'));
162224
}
163225

164226
/**
165227
* covers ::_writeParagraphStyle
166228
*/
167-
public function testWriteParagraphStylePagination()
229+
public function testWriteParagraphStyle()
168230
{
169231
// Create the doc
170232
$phpWord = new PhpWord();
171233
$section = $phpWord->addSection();
172234
$attributes = array(
235+
'align' => 'right',
173236
'widowControl' => false,
174237
'keepNext' => true,
175238
'keepLines' => true,
@@ -184,10 +247,13 @@ public function testWriteParagraphStylePagination()
184247
$i = 0;
185248
foreach ($attributes as $key => $value) {
186249
$i++;
187-
$path = "/w:document/w:body/w:p[{$i}]/w:pPr/w:{$key}";
250+
$nodeName = ($key == 'align') ? 'jc' : $key;
251+
$path = "/w:document/w:body/w:p[{$i}]/w:pPr/w:{$nodeName}";
252+
if ($key != 'align') {
253+
$value = $value ? 1 : 0;
254+
}
188255
$element = $doc->getElement($path);
189-
$expected = $value ? 1 : 0;
190-
$this->assertEquals($expected, $element->getAttribute('w:val'));
256+
$this->assertEquals($value, $element->getAttribute('w:val'));
191257
}
192258
}
193259

@@ -316,81 +382,4 @@ public function testWriteCellStyleCellGridSpan()
316382

317383
$this->assertEquals(5, $element->getAttribute('w:val'));
318384
}
319-
320-
/**
321-
* covers ::_writeImage
322-
*/
323-
public function testWriteImagePosition()
324-
{
325-
$phpWord = new PhpWord();
326-
$section = $phpWord->addSection();
327-
$section->addImage(
328-
__DIR__ . "/../../_files/images/earth.jpg",
329-
array(
330-
'marginTop' => -1,
331-
'marginLeft' => -1,
332-
'wrappingStyle' => 'behind'
333-
)
334-
);
335-
336-
$doc = TestHelperDOCX::getDocument($phpWord);
337-
$element = $doc->getElement('/w:document/w:body/w:p/w:r/w:pict/v:shape');
338-
339-
$style = $element->getAttribute('style');
340-
341-
$this->assertRegExp('/z\-index:\-[0-9]*/', $style);
342-
$this->assertRegExp('/position:absolute;/', $style);
343-
}
344-
345-
/**
346-
* covers ::_writeWatermark
347-
*/
348-
public function testWriteWatermark()
349-
{
350-
$imageSrc = __DIR__ . "/../../_files/images/earth.jpg";
351-
352-
$phpWord = new PhpWord();
353-
$section = $phpWord->addSection();
354-
$header = $section->addHeader();
355-
$header->addWatermark($imageSrc);
356-
$doc = TestHelperDOCX::getDocument($phpWord);
357-
358-
$element = $doc->getElement("/w:document/w:body/w:sectPr/w:headerReference");
359-
$this->assertStringStartsWith("rId", $element->getAttribute('r:id'));
360-
}
361-
362-
/**
363-
* covers ::_writeTitle
364-
*/
365-
public function testWriteTitle()
366-
{
367-
$phpWord = new PhpWord();
368-
$phpWord->addTitleStyle(1, array('bold' => true), array('spaceAfter' => 240));
369-
$phpWord->addSection()->addTitle('Test', 1);
370-
$doc = TestHelperDOCX::getDocument($phpWord);
371-
372-
$element = "/w:document/w:body/w:p/w:pPr/w:pStyle";
373-
$this->assertEquals('Heading1', $doc->getElementAttribute($element, 'w:val'));
374-
$element = "/w:document/w:body/w:p/w:r/w:fldChar";
375-
$this->assertEquals('end', $doc->getElementAttribute($element, 'w:fldCharType'));
376-
}
377-
378-
/**
379-
* covers ::_writeCheckbox
380-
*/
381-
public function testWriteCheckbox()
382-
{
383-
$rStyle = 'rStyle';
384-
$pStyle = 'pStyle';
385-
386-
$phpWord = new PhpWord();
387-
$phpWord->addFontStyle($rStyle, array('bold' => true));
388-
$phpWord->addParagraphStyle($pStyle, array('hanging' => 120, 'indent' => 120));
389-
$section = $phpWord->addSection();
390-
$section->addCheckbox('Check1', 'Test', $rStyle, $pStyle);
391-
$doc = TestHelperDOCX::getDocument($phpWord);
392-
393-
$element = '/w:document/w:body/w:p/w:r/w:fldChar/w:ffData/w:name';
394-
$this->assertEquals('Check1', $doc->getElementAttribute($element, 'w:val'));
395-
}
396385
}

0 commit comments

Comments
 (0)