diff --git a/Classes/PHPWord/Writer/Word2007.php b/Classes/PHPWord/Writer/Word2007.php index 8375c56a..f58f2c43 100755 --- a/Classes/PHPWord/Writer/Word2007.php +++ b/Classes/PHPWord/Writer/Word2007.php @@ -25,6 +25,9 @@ * @version 0.7.0 */ +use PhpOffice\PhpWord\Exceptions\InvalidImageException; +use PhpOffice\PhpWord\Exceptions\UnsupportedImageTypeException; + /** * Class PHPWord_Writer_Word2007 */ @@ -191,25 +194,40 @@ class PHPWord_Writer_Word2007 implements PHPWord_Writer_IWriter } } - private function _chkContentTypes($src) + /** + * @param string $src + */ + private function checkContentTypes($src) { - $srcInfo = pathinfo($src); - $extension = strtolower($srcInfo['extension']); - if (substr($extension, 0, 3) == 'php') { - $extension = 'php'; - } - $_supportedImageTypes = array('jpg', 'jpeg', 'gif', 'png', 'bmp', 'tif', 'tiff', 'php'); + $supportedImageTypes = array(IMAGETYPE_JPEG, IMAGETYPE_GIF, IMAGETYPE_PNG, IMAGETYPE_BMP, IMAGETYPE_TIFF_II, IMAGETYPE_TIFF_MM); - if (in_array($extension, $_supportedImageTypes)) { - $imagedata = getimagesize($src); - $imagetype = image_type_to_mime_type($imagedata[2]); - $imageext = image_type_to_extension($imagedata[2]); - $imageext = str_replace('.', '', $imageext); - if ($imageext == 'jpeg') { - $imageext = 'jpg'; + $extension = null; + if (stripos(strrev($src), strrev('.php')) === 0) { + $extension = 'php'; + } else { + $imageType = exif_imagetype($src); + if ($imageType === IMAGETYPE_JPEG) { + $extension = 'jpg'; + } elseif ($imageType === IMAGETYPE_GIF) { + $extension = 'gif'; + } elseif ($imageType === IMAGETYPE_PNG) { + $extension = 'png'; + } elseif ($imageType === IMAGETYPE_BMP) { + $extension = 'bmp'; + } elseif ($imageType === IMAGETYPE_TIFF_II || $imageType === IMAGETYPE_TIFF_MM) { + $extension = 'tif'; } - if (!in_array($imagetype, $this->_imageTypes)) { - $this->_imageTypes[$imageext] = $imagetype; + } + + if (in_array($extension, $supportedImageTypes)) { + $imageData = getimagesize($src); + $imageType = image_type_to_mime_type($imageData[2]); + $imageExtension = str_replace('.', '', image_type_to_extension($imageData[2])); + if ($imageExtension === 'jpeg') { + $imageExtension = 'jpg'; + } + if (!in_array($imageType, $this->_imageTypes)) { + $this->_imageTypes[$imageExtension] = $imageType; } } else { if (!in_array($extension, $this->_objectTypes)) { @@ -258,10 +276,10 @@ class PHPWord_Writer_Word2007 implements PHPWord_Writer_IWriter $objZip->addFromString('word/' . $element['target'], $imageContents); imagedestroy($image); - $this->_chkContentTypes($element['source']); + $this->checkContentTypes($element['source']); } else { $objZip->addFile($element['source'], 'word/' . $element['target']); - $this->_chkContentTypes($element['source']); + $this->checkContentTypes($element['source']); } } } diff --git a/Tests/PHPWord/Writer/Word2007Test.php b/Tests/PHPWord/Writer/Word2007Test.php index 28b0d581..59bc6387 100644 --- a/Tests/PHPWord/Writer/Word2007Test.php +++ b/Tests/PHPWord/Writer/Word2007Test.php @@ -4,6 +4,7 @@ namespace PHPWord\Tests\Writer; use PHPUnit_Framework_TestCase; use PHPWord_Writer_Word2007; use PHPWord; +use PHPWord\Tests\TestHelperDOCX; /** * Class Word2007Test @@ -13,9 +14,11 @@ use PHPWord; */ class Word2007Test extends \PHPUnit_Framework_TestCase { - /** - * Test construct - */ + public function tearDown() + { + TestHelperDOCX::clear(); + } + public function testConstruct() { $object = new PHPWord_Writer_Word2007(new PHPWord()); @@ -35,9 +38,6 @@ class Word2007Test extends \PHPUnit_Framework_TestCase } } - /** - * Test save() - */ public function testSave() { $phpWord = new PHPWord(); @@ -60,4 +60,29 @@ class Word2007Test extends \PHPUnit_Framework_TestCase $this->assertTrue(file_exists($file)); unlink($file); } -} + + /** + * @covers PHPWord_Writer_Word2007::checkContentTypes + */ + public function testCheckContentTypes() + { + $phpWord = new PHPWord(); + $section = $phpWord->createSection(); + $section->addImage(PHPWORD_TESTS_DIR_ROOT . "/_files/images/mars_noext_jpg"); + $section->addImage(PHPWORD_TESTS_DIR_ROOT . "/_files/images/mars.jpg"); + $section->addImage(PHPWORD_TESTS_DIR_ROOT . "/_files/images/mario.gif"); + $section->addImage(PHPWORD_TESTS_DIR_ROOT . "/_files/images/firefox.png"); + $section->addImage(PHPWORD_TESTS_DIR_ROOT . "/_files/images/duke_nukem.bmp"); + $section->addImage(PHPWORD_TESTS_DIR_ROOT . "/_files/images/angela_merkel.tif"); + + $doc = TestHelperDOCX::getDocument($phpWord); + $mediaPath = $doc->getPath() . "/word/media"; + + $this->assertFileEquals(PHPWORD_TESTS_DIR_ROOT . "/_files/images/mars_noext_jpg", $mediaPath . "/section_image1.jpg"); + $this->assertFileEquals(PHPWORD_TESTS_DIR_ROOT . "/_files/images/mars.jpg", $mediaPath . "/section_image2.jpg"); + $this->assertFileEquals(PHPWORD_TESTS_DIR_ROOT . "/_files/images/mario.gif", $mediaPath . "/section_image3.gif"); + $this->assertFileEquals(PHPWORD_TESTS_DIR_ROOT . "/_files/images/firefox.png", $mediaPath . "/section_image4.png"); + $this->assertFileEquals(PHPWORD_TESTS_DIR_ROOT . "/_files/images/duke_nukem.bmp", $mediaPath . "/section_image5.bmp"); + $this->assertFileEquals(PHPWORD_TESTS_DIR_ROOT . "/_files/images/angela_merkel.tif", $mediaPath . "/section_image6.tif"); + } +} \ No newline at end of file diff --git a/Tests/_inc/TestHelperDOCX.php b/Tests/_inc/TestHelperDOCX.php index 643cc00a..a368b8e4 100644 --- a/Tests/_inc/TestHelperDOCX.php +++ b/Tests/_inc/TestHelperDOCX.php @@ -59,4 +59,4 @@ class TestHelperDOCX rmdir($dir); } -} +} \ No newline at end of file diff --git a/Tests/_inc/XmlDocument.php b/Tests/_inc/XmlDocument.php index 69569aa6..f16bd6d8 100644 --- a/Tests/_inc/XmlDocument.php +++ b/Tests/_inc/XmlDocument.php @@ -63,4 +63,20 @@ class XmlDocument $elements = $this->xpath->query($path); return $elements->item(0); } + + /** + * @return string + */ + public function getFile() + { + return $this->file; + } + + /** + * @return string + */ + public function getPath() + { + return $this->path; + } }