Skip to content

Commit 74566e3

Browse files
committed
Added test case for image type detection
Conflicts: changelog.txt
1 parent 745f3c8 commit 74566e3

File tree

8 files changed

+87
-45
lines changed

8 files changed

+87
-45
lines changed

Classes/PHPWord/Media.php

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -94,25 +94,13 @@ public static function addSectionMediaElement($src, $type, PHPWord_Section_Memor
9494
if (!$isMemImage) {
9595
$isMemImage = (filter_var($src, FILTER_VALIDATE_URL) !== false);
9696
}
97-
$extension = '';
9897
if ($isMemImage) {
9998
$extension = $memoryImage->getImageExtension();
10099
$media['isMemImage'] = true;
101100
$media['createfunction'] = $memoryImage->getImageCreateFunction();
102101
$media['imagefunction'] = $memoryImage->getImageFunction();
103102
} else {
104-
$imageType = PHPWord_Shared_File::imagetype($src);
105-
if ($imageType === IMAGETYPE_JPEG) {
106-
$extension = 'jpg';
107-
} elseif ($imageType === IMAGETYPE_GIF) {
108-
$extension = 'gif';
109-
} elseif ($imageType === IMAGETYPE_PNG) {
110-
$extension = 'png';
111-
} elseif ($imageType === IMAGETYPE_BMP) {
112-
$extension = 'bmp';
113-
} elseif ($imageType === IMAGETYPE_TIFF_II || $imageType === IMAGETYPE_TIFF_MM) {
114-
$extension = 'tif';
115-
}
103+
$extension = PHPWord_Shared_File::imagetype($src);
116104
}
117105

118106
$folder = 'media';

Classes/PHPWord/Section/Image.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -71,13 +71,11 @@ class PHPWord_Section_Image
7171
*/
7272
public function __construct($src, $style = null, $isWatermark = false)
7373
{
74-
$supportedImageTypes = array(IMAGETYPE_JPEG, IMAGETYPE_GIF, IMAGETYPE_PNG, IMAGETYPE_BMP, IMAGETYPE_TIFF_II, IMAGETYPE_TIFF_MM);
75-
7674
if (!file_exists($src)) {
7775
throw new InvalidImageException;
7876
}
7977

80-
if (!in_array(PHPWord_Shared_File::imagetype($src), $supportedImageTypes)) {
78+
if (!PHPWord_Shared_File::imagetype($src)) {
8179
throw new UnsupportedImageTypeException;
8280
}
8381

Classes/PHPWord/Shared/File.php

Lines changed: 35 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,12 @@
3030
*/
3131
class PHPWord_Shared_File
3232
{
33+
const IMAGETYPE_JPEG = 'jpg';
34+
const IMAGETYPE_GIF = 'gif';
35+
const IMAGETYPE_PNG = 'png';
36+
const IMAGETYPE_BMP = 'bmp';
37+
const IMAGETYPE_TIFF = 'tif';
38+
3339
/**
3440
* Verify if a file exists
3541
*
@@ -74,10 +80,20 @@ public static function realpath($pFilename)
7480
* @param string $filename
7581
* @return int|bool
7682
*/
77-
public static function PHPWord_imagetype($filename)
83+
private static function fallbackImagetype($filename)
7884
{
7985
if ((list($width, $height, $type, $attr) = getimagesize($filename)) !== false) {
80-
return $type;
86+
if ($type === 2) {
87+
return self::IMAGETYPE_JPEG;
88+
} elseif ($type === 1) {
89+
return self::IMAGETYPE_GIF;
90+
} elseif ($type === 3) {
91+
return self::IMAGETYPE_PNG;
92+
} elseif ($type === 6) {
93+
return self::IMAGETYPE_BMP;
94+
} elseif ($type === 7 || $type === 8) {
95+
return self::IMAGETYPE_TIFF;
96+
}
8197
}
8298
return false;
8399
}
@@ -86,14 +102,26 @@ public static function PHPWord_imagetype($filename)
86102
* Return the Image Type from a file
87103
*
88104
* @param string $filename
105+
* @param bool $userFallbackFunction
89106
* @return int|bool
90107
*/
91-
public static function imagetype($filename)
108+
public static function imagetype($filename, $userFallbackFunction = false)
92109
{
93-
if (function_exists('exif_imagetype')) {
94-
return exif_imagetype($filename);
95-
} else {
96-
return self::PHPWord_imagetype($filename);
110+
if ($userFallbackFunction || !function_exists('exif_imagetype')) {
111+
return self::fallbackImagetype($filename);
112+
}
113+
114+
$imagetype = exif_imagetype($filename);
115+
if ($imagetype === IMAGETYPE_JPEG) {
116+
return self::IMAGETYPE_JPEG;
117+
} elseif ($imagetype === IMAGETYPE_GIF) {
118+
return self::IMAGETYPE_GIF;
119+
} elseif ($imagetype === IMAGETYPE_PNG) {
120+
return self::IMAGETYPE_PNG;
121+
} elseif ($imagetype === IMAGETYPE_BMP) {
122+
return self::IMAGETYPE_BMP;
123+
} elseif ($imagetype === IMAGETYPE_TIFF_II || $imagetype === IMAGETYPE_TIFF_MM) {
124+
return self::IMAGETYPE_TIFF;
97125
}
98126
return false;
99127
}

Classes/PHPWord/Writer/Word2007.php

Lines changed: 4 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -212,21 +212,10 @@ private function checkContentTypes($src)
212212
if (stripos(strrev($src), strrev('.php')) === 0) {
213213
$extension = 'php';
214214
} else {
215-
$imageType = PHPWord_Shared_File::imagetype($src);
216-
if ($imageType === IMAGETYPE_JPEG) {
217-
$extension = 'jpg';
218-
} elseif ($imageType === IMAGETYPE_GIF) {
219-
$extension = 'gif';
220-
} elseif ($imageType === IMAGETYPE_PNG) {
221-
$extension = 'png';
222-
} elseif ($imageType === IMAGETYPE_BMP) {
223-
$extension = 'bmp';
224-
} elseif ($imageType === IMAGETYPE_TIFF_II || $imageType === IMAGETYPE_TIFF_MM) {
225-
$extension = 'tif';
226-
}
215+
$extension = PHPWord_Shared_File::imagetype($src);
227216
}
228217

229-
if (isset($extension)) {
218+
if (isset($extension) && $extension) {
230219
$imageData = getimagesize($src);
231220
$imageType = image_type_to_mime_type($imageData[2]);
232221
$imageExtension = str_replace('.', '', image_type_to_extension($imageData[2]));
@@ -236,10 +225,8 @@ private function checkContentTypes($src)
236225
if (!in_array($imageType, $this->_imageTypes)) {
237226
$this->_imageTypes[$imageExtension] = $imageType;
238227
}
239-
} else {
240-
if (!in_array($extension, $this->_objectTypes)) {
241-
$this->_objectTypes[] = $extension;
242-
}
228+
} elseif (!in_array($extension, $this->_objectTypes)) {
229+
$this->_objectTypes[] = $extension;
243230
}
244231
}
245232

Tests/PHPWord/Shared/FileTest.php

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,4 +54,39 @@ public function testRealpath()
5454
$expected = $dir . DIRECTORY_SEPARATOR . $file;
5555
$this->assertEquals($expected, PHPWord_Shared_File::realpath($file));
5656
}
57-
}
57+
58+
/**
59+
* @covers PHPWord_Shared_File::imagetype
60+
* @covers PHPWord_Shared_File::fallbackImagetype
61+
*/
62+
public function testImagetype()
63+
{
64+
$filename = PHPWORD_TESTS_DIR_ROOT . "/_files/images/mars_noext_jpg";
65+
$this->assertEquals(PHPWord_Shared_File::IMAGETYPE_JPEG, PHPWord_Shared_File::imagetype($filename, true));
66+
$this->assertEquals(PHPWord_Shared_File::IMAGETYPE_JPEG, PHPWord_Shared_File::imagetype($filename));
67+
68+
$filename = PHPWORD_TESTS_DIR_ROOT . "/_files/images/mars.jpg";
69+
$this->assertEquals(PHPWord_Shared_File::IMAGETYPE_JPEG, PHPWord_Shared_File::imagetype($filename, true));
70+
$this->assertEquals(PHPWord_Shared_File::IMAGETYPE_JPEG, PHPWord_Shared_File::imagetype($filename));
71+
72+
$filename = PHPWORD_TESTS_DIR_ROOT . "/_files/images/mario.gif";
73+
$this->assertEquals(PHPWord_Shared_File::IMAGETYPE_GIF, PHPWord_Shared_File::imagetype($filename, true));
74+
$this->assertEquals(PHPWord_Shared_File::IMAGETYPE_GIF, PHPWord_Shared_File::imagetype($filename));
75+
76+
$filename = PHPWORD_TESTS_DIR_ROOT . "/_files/images/firefox.png";
77+
$this->assertEquals(PHPWord_Shared_File::IMAGETYPE_PNG, PHPWord_Shared_File::imagetype($filename, true));
78+
$this->assertEquals(PHPWord_Shared_File::IMAGETYPE_PNG, PHPWord_Shared_File::imagetype($filename));
79+
80+
$filename = PHPWORD_TESTS_DIR_ROOT . "/_files/images/duke_nukem.bmp";
81+
$this->assertEquals(PHPWord_Shared_File::IMAGETYPE_BMP, PHPWord_Shared_File::imagetype($filename, true));
82+
$this->assertEquals(PHPWord_Shared_File::IMAGETYPE_BMP, PHPWord_Shared_File::imagetype($filename));
83+
84+
$filename = PHPWORD_TESTS_DIR_ROOT . "/_files/images/angela_merkel.tif";
85+
$this->assertEquals(PHPWord_Shared_File::IMAGETYPE_TIFF, PHPWord_Shared_File::imagetype($filename, true));
86+
$this->assertEquals(PHPWord_Shared_File::IMAGETYPE_TIFF, PHPWord_Shared_File::imagetype($filename));
87+
88+
$filename = PHPWORD_TESTS_DIR_ROOT . "/_files/images/alexz-johnson.pcx";
89+
$this->assertFalse(PHPWord_Shared_File::imagetype($filename, true));
90+
$this->assertFalse(PHPWord_Shared_File::imagetype($filename));
91+
}
92+
}

Tests/PHPWord/Writer/Word2007/DocumentTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,4 +84,4 @@ public function testElements()
8484
$element = $doc->getElement('/w:document/w:body/w:p[11]/w:r/w:object/o:OLEObject');
8585
$this->assertEquals('Embed', $element->getAttribute('Type'));
8686
}
87-
}
87+
}

Tests/_inc/TestHelperDOCX.php

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,14 @@
55

66
class TestHelperDOCX
77
{
8-
/** @var string $file */
9-
static protected $file;
8+
/**
9+
* @var string
10+
*/
11+
protected static $file;
1012

1113
/**
1214
* @param \PHPWord $PHPWord
15+
* @param string $writer
1316
* @return \PHPWord\Tests\XmlDocument
1417
*/
1518
public static function getDocument(PHPWord $PHPWord, $writer = 'Word2007')
@@ -67,4 +70,4 @@ public static function getFile()
6770
{
6871
return self::$file;
6972
}
70-
}
73+
}

changelog.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,9 @@
2222
* @version ##VERSION##, ##DATE##
2323
**************************************************************************************
2424

25+
Changes in branch for release 0.8.1 :
26+
- Feature: (bskrtich, gabrielbull) - Added fallback for computers that do not have exif_imagetype
27+
2528
Changes in branch for release 0.8.0 :
2629
- Bugfix: (gabrielbull) - Fixed bug with cell styling
2730
- Bugfix: (gabrielbull) - Fixed bug list items inside of cells

0 commit comments

Comments
 (0)