Skip to content

Commit

Permalink
Add function to check for image type.
Browse files Browse the repository at this point in the history
Some servers do not have exif_imagetype available. So I added a work around.
  • Loading branch information
hskrtich authored and gabrielbull committed Mar 16, 2014
1 parent 04623f2 commit 745f3c8
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 16 deletions.
2 changes: 1 addition & 1 deletion Classes/PHPWord/Media.php
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ public static function addSectionMediaElement($src, $type, PHPWord_Section_Memor
$media['createfunction'] = $memoryImage->getImageCreateFunction();
$media['imagefunction'] = $memoryImage->getImageFunction();
} else {
$imageType = exif_imagetype($src);
$imageType = PHPWord_Shared_File::imagetype($src);
if ($imageType === IMAGETYPE_JPEG) {
$extension = 'jpg';
} elseif ($imageType === IMAGETYPE_GIF) {
Expand Down
3 changes: 1 addition & 2 deletions Classes/PHPWord/Section/Image.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,6 @@ class PHPWord_Section_Image
*/
private $_isWatermark;


/**
* Create a new Image
*
Expand All @@ -78,7 +77,7 @@ public function __construct($src, $style = null, $isWatermark = false)
throw new InvalidImageException;
}

if (!in_array(exif_imagetype($src), $supportedImageTypes)) {
if (!in_array(PHPWord_Shared_File::imagetype($src), $supportedImageTypes)) {
throw new UnsupportedImageTypeException;
}

Expand Down
47 changes: 35 additions & 12 deletions Classes/PHPWord/Shared/File.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,11 @@ class PHPWord_Shared_File
/**
* Verify if a file exists
*
* @param string $pFilename Filename
* @param string $pFilename Filename
* @return bool
*/
public static function file_exists($pFilename)
{
// Regular file_exists
return file_exists($pFilename);
}

Expand All @@ -50,18 +49,13 @@ public static function file_exists($pFilename)
*/
public static function realpath($pFilename)
{
// Returnvalue
$returnValue = '';

// Try using realpath()
$returnValue = realpath($pFilename);

// Found something?
if ($returnValue == '' || is_null($returnValue)) {
if (!$returnValue) {
$pathArray = explode('/', $pFilename);
while (in_array('..', $pathArray) && $pathArray[0] != '..') {
while (in_array('..', $pathArray) && $pathArray[0] !== '..') {
for ($i = 0; $i < count($pathArray); ++$i) {
if ($pathArray[$i] == '..' && $i > 0) {
if ($pathArray[$i] === '..' && $i > 0) {
unset($pathArray[$i]);
unset($pathArray[$i - 1]);
break;
Expand All @@ -71,7 +65,36 @@ public static function realpath($pFilename)
$returnValue = implode('/', $pathArray);
}

// Return
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;
}
}
2 changes: 1 addition & 1 deletion Classes/PHPWord/Writer/Word2007.php
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,7 @@ private function checkContentTypes($src)
if (stripos(strrev($src), strrev('.php')) === 0) {
$extension = 'php';
} else {
$imageType = exif_imagetype($src);
$imageType = PHPWord_Shared_File::imagetype($src);
if ($imageType === IMAGETYPE_JPEG) {
$extension = 'jpg';
} elseif ($imageType === IMAGETYPE_GIF) {
Expand Down

0 comments on commit 745f3c8

Please sign in to comment.