Skip to content

Commit 12a7977

Browse files
committed
Merge branch 'refs/heads/master' into develop
Conflicts: changelog.txt
2 parents 5bcd114 + 74566e3 commit 12a7977

File tree

8 files changed

+114
-53
lines changed

8 files changed

+114
-53
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 = exif_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 & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,6 @@ class PHPWord_Section_Image
6161
*/
6262
private $_isWatermark;
6363

64-
6564
/**
6665
* Create a new Image
6766
*
@@ -72,13 +71,11 @@ class PHPWord_Section_Image
7271
*/
7372
public function __construct($src, $style = null, $isWatermark = false)
7473
{
75-
$supportedImageTypes = array(IMAGETYPE_JPEG, IMAGETYPE_GIF, IMAGETYPE_PNG, IMAGETYPE_BMP, IMAGETYPE_TIFF_II, IMAGETYPE_TIFF_MM);
76-
7774
if (!file_exists($src)) {
7875
throw new InvalidImageException;
7976
}
8077

81-
if (!in_array(exif_imagetype($src), $supportedImageTypes)) {
78+
if (!PHPWord_Shared_File::imagetype($src)) {
8279
throw new UnsupportedImageTypeException;
8380
}
8481

Classes/PHPWord/Shared/File.php

Lines changed: 63 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -30,15 +30,20 @@
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
*
36-
* @param string $pFilename Filename
42+
* @param string $pFilename Filename
3743
* @return bool
3844
*/
3945
public static function file_exists($pFilename)
4046
{
41-
// Regular file_exists
4247
return file_exists($pFilename);
4348
}
4449

@@ -50,18 +55,13 @@ public static function file_exists($pFilename)
5055
*/
5156
public static function realpath($pFilename)
5257
{
53-
// Returnvalue
54-
$returnValue = '';
55-
56-
// Try using realpath()
5758
$returnValue = realpath($pFilename);
5859

59-
// Found something?
60-
if ($returnValue == '' || is_null($returnValue)) {
60+
if (!$returnValue) {
6161
$pathArray = explode('/', $pFilename);
62-
while (in_array('..', $pathArray) && $pathArray[0] != '..') {
62+
while (in_array('..', $pathArray) && $pathArray[0] !== '..') {
6363
for ($i = 0; $i < count($pathArray); ++$i) {
64-
if ($pathArray[$i] == '..' && $i > 0) {
64+
if ($pathArray[$i] === '..' && $i > 0) {
6565
unset($pathArray[$i]);
6666
unset($pathArray[$i - 1]);
6767
break;
@@ -71,7 +71,58 @@ public static function realpath($pFilename)
7171
$returnValue = implode('/', $pathArray);
7272
}
7373

74-
// Return
7574
return $returnValue;
7675
}
77-
}
76+
77+
/**
78+
* PHP Words version of exif_imagetype to return the Image Type from a file
79+
*
80+
* @param string $filename
81+
* @return int|bool
82+
*/
83+
private static function fallbackImagetype($filename)
84+
{
85+
if ((list($width, $height, $type, $attr) = getimagesize($filename)) !== false) {
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+
}
97+
}
98+
return false;
99+
}
100+
101+
/**
102+
* Return the Image Type from a file
103+
*
104+
* @param string $filename
105+
* @param bool $userFallbackFunction
106+
* @return int|bool
107+
*/
108+
public static function imagetype($filename, $userFallbackFunction = false)
109+
{
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;
125+
}
126+
return false;
127+
}
128+
}

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 = exif_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: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@
2222
* @version ##VERSION##, ##DATE##
2323
**************************************************************************************
2424

25-
Changes in branch for release 0.9.0 :
26-
- QA: (Progi1984) - Documentation
25+
Changes in branch for release 0.8.1 :
26+
- Feature: (bskrtich, gabrielbull) - Added fallback for computers that do not have exif_imagetype
2727

2828
Changes in branch for release 0.8.0 :
2929
- Bugfix: (gabrielbull) - Fixed bug with cell styling

0 commit comments

Comments
 (0)