Add function to check for image type.

Some servers do not have exif_imagetype available. So I added a work around.
This commit is contained in:
Brandon Skrtich 2014-03-16 11:52:56 -06:00 committed by Gabriel Bull
parent 04623f2b03
commit 745f3c81b5
4 changed files with 38 additions and 16 deletions

View File

@ -101,7 +101,7 @@ class PHPWord_Media
$media['createfunction'] = $memoryImage->getImageCreateFunction(); $media['createfunction'] = $memoryImage->getImageCreateFunction();
$media['imagefunction'] = $memoryImage->getImageFunction(); $media['imagefunction'] = $memoryImage->getImageFunction();
} else { } else {
$imageType = exif_imagetype($src); $imageType = PHPWord_Shared_File::imagetype($src);
if ($imageType === IMAGETYPE_JPEG) { if ($imageType === IMAGETYPE_JPEG) {
$extension = 'jpg'; $extension = 'jpg';
} elseif ($imageType === IMAGETYPE_GIF) { } elseif ($imageType === IMAGETYPE_GIF) {

View File

@ -61,7 +61,6 @@ class PHPWord_Section_Image
*/ */
private $_isWatermark; private $_isWatermark;
/** /**
* Create a new Image * Create a new Image
* *
@ -78,7 +77,7 @@ class PHPWord_Section_Image
throw new InvalidImageException; throw new InvalidImageException;
} }
if (!in_array(exif_imagetype($src), $supportedImageTypes)) { if (!in_array(PHPWord_Shared_File::imagetype($src), $supportedImageTypes)) {
throw new UnsupportedImageTypeException; throw new UnsupportedImageTypeException;
} }

View File

@ -33,12 +33,11 @@ class PHPWord_Shared_File
/** /**
* Verify if a file exists * Verify if a file exists
* *
* @param string $pFilename Filename * @param string $pFilename Filename
* @return bool * @return bool
*/ */
public static function file_exists($pFilename) public static function file_exists($pFilename)
{ {
// Regular file_exists
return file_exists($pFilename); return file_exists($pFilename);
} }
@ -50,18 +49,13 @@ class PHPWord_Shared_File
*/ */
public static function realpath($pFilename) public static function realpath($pFilename)
{ {
// Returnvalue
$returnValue = '';
// Try using realpath()
$returnValue = realpath($pFilename); $returnValue = realpath($pFilename);
// Found something? if (!$returnValue) {
if ($returnValue == '' || is_null($returnValue)) {
$pathArray = explode('/', $pFilename); $pathArray = explode('/', $pFilename);
while (in_array('..', $pathArray) && $pathArray[0] != '..') { while (in_array('..', $pathArray) && $pathArray[0] !== '..') {
for ($i = 0; $i < count($pathArray); ++$i) { for ($i = 0; $i < count($pathArray); ++$i) {
if ($pathArray[$i] == '..' && $i > 0) { if ($pathArray[$i] === '..' && $i > 0) {
unset($pathArray[$i]); unset($pathArray[$i]);
unset($pathArray[$i - 1]); unset($pathArray[$i - 1]);
break; break;
@ -71,7 +65,36 @@ class PHPWord_Shared_File
$returnValue = implode('/', $pathArray); $returnValue = implode('/', $pathArray);
} }
// Return
return $returnValue; return $returnValue;
} }
}
/**
* PHP Words version of exif_imagetype to return the Image Type from a file
*
* @param string $filename
* @return int|bool
*/
public static function PHPWord_imagetype($filename)
{
if ((list($width, $height, $type, $attr) = getimagesize($filename)) !== false) {
return $type;
}
return false;
}
/**
* Return the Image Type from a file
*
* @param string $filename
* @return int|bool
*/
public static function imagetype($filename)
{
if (function_exists('exif_imagetype')) {
return exif_imagetype($filename);
} else {
return self::PHPWord_imagetype($filename);
}
return false;
}
}

View File

@ -212,7 +212,7 @@ class PHPWord_Writer_Word2007 implements PHPWord_Writer_IWriter
if (stripos(strrev($src), strrev('.php')) === 0) { if (stripos(strrev($src), strrev('.php')) === 0) {
$extension = 'php'; $extension = 'php';
} else { } else {
$imageType = exif_imagetype($src); $imageType = PHPWord_Shared_File::imagetype($src);
if ($imageType === IMAGETYPE_JPEG) { if ($imageType === IMAGETYPE_JPEG) {
$extension = 'jpg'; $extension = 'jpg';
} elseif ($imageType === IMAGETYPE_GIF) { } elseif ($imageType === IMAGETYPE_GIF) {