Merge remote-tracking branch 'upstream/develop' into develop

This commit is contained in:
Ivan Lanin 2014-03-12 10:46:36 +07:00
commit 46a0768d29
11 changed files with 189 additions and 49 deletions

View File

@ -0,0 +1,15 @@
<?php
namespace PhpOffice\PhpWord\Exceptions;
use Exception;
/**
* InvalidImageException
*
* Exception used for when an image is not found
*
* @package PHPWord
*/
class InvalidImageException extends Exception
{
}

View File

@ -0,0 +1,15 @@
<?php
namespace PhpOffice\PhpWord\Exceptions;
use Exception;
/**
* UnsupportedImageTypeException
*
* Exception used for when an image type is unsupported
*
* @package PHPWord
*/
class UnsupportedImageTypeException extends Exception
{
}

View File

@ -25,12 +25,14 @@
* @version 0.7.0
*/
use PhpOffice\PhpWord\Exceptions\InvalidImageException;
use PhpOffice\PhpWord\Exceptions\UnsupportedImageTypeException;
/**
* Class PHPWord_Section_Image
*/
class PHPWord_Section_Image
{
/**
* Image Src
*
@ -64,42 +66,43 @@ class PHPWord_Section_Image
* Create a new Image
*
* @param string $src
* @param mixed style
* @param mixed $style
* @param bool $isWatermark
* @throws InvalidImageException|UnsupportedImageTypeException
*/
public function __construct($src, $style = null, $isWatermark = false)
{
$_supportedImageTypes = array('jpg', 'jpeg', 'gif', 'png', 'bmp', 'tif', 'tiff');
$supportedImageTypes = array(IMAGETYPE_JPEG, IMAGETYPE_GIF, IMAGETYPE_PNG, IMAGETYPE_BMP, IMAGETYPE_TIFF_II, IMAGETYPE_TIFF_MM);
$inf = pathinfo($src);
$ext = strtolower($inf['extension']);
if (!file_exists($src)) {
throw new InvalidImageException;
}
if (file_exists($src) && in_array($ext, $_supportedImageTypes)) {
$this->_src = $src;
$this->_isWatermark = $isWatermark;
$this->_style = new PHPWord_Style_Image();
if (!in_array(exif_imagetype($src), $supportedImageTypes)) {
throw new UnsupportedImageTypeException;
}
if (!is_null($style) && is_array($style)) {
foreach ($style as $key => $value) {
if (substr($key, 0, 1) != '_') {
$key = '_' . $key;
}
$this->_style->setStyleValue($key, $value);
$this->_src = $src;
$this->_isWatermark = $isWatermark;
$this->_style = new PHPWord_Style_Image();
if (!is_null($style) && is_array($style)) {
foreach ($style as $key => $value) {
if (substr($key, 0, 1) != '_') {
$key = '_' . $key;
}
$this->_style->setStyleValue($key, $value);
}
}
if (isset($style['wrappingStyle'])) {
$this->_style->setWrappingStyle($style['wrappingStyle']);
}
if (isset($style['wrappingStyle'])) {
$this->_style->setWrappingStyle($style['wrappingStyle']);
}
if ($this->_style->getWidth() == null && $this->_style->getHeight() == null) {
$imgData = getimagesize($this->_src);
$this->_style->setWidth($imgData[0]);
$this->_style->setHeight($imgData[1]);
}
return $this;
} else {
return false;
if ($this->_style->getWidth() == null && $this->_style->getHeight() == null) {
$imgData = getimagesize($this->_src);
$this->_style->setWidth($imgData[0]);
$this->_style->setHeight($imgData[1]);
}
}

View File

@ -285,6 +285,9 @@ class PHPWord_Writer_Word2007_Base extends PHPWord_Writer_Word2007_WriterPart
$SpIsObject = ($styleParagraph instanceof PHPWord_Style_Paragraph) ? true : false;
$arrText = $textrun->getText();
if (!is_array($arrText)) {
$arrText = array($arrText);
}
$objWriter->startElement('w:p');

View File

@ -33,6 +33,32 @@ class ImageTest extends \PHPUnit_Framework_TestCase
$this->assertInstanceOf('PHPWord_Style_Image', $oImage->getStyle());
}
public function testValidImageTypes()
{
new PHPWord_Section_Image(PHPWORD_TESTS_DIR_ROOT . "/_files/images/mars_noext_jpg");
new PHPWord_Section_Image(PHPWORD_TESTS_DIR_ROOT . "/_files/images/mars.jpg");
new PHPWord_Section_Image(PHPWORD_TESTS_DIR_ROOT . "/_files/images/mario.gif");
new PHPWord_Section_Image(PHPWORD_TESTS_DIR_ROOT . "/_files/images/firefox.png");
new PHPWord_Section_Image(PHPWORD_TESTS_DIR_ROOT . "/_files/images/duke_nukem.bmp");
new PHPWord_Section_Image(PHPWORD_TESTS_DIR_ROOT . "/_files/images/angela_merkel.tif");
}
/**
* @expectedException \PhpOffice\PhpWord\Exceptions\InvalidImageException
*/
public function testImageNotFound()
{
new PHPWord_Section_Image(PHPWORD_TESTS_DIR_ROOT . "/_files/images/thisisnotarealimage");
}
/**
* @expectedException \PhpOffice\PhpWord\Exceptions\UnsupportedImageTypeException
*/
public function testInvalidImageTypes()
{
new PHPWord_Section_Image(PHPWORD_TESTS_DIR_ROOT . "/_files/images/alexz-johnson.pcx");
}
public function testStyle()
{
$oImage = new PHPWord_Section_Image(\join(

View File

@ -20,7 +20,7 @@ class BaseTest extends \PHPUnit_Framework_TestCase
TestHelperDOCX::clear();
}
public function testWriteImagePosition()
public function testWriteImage_Position()
{
$PHPWord = new PHPWord();
$section = $PHPWord->createSection();
@ -42,7 +42,7 @@ class BaseTest extends \PHPUnit_Framework_TestCase
$this->assertRegExp('/position:absolute;/', $style);
}
public function testWriteParagraphStyleAlign()
public function testWriteParagraphStyle_Align()
{
$PHPWord = new PHPWord();
$section = $PHPWord->createSection();
@ -55,10 +55,34 @@ class BaseTest extends \PHPUnit_Framework_TestCase
$this->assertEquals('right', $element->getAttribute('w:val'));
}
public function testWriteCellStyle_CellGridSpan()
{
$PHPWord = new PHPWord();
$section = $PHPWord->createSection();
$table = $section->addTable();
$table->addRow();
$cell = $table->addCell(200);
$cell->getStyle()->setGridSpan(5);
$table->addRow();
$table->addCell(40);
$table->addCell(40);
$table->addCell(40);
$table->addCell(40);
$table->addCell(40);
$doc = TestHelperDOCX::getDocument($PHPWord);
$element = $doc->getElement('/w:document/w:body/w:tbl/w:tr/w:tc/w:tcPr/w:gridSpan');
$this->assertEquals(5, $element->getAttribute('w:val'));
}
/**
* Test write paragraph pagination
*/
public function testWriteParagraphStylePagination()
public function testWriteParagraphStyle_Pagination()
{
// Create the doc
$PHPWord = new PHPWord();
@ -85,27 +109,18 @@ class BaseTest extends \PHPUnit_Framework_TestCase
}
}
public function testWriteCellStyleCellGridSpan()
public function testWritePreserveText()
{
$PHPWord = new PHPWord();
$section = $PHPWord->createSection();
$footer = $section->createFooter();
$table = $section->addTable();
$table->addRow();
$cell = $table->addCell(200);
$cell->getStyle()->setGridSpan(5);
$table->addRow();
$table->addCell(40);
$table->addCell(40);
$table->addCell(40);
$table->addCell(40);
$table->addCell(40);
$footer->addPreserveText('{PAGE}');
$doc = TestHelperDOCX::getDocument($PHPWord);
$element = $doc->getElement('/w:document/w:body/w:tbl/w:tr/w:tc/w:tcPr/w:gridSpan');
$preserve = $doc->getElement("w:p/w:r[2]/w:instrText", 'word/footer1.xml');
$this->assertEquals(5, $element->getAttribute('w:val'));
$this->assertEquals('PAGE', $preserve->nodeValue);
$this->assertEquals('preserve', $preserve->getAttribute('xml:space'));
}
}
}

Binary file not shown.

Binary file not shown.

Binary file not shown.

After

Width:  |  Height:  |  Size: 24 KiB

View File

@ -2,6 +2,7 @@
namespace PHPWord\Tests;
use PHPWord;
use DOMDocument;
class TestHelperDOCX
{
@ -9,7 +10,7 @@ class TestHelperDOCX
/**
* @param \PHPWord $PHPWord
* @return \PHPWord\Tests\XmlDocument
* @return \PHPWord\Tests\Xml_Document
*/
public static function getDocument(PHPWord $PHPWord)
{
@ -28,7 +29,7 @@ class TestHelperDOCX
$zip->close();
}
return new XmlDocument(sys_get_temp_dir() . '/PHPWord_Unit_Test/');
return new Xml_Document(sys_get_temp_dir() . '/PHPWord_Unit_Test/');
}
public static function clear()
@ -59,3 +60,65 @@ class TestHelperDOCX
rmdir($dir);
}
}
class Xml_Document
{
/** @var string $path */
private $path;
/** @var \DOMDocument $dom */
private $dom;
/** @var \DOMXpath $xpath */
private $xpath;
/** @var string $file */
private $file;
/**
* @param string $path
*/
public function __construct($path)
{
$this->path = realpath($path);
}
/**
* @param string $file
* @return \DOMDocument
*/
public function getFileDom($file = 'word/document.xml')
{
if (null !== $this->dom && $file === $this->file) {
return $this->dom;
}
$this->xpath = null;
$this->file = $file;
$file = $this->path . '/' . $file;
$this->dom = new DOMDocument();
$this->dom->load($file);
return $this->dom;
}
/**
* @param string $path
* @param string $file
* @return \DOMElement
*/
public function getElement($path, $file = 'word/document.xml')
{
if ($this->dom === null || $file !== $this->file) {
$this->getFileDom($file);
}
if (null === $this->xpath) {
$this->xpath = new \DOMXpath($this->dom);
}
$elements = $this->xpath->query($path);
return $elements->item(0);
}
}

View File

@ -24,7 +24,7 @@
"ext-xml": "*"
},
"require-dev": {
"phpunit/phpunit": "3.7.28"
"phpunit/phpunit": "3.7.*"
},
"recommend": {
"ext-zip": "*",