Skip to content

Allow adding MemoryImage using URL #122

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Mar 13, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion Classes/PHPWord/Media.php
Original file line number Diff line number Diff line change
Expand Up @@ -86,11 +86,14 @@ public static function addSectionMediaElement($src, $type, PHPWord_Section_Memor
$file = null;
if ($type === 'image') {
$cImg++;
//Detect if it's a memory image first by php ext and second by regex
$isMemImage = false;
if (stripos(strrev($src), strrev('.php')) === 0) {
$isMemImage = true;
}

if (!$isMemImage) {
$isMemImage = (filter_var($src, FILTER_VALIDATE_URL) !== false);
}
$extension = '';
if ($isMemImage) {
$extension = $memoryImage->getImageExtension();
Expand Down
4 changes: 2 additions & 2 deletions Classes/PHPWord/Writer/Word2007/Base.php
Original file line number Diff line number Diff line change
Expand Up @@ -751,9 +751,9 @@ protected function _writeCellStyle(PHPWord_Shared_XMLWriter $objWriter = null, P

/**
* @param \PHPWord_Shared_XMLWriter $objWriter
* @param \PHPWord_Section_Image $image
* @param \PHPWord_Section_Image|\PHPWord_Section_MemoryImage $image
*/
protected function _writeImage(PHPWord_Shared_XMLWriter $objWriter = null, PHPWord_Section_Image $image, $withoutP = false)
protected function _writeImage(PHPWord_Shared_XMLWriter $objWriter = null, $image, $withoutP = false)
{
$rId = $image->getRelationId();

Expand Down
7 changes: 7 additions & 0 deletions Classes/PHPWord/Writer/Word2007/ContentTypes.php
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,13 @@ public function writeContentTypes($_imageTypes, $_objectTypes, $_cHdrs, $_cFtrs)
'application/vnd.openxmlformats-officedocument.wordprocessingml.fontTable+xml'
);

// Footnotes
$this->_writeOverrideContentType(
$objWriter,
'/word/footnotes.xml',
'application/vnd.openxmlformats-officedocument.wordprocessingml.footnotes+xml'
);

for ($i = 1; $i <= $_cHdrs; $i++) {
$this->_writeOverrideContentType(
$objWriter,
Expand Down
26 changes: 26 additions & 0 deletions Tests/PHPWord/SettingsTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php
namespace PHPWord\Tests;

use PHPWord_Settings;

/**
* Class TOCTest
*
* @package PHPWord\Tests
* @covers PHPWord_Settings
* @runTestsInSeparateProcesses
*/
class SettingsTest extends \PHPUnit_Framework_TestCase
{
/**
* @covers PHPWord_Settings::setCompatibility
* @covers PHPWord_Settings::getCompatibility
*/
public function testGetSetCompatibility()
{
$this->assertTrue(PHPWord_Settings::getCompatibility());
$this->assertTrue(PHPWord_Settings::setCompatibility(false));
$this->assertFalse(PHPWord_Settings::getCompatibility());
$this->assertFalse(PHPWord_Settings::setCompatibility('Non boolean'));
}
}
3 changes: 1 addition & 2 deletions Tests/PHPWord/StyleTest.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
<?php
namespace PHPWord\Tests;

use PHPUnit_Framework_TestCase;
use PHPWord_Style;

/**
Expand All @@ -11,7 +10,7 @@
* @covers PHPWord_Style
* @runTestsInSeparateProcesses
*/
class StyleTest extends PHPUnit_Framework_TestCase
class StyleTest extends \PHPUnit_Framework_TestCase
{
/**
* @covers PHPWord_Style::addParagraphStyle
Expand Down
41 changes: 32 additions & 9 deletions Tests/PHPWord/Writer/Word2007/BaseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ public function tearDown()
TestHelperDOCX::clear();
}

/**
* covers ::_writeText
*/
public function testWriteText()
{
$rStyle = 'rStyle';
Expand All @@ -40,7 +43,7 @@ public function testWriteText()
}

/**
* Write text run
* covers ::_writeTextRun
*/
public function testWriteTextRun()
{
Expand All @@ -67,7 +70,7 @@ public function testWriteTextRun()
}

/**
* Write link
* covers ::_writeLink
*/
public function testWriteLink()
{
Expand All @@ -84,7 +87,7 @@ public function testWriteLink()
}

/**
* Write preserve text
* covers ::_writePreserveText
*/
public function testWritePreserveText()
{
Expand All @@ -102,7 +105,7 @@ public function testWritePreserveText()
}

/**
* Write paragraph style: Alignment
* covers ::_writeParagraphStyle
*/
public function testWriteParagraphStyleAlign()
{
Expand All @@ -118,7 +121,7 @@ public function testWriteParagraphStyleAlign()
}

/**
* Write paragraph style: Pagination
* covers ::_writeParagraphStyle
*/
public function testWriteParagraphStylePagination()
{
Expand Down Expand Up @@ -180,7 +183,7 @@ public function testWriteFontStyle()
}

/**
* Write table
* covers ::_writeTableStyle
*/
public function testWriteTableStyle()
{
Expand Down Expand Up @@ -238,7 +241,7 @@ public function testWriteTableStyle()
}

/**
* Write cell style
* covers ::_writeCellStyle
*/
public function testWriteCellStyleCellGridSpan()
{
Expand All @@ -265,7 +268,7 @@ public function testWriteCellStyleCellGridSpan()
}

/**
* Write image
* covers ::_writeImage
*/
public function testWriteImagePosition()
{
Expand All @@ -290,7 +293,27 @@ public function testWriteImagePosition()
}

/**
* Write title
* covers ::_writeWatermark
*/
public function testWriteWatermark()
{
$imageSrc = join(
DIRECTORY_SEPARATOR,
array(PHPWORD_TESTS_DIR_ROOT, '_files', 'images', 'earth.jpg')
);

$PHPWord = new PHPWord();
$section = $PHPWord->createSection();
$header = $section->createHeader();
$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()
{
Expand Down
52 changes: 52 additions & 0 deletions Tests/PHPWord/Writer/Word2007/DocumentTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,56 @@ public function testWriteEndSectionPageNumbering()

$this->assertEquals(2, $element->getAttribute('w:start'));
}

/**
* covers ::_writeTOC
* covers ::_writePageBreak
* covers ::_writeListItem
* covers ::_writeTitle
* covers ::_writeObject
*/
public function testElements()
{
$objectSrc = join(
DIRECTORY_SEPARATOR,
array(PHPWORD_TESTS_DIR_ROOT, '_files', 'documents', 'sheet.xls')
);

$PHPWord = new PHPWord();
$PHPWord->addTitleStyle(1, array('color' => '333333', 'bold'=>true));
$PHPWord->addTitleStyle(2, array('color'=>'666666'));
$section = $PHPWord->createSection();
$section->addTOC();
$section->addPageBreak();
$section->addTitle('Title 1', 1);
$section->addListItem('List Item 1', 0);
$section->addListItem('List Item 2', 0);
$section->addListItem('List Item 3', 0);
$section = $PHPWord->createSection();
$section->addTitle('Title 2', 2);
$section->addObject($objectSrc);
$doc = TestHelperDOCX::getDocument($PHPWord);

// TOC
$element = $doc->getElement('/w:document/w:body/w:p[1]/w:pPr/w:tabs/w:tab');
$this->assertEquals('right', $element->getAttribute('w:val'));
$this->assertEquals('dot', $element->getAttribute('w:leader'));
$this->assertEquals(9062, $element->getAttribute('w:pos'));

// Page break
$element = $doc->getElement('/w:document/w:body/w:p[4]/w:r/w:br');
$this->assertEquals('page', $element->getAttribute('w:type'));

// Title
$element = $doc->getElement('/w:document/w:body/w:p[5]/w:pPr/w:pStyle');
$this->assertEquals('Heading1', $element->getAttribute('w:val'));

// List item
$element = $doc->getElement('/w:document/w:body/w:p[6]/w:pPr/w:numPr/w:numId');
$this->assertEquals(3, $element->getAttribute('w:val'));

// Object
$element = $doc->getElement('/w:document/w:body/w:p[11]/w:r/w:object/o:OLEObject');
$this->assertEquals('Embed', $element->getAttribute('Type'));
}
}
34 changes: 34 additions & 0 deletions Tests/PHPWord/Writer/Word2007/FootnotesTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php
namespace PHPWord\Tests\Writer\Word2007;

use PHPWord;
use PHPWord\Tests\TestHelperDOCX;

/**
* Class PHPWord_Writer_Word2007_FootnotesTest
* @package PHPWord\Tests
* @runTestsInSeparateProcesses
*/
class FootnotesTest extends \PHPUnit_Framework_TestCase
{
/**
* Executed before each method of the class
*/
public function tearDown()
{
TestHelperDOCX::clear();
}

public function testWriteFootnotes()
{
$PHPWord = new PHPWord();
$section = $PHPWord->createSection();
$section->addText('Text');
$footnote = $section->createFootnote();
$footnote->addText('Footnote');
$footnote->addLink('http://google.com');
$doc = TestHelperDOCX::getDocument($PHPWord);

$this->assertTrue($doc->elementExists("/w:document/w:body/w:p/w:r/w:footnoteReference"));
}
}
30 changes: 24 additions & 6 deletions Tests/PHPWord/Writer/Word2007/StylesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,31 @@ public function testWriteStyles()
{
$PHPWord = new PHPWord();

$defaultStyle = array('align' => 'both');
$baseStyle = array('basedOn' => 'Normal');
$newStyle = array('basedOn' => 'Base Style', 'next' => 'Normal');
$PHPWord->setDefaultParagraphStyle($defaultStyle);
$PHPWord->addParagraphStyle('Base Style', $baseStyle);
$PHPWord->addParagraphStyle('New Style', $newStyle);
$pStyle = array('align' => 'both');
$pBase = array('basedOn' => 'Normal');
$pNew = array('basedOn' => 'Base Style', 'next' => 'Normal');
$rStyle = array('size' => 20);
$tStyle = array(
'bgColor' => 'FF0000',
'cellMarginTop' => 120,
'cellMarginBottom' => 120,
'cellMarginLeft' => 120,
'cellMarginRight' => 120,
'borderTopSize' => 120,
'borderBottomSize' => 120,
'borderLeftSize' => 120,
'borderRightSize' => 120,
'borderInsideHSize' => 120,
'borderInsideVSize' => 120,
);
$PHPWord->setDefaultParagraphStyle($pStyle);
$PHPWord->addParagraphStyle('Base Style', $pBase);
$PHPWord->addParagraphStyle('New Style', $pNew);
$PHPWord->addFontStyle('New Style', $rStyle, $pStyle);
$PHPWord->addTableStyle('Table Style', $tStyle, $tStyle);
$PHPWord->addTitleStyle(1, $rStyle, $pStyle);
$doc = TestHelperDOCX::getDocument($PHPWord);

$file = 'word/styles.xml';

// Normal style generated?
Expand Down
1 change: 1 addition & 0 deletions changelog.txt
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ Changes in branch for release 0.7.1 :
- Feature: (ivanlanin) - Paragraph: setTabs() function
- Feature: (ivanlanin) GH-99 - General: Basic support for TextRun on ODT and RTF
- Feature: (ivanlanin) - Reader: Initial effort for Word2007
- Feature: (ivanlanin) - MemoryImage: Allow remote image when allow_url_open = on
- QA: (Progi1984) - UnitTests

Changes in branch for release 0.7.0 :
Expand Down
2 changes: 1 addition & 1 deletion samples/Sample_01_SimpleText.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@
$section->addTextBreak();

// Image
$section->addImage('old/_earth.jpg', array('width'=>18, 'height'=>18));
$section->addImage('resources/_earth.jpg', array('width'=>18, 'height'=>18));

// Save file
$name = basename(__FILE__, '.php');
Expand Down
2 changes: 1 addition & 1 deletion samples/Sample_04_Textrun.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
$textrun->addText(' Sample Link: ');
$textrun->addLink('http://www.google.com', null, 'NLink');
$textrun->addText(' Sample Image: ');
$textrun->addImage('old/_earth.jpg', array('width'=>18, 'height'=>18));
$textrun->addImage('resources/_earth.jpg', array('width'=>18, 'height'=>18));
$textrun->addText(' Here is some more text. ');

// Save file
Expand Down
Loading