diff --git a/Classes/PHPWord/Exceptions/InvalidImageException.php b/Classes/PHPWord/Exceptions/InvalidImageException.php new file mode 100644 index 00000000..2d5337b4 --- /dev/null +++ b/Classes/PHPWord/Exceptions/InvalidImageException.php @@ -0,0 +1,15 @@ +_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]); } } diff --git a/Tests/PHPWord/Section/ImageTest.php b/Tests/PHPWord/Section/ImageTest.php index 4e265db7..f11b1293 100644 --- a/Tests/PHPWord/Section/ImageTest.php +++ b/Tests/PHPWord/Section/ImageTest.php @@ -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( diff --git a/Tests/_files/images/alexz-johnson.pcx b/Tests/_files/images/alexz-johnson.pcx new file mode 100644 index 00000000..cffae373 Binary files /dev/null and b/Tests/_files/images/alexz-johnson.pcx differ diff --git a/Tests/_files/images/angela_merkel.tif b/Tests/_files/images/angela_merkel.tif new file mode 100644 index 00000000..236dedd1 Binary files /dev/null and b/Tests/_files/images/angela_merkel.tif differ diff --git a/Tests/_files/images/mars_noext_jpg b/Tests/_files/images/mars_noext_jpg new file mode 100644 index 00000000..584d3171 Binary files /dev/null and b/Tests/_files/images/mars_noext_jpg differ