Merge remote-tracking branch 'upstream/develop' into develop
This commit is contained in:
commit
46a0768d29
15
Classes/PHPWord/Exceptions/InvalidImageException.php
Normal file
15
Classes/PHPWord/Exceptions/InvalidImageException.php
Normal 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
|
||||||
|
{
|
||||||
|
}
|
||||||
15
Classes/PHPWord/Exceptions/UnsupportedImageTypeException.php
Normal file
15
Classes/PHPWord/Exceptions/UnsupportedImageTypeException.php
Normal 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
|
||||||
|
{
|
||||||
|
}
|
||||||
@ -25,12 +25,14 @@
|
|||||||
* @version 0.7.0
|
* @version 0.7.0
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
use PhpOffice\PhpWord\Exceptions\InvalidImageException;
|
||||||
|
use PhpOffice\PhpWord\Exceptions\UnsupportedImageTypeException;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Class PHPWord_Section_Image
|
* Class PHPWord_Section_Image
|
||||||
*/
|
*/
|
||||||
class PHPWord_Section_Image
|
class PHPWord_Section_Image
|
||||||
{
|
{
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Image Src
|
* Image Src
|
||||||
*
|
*
|
||||||
@ -64,42 +66,43 @@ class PHPWord_Section_Image
|
|||||||
* Create a new Image
|
* Create a new Image
|
||||||
*
|
*
|
||||||
* @param string $src
|
* @param string $src
|
||||||
* @param mixed style
|
* @param mixed $style
|
||||||
|
* @param bool $isWatermark
|
||||||
|
* @throws InvalidImageException|UnsupportedImageTypeException
|
||||||
*/
|
*/
|
||||||
public function __construct($src, $style = null, $isWatermark = false)
|
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);
|
if (!file_exists($src)) {
|
||||||
$ext = strtolower($inf['extension']);
|
throw new InvalidImageException;
|
||||||
|
}
|
||||||
|
|
||||||
if (file_exists($src) && in_array($ext, $_supportedImageTypes)) {
|
if (!in_array(exif_imagetype($src), $supportedImageTypes)) {
|
||||||
$this->_src = $src;
|
throw new UnsupportedImageTypeException;
|
||||||
$this->_isWatermark = $isWatermark;
|
}
|
||||||
$this->_style = new PHPWord_Style_Image();
|
|
||||||
|
|
||||||
if (!is_null($style) && is_array($style)) {
|
$this->_src = $src;
|
||||||
foreach ($style as $key => $value) {
|
$this->_isWatermark = $isWatermark;
|
||||||
if (substr($key, 0, 1) != '_') {
|
$this->_style = new PHPWord_Style_Image();
|
||||||
$key = '_' . $key;
|
|
||||||
}
|
if (!is_null($style) && is_array($style)) {
|
||||||
$this->_style->setStyleValue($key, $value);
|
foreach ($style as $key => $value) {
|
||||||
|
if (substr($key, 0, 1) != '_') {
|
||||||
|
$key = '_' . $key;
|
||||||
}
|
}
|
||||||
|
$this->_style->setStyleValue($key, $value);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if (isset($style['wrappingStyle'])) {
|
if (isset($style['wrappingStyle'])) {
|
||||||
$this->_style->setWrappingStyle($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]);
|
||||||
$this->_style->setHeight($imgData[1]);
|
$this->_style->setHeight($imgData[1]);
|
||||||
}
|
|
||||||
|
|
||||||
return $this;
|
|
||||||
} else {
|
|
||||||
return false;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -285,6 +285,9 @@ class PHPWord_Writer_Word2007_Base extends PHPWord_Writer_Word2007_WriterPart
|
|||||||
$SpIsObject = ($styleParagraph instanceof PHPWord_Style_Paragraph) ? true : false;
|
$SpIsObject = ($styleParagraph instanceof PHPWord_Style_Paragraph) ? true : false;
|
||||||
|
|
||||||
$arrText = $textrun->getText();
|
$arrText = $textrun->getText();
|
||||||
|
if (!is_array($arrText)) {
|
||||||
|
$arrText = array($arrText);
|
||||||
|
}
|
||||||
|
|
||||||
$objWriter->startElement('w:p');
|
$objWriter->startElement('w:p');
|
||||||
|
|
||||||
|
|||||||
@ -33,6 +33,32 @@ class ImageTest extends \PHPUnit_Framework_TestCase
|
|||||||
$this->assertInstanceOf('PHPWord_Style_Image', $oImage->getStyle());
|
$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()
|
public function testStyle()
|
||||||
{
|
{
|
||||||
$oImage = new PHPWord_Section_Image(\join(
|
$oImage = new PHPWord_Section_Image(\join(
|
||||||
|
|||||||
@ -20,7 +20,7 @@ class BaseTest extends \PHPUnit_Framework_TestCase
|
|||||||
TestHelperDOCX::clear();
|
TestHelperDOCX::clear();
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testWriteImagePosition()
|
public function testWriteImage_Position()
|
||||||
{
|
{
|
||||||
$PHPWord = new PHPWord();
|
$PHPWord = new PHPWord();
|
||||||
$section = $PHPWord->createSection();
|
$section = $PHPWord->createSection();
|
||||||
@ -42,7 +42,7 @@ class BaseTest extends \PHPUnit_Framework_TestCase
|
|||||||
$this->assertRegExp('/position:absolute;/', $style);
|
$this->assertRegExp('/position:absolute;/', $style);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testWriteParagraphStyleAlign()
|
public function testWriteParagraphStyle_Align()
|
||||||
{
|
{
|
||||||
$PHPWord = new PHPWord();
|
$PHPWord = new PHPWord();
|
||||||
$section = $PHPWord->createSection();
|
$section = $PHPWord->createSection();
|
||||||
@ -55,10 +55,34 @@ class BaseTest extends \PHPUnit_Framework_TestCase
|
|||||||
$this->assertEquals('right', $element->getAttribute('w:val'));
|
$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
|
* Test write paragraph pagination
|
||||||
*/
|
*/
|
||||||
public function testWriteParagraphStylePagination()
|
public function testWriteParagraphStyle_Pagination()
|
||||||
{
|
{
|
||||||
// Create the doc
|
// Create the doc
|
||||||
$PHPWord = new PHPWord();
|
$PHPWord = new PHPWord();
|
||||||
@ -85,27 +109,18 @@ class BaseTest extends \PHPUnit_Framework_TestCase
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testWriteCellStyleCellGridSpan()
|
public function testWritePreserveText()
|
||||||
{
|
{
|
||||||
$PHPWord = new PHPWord();
|
$PHPWord = new PHPWord();
|
||||||
$section = $PHPWord->createSection();
|
$section = $PHPWord->createSection();
|
||||||
|
$footer = $section->createFooter();
|
||||||
|
|
||||||
$table = $section->addTable();
|
$footer->addPreserveText('{PAGE}');
|
||||||
|
|
||||||
$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);
|
$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'));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
BIN
Tests/_files/images/alexz-johnson.pcx
Normal file
BIN
Tests/_files/images/alexz-johnson.pcx
Normal file
Binary file not shown.
BIN
Tests/_files/images/angela_merkel.tif
Normal file
BIN
Tests/_files/images/angela_merkel.tif
Normal file
Binary file not shown.
BIN
Tests/_files/images/mars_noext_jpg
Normal file
BIN
Tests/_files/images/mars_noext_jpg
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 24 KiB |
@ -2,6 +2,7 @@
|
|||||||
namespace PHPWord\Tests;
|
namespace PHPWord\Tests;
|
||||||
|
|
||||||
use PHPWord;
|
use PHPWord;
|
||||||
|
use DOMDocument;
|
||||||
|
|
||||||
class TestHelperDOCX
|
class TestHelperDOCX
|
||||||
{
|
{
|
||||||
@ -9,7 +10,7 @@ class TestHelperDOCX
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* @param \PHPWord $PHPWord
|
* @param \PHPWord $PHPWord
|
||||||
* @return \PHPWord\Tests\XmlDocument
|
* @return \PHPWord\Tests\Xml_Document
|
||||||
*/
|
*/
|
||||||
public static function getDocument(PHPWord $PHPWord)
|
public static function getDocument(PHPWord $PHPWord)
|
||||||
{
|
{
|
||||||
@ -28,7 +29,7 @@ class TestHelperDOCX
|
|||||||
$zip->close();
|
$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()
|
public static function clear()
|
||||||
@ -59,3 +60,65 @@ class TestHelperDOCX
|
|||||||
rmdir($dir);
|
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);
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -24,7 +24,7 @@
|
|||||||
"ext-xml": "*"
|
"ext-xml": "*"
|
||||||
},
|
},
|
||||||
"require-dev": {
|
"require-dev": {
|
||||||
"phpunit/phpunit": "3.7.28"
|
"phpunit/phpunit": "3.7.*"
|
||||||
},
|
},
|
||||||
"recommend": {
|
"recommend": {
|
||||||
"ext-zip": "*",
|
"ext-zip": "*",
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user