Merge pull request #108 from gabrielbull/develop
Use exif_imagetype to check image format instead of extension name
This commit is contained in:
commit
595bcc33fa
15
Classes/PHPWord/Exceptions/InvalidImageException.php
Normal file
15
Classes/PHPWord/Exceptions/InvalidImageException.php
Normal file
@ -0,0 +1,15 @@
|
|||||||
|
<?php
|
||||||
|
namespace PhpOffice\PhpWord\Exceptions;
|
||||||
|
|
||||||
|
use Exception;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* InvalidImageException
|
||||||
|
*
|
||||||
|
* Exception used for when an image is not found
|
||||||
|
*
|
||||||
|
* @package PHPWord
|
||||||
|
*/
|
||||||
|
class InvalidImageException extends Exception
|
||||||
|
{
|
||||||
|
}
|
||||||
15
Classes/PHPWord/Exceptions/UnsupportedImageTypeException.php
Normal file
15
Classes/PHPWord/Exceptions/UnsupportedImageTypeException.php
Normal file
@ -0,0 +1,15 @@
|
|||||||
|
<?php
|
||||||
|
namespace PhpOffice\PhpWord\Exceptions;
|
||||||
|
|
||||||
|
use Exception;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* UnsupportedImageTypeException
|
||||||
|
*
|
||||||
|
* Exception used for when an image type is unsupported
|
||||||
|
*
|
||||||
|
* @package PHPWord
|
||||||
|
*/
|
||||||
|
class UnsupportedImageTypeException extends Exception
|
||||||
|
{
|
||||||
|
}
|
||||||
@ -25,12 +25,14 @@
|
|||||||
* @version 0.7.0
|
* @version 0.7.0
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
use PhpOffice\PhpWord\Exceptions\InvalidImageException;
|
||||||
|
use PhpOffice\PhpWord\Exceptions\UnsupportedImageTypeException;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Class PHPWord_Section_Image
|
* Class PHPWord_Section_Image
|
||||||
*/
|
*/
|
||||||
class PHPWord_Section_Image
|
class PHPWord_Section_Image
|
||||||
{
|
{
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Image Src
|
* Image Src
|
||||||
*
|
*
|
||||||
@ -64,16 +66,22 @@ class PHPWord_Section_Image
|
|||||||
* Create a new Image
|
* Create a new Image
|
||||||
*
|
*
|
||||||
* @param string $src
|
* @param string $src
|
||||||
* @param mixed style
|
* @param mixed $style
|
||||||
|
* @param bool $isWatermark
|
||||||
|
* @throws InvalidImageException|UnsupportedImageTypeException
|
||||||
*/
|
*/
|
||||||
public function __construct($src, $style = null, $isWatermark = false)
|
public function __construct($src, $style = null, $isWatermark = false)
|
||||||
{
|
{
|
||||||
$_supportedImageTypes = array('jpg', 'jpeg', 'gif', 'png', 'bmp', 'tif', 'tiff');
|
$supportedImageTypes = array(IMAGETYPE_JPEG, IMAGETYPE_GIF, IMAGETYPE_PNG, IMAGETYPE_BMP, IMAGETYPE_TIFF_II, IMAGETYPE_TIFF_MM);
|
||||||
|
|
||||||
$inf = pathinfo($src);
|
if (!file_exists($src)) {
|
||||||
$ext = strtolower($inf['extension']);
|
throw new InvalidImageException;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!in_array(exif_imagetype($src), $supportedImageTypes)) {
|
||||||
|
throw new UnsupportedImageTypeException;
|
||||||
|
}
|
||||||
|
|
||||||
if (file_exists($src) && in_array($ext, $_supportedImageTypes)) {
|
|
||||||
$this->_src = $src;
|
$this->_src = $src;
|
||||||
$this->_isWatermark = $isWatermark;
|
$this->_isWatermark = $isWatermark;
|
||||||
$this->_style = new PHPWord_Style_Image();
|
$this->_style = new PHPWord_Style_Image();
|
||||||
@ -96,11 +104,6 @@ class PHPWord_Section_Image
|
|||||||
$this->_style->setWidth($imgData[0]);
|
$this->_style->setWidth($imgData[0]);
|
||||||
$this->_style->setHeight($imgData[1]);
|
$this->_style->setHeight($imgData[1]);
|
||||||
}
|
}
|
||||||
|
|
||||||
return $this;
|
|
||||||
} else {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@ -33,6 +33,32 @@ class ImageTest extends \PHPUnit_Framework_TestCase
|
|||||||
$this->assertInstanceOf('PHPWord_Style_Image', $oImage->getStyle());
|
$this->assertInstanceOf('PHPWord_Style_Image', $oImage->getStyle());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function testValidImageTypes()
|
||||||
|
{
|
||||||
|
new PHPWord_Section_Image(PHPWORD_TESTS_DIR_ROOT . "/_files/images/mars_noext_jpg");
|
||||||
|
new PHPWord_Section_Image(PHPWORD_TESTS_DIR_ROOT . "/_files/images/mars.jpg");
|
||||||
|
new PHPWord_Section_Image(PHPWORD_TESTS_DIR_ROOT . "/_files/images/mario.gif");
|
||||||
|
new PHPWord_Section_Image(PHPWORD_TESTS_DIR_ROOT . "/_files/images/firefox.png");
|
||||||
|
new PHPWord_Section_Image(PHPWORD_TESTS_DIR_ROOT . "/_files/images/duke_nukem.bmp");
|
||||||
|
new PHPWord_Section_Image(PHPWORD_TESTS_DIR_ROOT . "/_files/images/angela_merkel.tif");
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @expectedException \PhpOffice\PhpWord\Exceptions\InvalidImageException
|
||||||
|
*/
|
||||||
|
public function testImageNotFound()
|
||||||
|
{
|
||||||
|
new PHPWord_Section_Image(PHPWORD_TESTS_DIR_ROOT . "/_files/images/thisisnotarealimage");
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @expectedException \PhpOffice\PhpWord\Exceptions\UnsupportedImageTypeException
|
||||||
|
*/
|
||||||
|
public function testInvalidImageTypes()
|
||||||
|
{
|
||||||
|
new PHPWord_Section_Image(PHPWORD_TESTS_DIR_ROOT . "/_files/images/alexz-johnson.pcx");
|
||||||
|
}
|
||||||
|
|
||||||
public function testStyle()
|
public function testStyle()
|
||||||
{
|
{
|
||||||
$oImage = new PHPWord_Section_Image(\join(
|
$oImage = new PHPWord_Section_Image(\join(
|
||||||
|
|||||||
BIN
Tests/_files/images/alexz-johnson.pcx
Normal file
BIN
Tests/_files/images/alexz-johnson.pcx
Normal file
Binary file not shown.
BIN
Tests/_files/images/angela_merkel.tif
Normal file
BIN
Tests/_files/images/angela_merkel.tif
Normal file
Binary file not shown.
BIN
Tests/_files/images/mars_noext_jpg
Normal file
BIN
Tests/_files/images/mars_noext_jpg
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 24 KiB |
Loading…
x
Reference in New Issue
Block a user