Skip to content

Commit

Permalink
Added test case for image type detection
Browse files Browse the repository at this point in the history
Conflicts:
	changelog.txt
  • Loading branch information
gabrielbull committed Mar 16, 2014
1 parent 745f3c8 commit 74566e3
Show file tree
Hide file tree
Showing 8 changed files with 87 additions and 45 deletions.
14 changes: 1 addition & 13 deletions Classes/PHPWord/Media.php
Original file line number Diff line number Diff line change
Expand Up @@ -94,25 +94,13 @@ public static function addSectionMediaElement($src, $type, PHPWord_Section_Memor
if (!$isMemImage) {
$isMemImage = (filter_var($src, FILTER_VALIDATE_URL) !== false);
}
$extension = '';
if ($isMemImage) {
$extension = $memoryImage->getImageExtension();
$media['isMemImage'] = true;
$media['createfunction'] = $memoryImage->getImageCreateFunction();
$media['imagefunction'] = $memoryImage->getImageFunction();
} else {
$imageType = PHPWord_Shared_File::imagetype($src);
if ($imageType === IMAGETYPE_JPEG) {
$extension = 'jpg';
} elseif ($imageType === IMAGETYPE_GIF) {
$extension = 'gif';
} elseif ($imageType === IMAGETYPE_PNG) {
$extension = 'png';
} elseif ($imageType === IMAGETYPE_BMP) {
$extension = 'bmp';
} elseif ($imageType === IMAGETYPE_TIFF_II || $imageType === IMAGETYPE_TIFF_MM) {
$extension = 'tif';
}
$extension = PHPWord_Shared_File::imagetype($src);
}

$folder = 'media';
Expand Down
4 changes: 1 addition & 3 deletions Classes/PHPWord/Section/Image.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,13 +71,11 @@ class PHPWord_Section_Image
*/
public function __construct($src, $style = null, $isWatermark = false)
{
$supportedImageTypes = array(IMAGETYPE_JPEG, IMAGETYPE_GIF, IMAGETYPE_PNG, IMAGETYPE_BMP, IMAGETYPE_TIFF_II, IMAGETYPE_TIFF_MM);

if (!file_exists($src)) {
throw new InvalidImageException;
}

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

Expand Down
42 changes: 35 additions & 7 deletions Classes/PHPWord/Shared/File.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,12 @@
*/
class PHPWord_Shared_File
{
const IMAGETYPE_JPEG = 'jpg';
const IMAGETYPE_GIF = 'gif';
const IMAGETYPE_PNG = 'png';
const IMAGETYPE_BMP = 'bmp';
const IMAGETYPE_TIFF = 'tif';

/**
* Verify if a file exists
*
Expand Down Expand Up @@ -74,10 +80,20 @@ public static function realpath($pFilename)
* @param string $filename
* @return int|bool
*/
public static function PHPWord_imagetype($filename)
private static function fallbackImagetype($filename)
{
if ((list($width, $height, $type, $attr) = getimagesize($filename)) !== false) {
return $type;
if ($type === 2) {
return self::IMAGETYPE_JPEG;
} elseif ($type === 1) {
return self::IMAGETYPE_GIF;
} elseif ($type === 3) {
return self::IMAGETYPE_PNG;
} elseif ($type === 6) {
return self::IMAGETYPE_BMP;
} elseif ($type === 7 || $type === 8) {
return self::IMAGETYPE_TIFF;
}
}
return false;
}
Expand All @@ -86,14 +102,26 @@ public static function PHPWord_imagetype($filename)
* Return the Image Type from a file
*
* @param string $filename
* @param bool $userFallbackFunction
* @return int|bool
*/
public static function imagetype($filename)
public static function imagetype($filename, $userFallbackFunction = false)
{
if (function_exists('exif_imagetype')) {
return exif_imagetype($filename);
} else {
return self::PHPWord_imagetype($filename);
if ($userFallbackFunction || !function_exists('exif_imagetype')) {
return self::fallbackImagetype($filename);
}

$imagetype = exif_imagetype($filename);
if ($imagetype === IMAGETYPE_JPEG) {
return self::IMAGETYPE_JPEG;
} elseif ($imagetype === IMAGETYPE_GIF) {
return self::IMAGETYPE_GIF;
} elseif ($imagetype === IMAGETYPE_PNG) {
return self::IMAGETYPE_PNG;
} elseif ($imagetype === IMAGETYPE_BMP) {
return self::IMAGETYPE_BMP;
} elseif ($imagetype === IMAGETYPE_TIFF_II || $imagetype === IMAGETYPE_TIFF_MM) {
return self::IMAGETYPE_TIFF;
}
return false;
}
Expand Down
21 changes: 4 additions & 17 deletions Classes/PHPWord/Writer/Word2007.php
Original file line number Diff line number Diff line change
Expand Up @@ -212,21 +212,10 @@ private function checkContentTypes($src)
if (stripos(strrev($src), strrev('.php')) === 0) {
$extension = 'php';
} else {
$imageType = PHPWord_Shared_File::imagetype($src);
if ($imageType === IMAGETYPE_JPEG) {
$extension = 'jpg';
} elseif ($imageType === IMAGETYPE_GIF) {
$extension = 'gif';
} elseif ($imageType === IMAGETYPE_PNG) {
$extension = 'png';
} elseif ($imageType === IMAGETYPE_BMP) {
$extension = 'bmp';
} elseif ($imageType === IMAGETYPE_TIFF_II || $imageType === IMAGETYPE_TIFF_MM) {
$extension = 'tif';
}
$extension = PHPWord_Shared_File::imagetype($src);
}

if (isset($extension)) {
if (isset($extension) && $extension) {
$imageData = getimagesize($src);
$imageType = image_type_to_mime_type($imageData[2]);
$imageExtension = str_replace('.', '', image_type_to_extension($imageData[2]));
Expand All @@ -236,10 +225,8 @@ private function checkContentTypes($src)
if (!in_array($imageType, $this->_imageTypes)) {
$this->_imageTypes[$imageExtension] = $imageType;
}
} else {
if (!in_array($extension, $this->_objectTypes)) {
$this->_objectTypes[] = $extension;
}
} elseif (!in_array($extension, $this->_objectTypes)) {
$this->_objectTypes[] = $extension;
}
}

Expand Down
37 changes: 36 additions & 1 deletion Tests/PHPWord/Shared/FileTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,4 +54,39 @@ public function testRealpath()
$expected = $dir . DIRECTORY_SEPARATOR . $file;
$this->assertEquals($expected, PHPWord_Shared_File::realpath($file));
}
}

/**
* @covers PHPWord_Shared_File::imagetype
* @covers PHPWord_Shared_File::fallbackImagetype
*/
public function testImagetype()
{
$filename = PHPWORD_TESTS_DIR_ROOT . "/_files/images/mars_noext_jpg";
$this->assertEquals(PHPWord_Shared_File::IMAGETYPE_JPEG, PHPWord_Shared_File::imagetype($filename, true));
$this->assertEquals(PHPWord_Shared_File::IMAGETYPE_JPEG, PHPWord_Shared_File::imagetype($filename));

$filename = PHPWORD_TESTS_DIR_ROOT . "/_files/images/mars.jpg";
$this->assertEquals(PHPWord_Shared_File::IMAGETYPE_JPEG, PHPWord_Shared_File::imagetype($filename, true));
$this->assertEquals(PHPWord_Shared_File::IMAGETYPE_JPEG, PHPWord_Shared_File::imagetype($filename));

$filename = PHPWORD_TESTS_DIR_ROOT . "/_files/images/mario.gif";
$this->assertEquals(PHPWord_Shared_File::IMAGETYPE_GIF, PHPWord_Shared_File::imagetype($filename, true));
$this->assertEquals(PHPWord_Shared_File::IMAGETYPE_GIF, PHPWord_Shared_File::imagetype($filename));

$filename = PHPWORD_TESTS_DIR_ROOT . "/_files/images/firefox.png";
$this->assertEquals(PHPWord_Shared_File::IMAGETYPE_PNG, PHPWord_Shared_File::imagetype($filename, true));
$this->assertEquals(PHPWord_Shared_File::IMAGETYPE_PNG, PHPWord_Shared_File::imagetype($filename));

$filename = PHPWORD_TESTS_DIR_ROOT . "/_files/images/duke_nukem.bmp";
$this->assertEquals(PHPWord_Shared_File::IMAGETYPE_BMP, PHPWord_Shared_File::imagetype($filename, true));
$this->assertEquals(PHPWord_Shared_File::IMAGETYPE_BMP, PHPWord_Shared_File::imagetype($filename));

$filename = PHPWORD_TESTS_DIR_ROOT . "/_files/images/angela_merkel.tif";
$this->assertEquals(PHPWord_Shared_File::IMAGETYPE_TIFF, PHPWord_Shared_File::imagetype($filename, true));
$this->assertEquals(PHPWord_Shared_File::IMAGETYPE_TIFF, PHPWord_Shared_File::imagetype($filename));

$filename = PHPWORD_TESTS_DIR_ROOT . "/_files/images/alexz-johnson.pcx";
$this->assertFalse(PHPWord_Shared_File::imagetype($filename, true));
$this->assertFalse(PHPWord_Shared_File::imagetype($filename));
}
}
2 changes: 1 addition & 1 deletion Tests/PHPWord/Writer/Word2007/DocumentTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,4 +84,4 @@ public function testElements()
$element = $doc->getElement('/w:document/w:body/w:p[11]/w:r/w:object/o:OLEObject');
$this->assertEquals('Embed', $element->getAttribute('Type'));
}
}
}
9 changes: 6 additions & 3 deletions Tests/_inc/TestHelperDOCX.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,14 @@

class TestHelperDOCX
{
/** @var string $file */
static protected $file;
/**
* @var string
*/
protected static $file;

/**
* @param \PHPWord $PHPWord
* @param string $writer
* @return \PHPWord\Tests\XmlDocument
*/
public static function getDocument(PHPWord $PHPWord, $writer = 'Word2007')
Expand Down Expand Up @@ -67,4 +70,4 @@ public static function getFile()
{
return self::$file;
}
}
}
3 changes: 3 additions & 0 deletions changelog.txt
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@
* @version ##VERSION##, ##DATE##
**************************************************************************************

Changes in branch for release 0.8.1 :
- Feature: (bskrtich, gabrielbull) - Added fallback for computers that do not have exif_imagetype

Changes in branch for release 0.8.0 :
- Bugfix: (gabrielbull) - Fixed bug with cell styling
- Bugfix: (gabrielbull) - Fixed bug list items inside of cells
Expand Down

0 comments on commit 74566e3

Please sign in to comment.