Skip to content

Commit 745f3c8

Browse files
hskrtichgabrielbull
authored andcommitted
Add function to check for image type.
Some servers do not have exif_imagetype available. So I added a work around.
1 parent 04623f2 commit 745f3c8

File tree

4 files changed

+38
-16
lines changed

4 files changed

+38
-16
lines changed

Classes/PHPWord/Media.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ public static function addSectionMediaElement($src, $type, PHPWord_Section_Memor
101101
$media['createfunction'] = $memoryImage->getImageCreateFunction();
102102
$media['imagefunction'] = $memoryImage->getImageFunction();
103103
} else {
104-
$imageType = exif_imagetype($src);
104+
$imageType = PHPWord_Shared_File::imagetype($src);
105105
if ($imageType === IMAGETYPE_JPEG) {
106106
$extension = 'jpg';
107107
} elseif ($imageType === IMAGETYPE_GIF) {

Classes/PHPWord/Section/Image.php

Lines changed: 1 addition & 2 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
*
@@ -78,7 +77,7 @@ public function __construct($src, $style = null, $isWatermark = false)
7877
throw new InvalidImageException;
7978
}
8079

81-
if (!in_array(exif_imagetype($src), $supportedImageTypes)) {
80+
if (!in_array(PHPWord_Shared_File::imagetype($src), $supportedImageTypes)) {
8281
throw new UnsupportedImageTypeException;
8382
}
8483

Classes/PHPWord/Shared/File.php

Lines changed: 35 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -33,12 +33,11 @@ class PHPWord_Shared_File
3333
/**
3434
* Verify if a file exists
3535
*
36-
* @param string $pFilename Filename
36+
* @param string $pFilename Filename
3737
* @return bool
3838
*/
3939
public static function file_exists($pFilename)
4040
{
41-
// Regular file_exists
4241
return file_exists($pFilename);
4342
}
4443

@@ -50,18 +49,13 @@ public static function file_exists($pFilename)
5049
*/
5150
public static function realpath($pFilename)
5251
{
53-
// Returnvalue
54-
$returnValue = '';
55-
56-
// Try using realpath()
5752
$returnValue = realpath($pFilename);
5853

59-
// Found something?
60-
if ($returnValue == '' || is_null($returnValue)) {
54+
if (!$returnValue) {
6155
$pathArray = explode('/', $pFilename);
62-
while (in_array('..', $pathArray) && $pathArray[0] != '..') {
56+
while (in_array('..', $pathArray) && $pathArray[0] !== '..') {
6357
for ($i = 0; $i < count($pathArray); ++$i) {
64-
if ($pathArray[$i] == '..' && $i > 0) {
58+
if ($pathArray[$i] === '..' && $i > 0) {
6559
unset($pathArray[$i]);
6660
unset($pathArray[$i - 1]);
6761
break;
@@ -71,7 +65,36 @@ public static function realpath($pFilename)
7165
$returnValue = implode('/', $pathArray);
7266
}
7367

74-
// Return
7568
return $returnValue;
7669
}
77-
}
70+
71+
/**
72+
* PHP Words version of exif_imagetype to return the Image Type from a file
73+
*
74+
* @param string $filename
75+
* @return int|bool
76+
*/
77+
public static function PHPWord_imagetype($filename)
78+
{
79+
if ((list($width, $height, $type, $attr) = getimagesize($filename)) !== false) {
80+
return $type;
81+
}
82+
return false;
83+
}
84+
85+
/**
86+
* Return the Image Type from a file
87+
*
88+
* @param string $filename
89+
* @return int|bool
90+
*/
91+
public static function imagetype($filename)
92+
{
93+
if (function_exists('exif_imagetype')) {
94+
return exif_imagetype($filename);
95+
} else {
96+
return self::PHPWord_imagetype($filename);
97+
}
98+
return false;
99+
}
100+
}

Classes/PHPWord/Writer/Word2007.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -212,7 +212,7 @@ private function checkContentTypes($src)
212212
if (stripos(strrev($src), strrev('.php')) === 0) {
213213
$extension = 'php';
214214
} else {
215-
$imageType = exif_imagetype($src);
215+
$imageType = PHPWord_Shared_File::imagetype($src);
216216
if ($imageType === IMAGETYPE_JPEG) {
217217
$extension = 'jpg';
218218
} elseif ($imageType === IMAGETYPE_GIF) {

0 commit comments

Comments
 (0)