Merge pull request #108 from gabrielbull/develop

Use exif_imagetype to check image format instead of extension name
This commit is contained in:
Progi1984 2014-03-11 21:57:22 +01:00
commit 595bcc33fa
7 changed files with 86 additions and 27 deletions

View 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
{
}

View 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
{
}

View File

@ -25,12 +25,14 @@
* @version 0.7.0
*/
use PhpOffice\PhpWord\Exceptions\InvalidImageException;
use PhpOffice\PhpWord\Exceptions\UnsupportedImageTypeException;
/**
* Class PHPWord_Section_Image
*/
class PHPWord_Section_Image
{
/**
* Image Src
*
@ -64,42 +66,43 @@ class PHPWord_Section_Image
* Create a new Image
*
* @param string $src
* @param mixed style
* @param mixed $style
* @param bool $isWatermark
* @throws InvalidImageException|UnsupportedImageTypeException
*/
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);
$ext = strtolower($inf['extension']);
if (!file_exists($src)) {
throw new InvalidImageException;
}
if (file_exists($src) && in_array($ext, $_supportedImageTypes)) {
$this->_src = $src;
$this->_isWatermark = $isWatermark;
$this->_style = new PHPWord_Style_Image();
if (!in_array(exif_imagetype($src), $supportedImageTypes)) {
throw new UnsupportedImageTypeException;
}
if (!is_null($style) && is_array($style)) {
foreach ($style as $key => $value) {
if (substr($key, 0, 1) != '_') {
$key = '_' . $key;
}
$this->_style->setStyleValue($key, $value);
$this->_src = $src;
$this->_isWatermark = $isWatermark;
$this->_style = new PHPWord_Style_Image();
if (!is_null($style) && is_array($style)) {
foreach ($style as $key => $value) {
if (substr($key, 0, 1) != '_') {
$key = '_' . $key;
}
$this->_style->setStyleValue($key, $value);
}
}
if (isset($style['wrappingStyle'])) {
$this->_style->setWrappingStyle($style['wrappingStyle']);
}
if (isset($style['wrappingStyle'])) {
$this->_style->setWrappingStyle($style['wrappingStyle']);
}
if ($this->_style->getWidth() == null && $this->_style->getHeight() == null) {
$imgData = getimagesize($this->_src);
$this->_style->setWidth($imgData[0]);
$this->_style->setHeight($imgData[1]);
}
return $this;
} else {
return false;
if ($this->_style->getWidth() == null && $this->_style->getHeight() == null) {
$imgData = getimagesize($this->_src);
$this->_style->setWidth($imgData[0]);
$this->_style->setHeight($imgData[1]);
}
}

View File

@ -33,6 +33,32 @@ class ImageTest extends \PHPUnit_Framework_TestCase
$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()
{
$oImage = new PHPWord_Section_Image(\join(

Binary file not shown.

Binary file not shown.

Binary file not shown.

After

Width:  |  Height:  |  Size: 24 KiB