Skip to content

Commit 595bcc3

Browse files
committed
Merge pull request #108 from gabrielbull/develop
Use exif_imagetype to check image format instead of extension name
2 parents da7c0ad + 77f52e9 commit 595bcc3

File tree

7 files changed

+89
-30
lines changed

7 files changed

+89
-30
lines changed
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php
2+
namespace PhpOffice\PhpWord\Exceptions;
3+
4+
use Exception;
5+
6+
/**
7+
* InvalidImageException
8+
*
9+
* Exception used for when an image is not found
10+
*
11+
* @package PHPWord
12+
*/
13+
class InvalidImageException extends Exception
14+
{
15+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php
2+
namespace PhpOffice\PhpWord\Exceptions;
3+
4+
use Exception;
5+
6+
/**
7+
* UnsupportedImageTypeException
8+
*
9+
* Exception used for when an image type is unsupported
10+
*
11+
* @package PHPWord
12+
*/
13+
class UnsupportedImageTypeException extends Exception
14+
{
15+
}

Classes/PHPWord/Section/Image.php

Lines changed: 33 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,14 @@
2525
* @version 0.7.0
2626
*/
2727

28+
use PhpOffice\PhpWord\Exceptions\InvalidImageException;
29+
use PhpOffice\PhpWord\Exceptions\UnsupportedImageTypeException;
30+
2831
/**
2932
* Class PHPWord_Section_Image
3033
*/
3134
class PHPWord_Section_Image
3235
{
33-
3436
/**
3537
* Image Src
3638
*
@@ -64,42 +66,43 @@ class PHPWord_Section_Image
6466
* Create a new Image
6567
*
6668
* @param string $src
67-
* @param mixed style
69+
* @param mixed $style
70+
* @param bool $isWatermark
71+
* @throws InvalidImageException|UnsupportedImageTypeException
6872
*/
6973
public function __construct($src, $style = null, $isWatermark = false)
7074
{
71-
$_supportedImageTypes = array('jpg', 'jpeg', 'gif', 'png', 'bmp', 'tif', 'tiff');
72-
73-
$inf = pathinfo($src);
74-
$ext = strtolower($inf['extension']);
75-
76-
if (file_exists($src) && in_array($ext, $_supportedImageTypes)) {
77-
$this->_src = $src;
78-
$this->_isWatermark = $isWatermark;
79-
$this->_style = new PHPWord_Style_Image();
80-
81-
if (!is_null($style) && is_array($style)) {
82-
foreach ($style as $key => $value) {
83-
if (substr($key, 0, 1) != '_') {
84-
$key = '_' . $key;
85-
}
86-
$this->_style->setStyleValue($key, $value);
87-
}
88-
}
75+
$supportedImageTypes = array(IMAGETYPE_JPEG, IMAGETYPE_GIF, IMAGETYPE_PNG, IMAGETYPE_BMP, IMAGETYPE_TIFF_II, IMAGETYPE_TIFF_MM);
8976

90-
if (isset($style['wrappingStyle'])) {
91-
$this->_style->setWrappingStyle($style['wrappingStyle']);
92-
}
77+
if (!file_exists($src)) {
78+
throw new InvalidImageException;
79+
}
9380

94-
if ($this->_style->getWidth() == null && $this->_style->getHeight() == null) {
95-
$imgData = getimagesize($this->_src);
96-
$this->_style->setWidth($imgData[0]);
97-
$this->_style->setHeight($imgData[1]);
81+
if (!in_array(exif_imagetype($src), $supportedImageTypes)) {
82+
throw new UnsupportedImageTypeException;
83+
}
84+
85+
$this->_src = $src;
86+
$this->_isWatermark = $isWatermark;
87+
$this->_style = new PHPWord_Style_Image();
88+
89+
if (!is_null($style) && is_array($style)) {
90+
foreach ($style as $key => $value) {
91+
if (substr($key, 0, 1) != '_') {
92+
$key = '_' . $key;
93+
}
94+
$this->_style->setStyleValue($key, $value);
9895
}
96+
}
97+
98+
if (isset($style['wrappingStyle'])) {
99+
$this->_style->setWrappingStyle($style['wrappingStyle']);
100+
}
99101

100-
return $this;
101-
} else {
102-
return false;
102+
if ($this->_style->getWidth() == null && $this->_style->getHeight() == null) {
103+
$imgData = getimagesize($this->_src);
104+
$this->_style->setWidth($imgData[0]);
105+
$this->_style->setHeight($imgData[1]);
103106
}
104107
}
105108

Tests/PHPWord/Section/ImageTest.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,32 @@ public function testConstructWithStyle()
3333
$this->assertInstanceOf('PHPWord_Style_Image', $oImage->getStyle());
3434
}
3535

36+
public function testValidImageTypes()
37+
{
38+
new PHPWord_Section_Image(PHPWORD_TESTS_DIR_ROOT . "/_files/images/mars_noext_jpg");
39+
new PHPWord_Section_Image(PHPWORD_TESTS_DIR_ROOT . "/_files/images/mars.jpg");
40+
new PHPWord_Section_Image(PHPWORD_TESTS_DIR_ROOT . "/_files/images/mario.gif");
41+
new PHPWord_Section_Image(PHPWORD_TESTS_DIR_ROOT . "/_files/images/firefox.png");
42+
new PHPWord_Section_Image(PHPWORD_TESTS_DIR_ROOT . "/_files/images/duke_nukem.bmp");
43+
new PHPWord_Section_Image(PHPWORD_TESTS_DIR_ROOT . "/_files/images/angela_merkel.tif");
44+
}
45+
46+
/**
47+
* @expectedException \PhpOffice\PhpWord\Exceptions\InvalidImageException
48+
*/
49+
public function testImageNotFound()
50+
{
51+
new PHPWord_Section_Image(PHPWORD_TESTS_DIR_ROOT . "/_files/images/thisisnotarealimage");
52+
}
53+
54+
/**
55+
* @expectedException \PhpOffice\PhpWord\Exceptions\UnsupportedImageTypeException
56+
*/
57+
public function testInvalidImageTypes()
58+
{
59+
new PHPWord_Section_Image(PHPWORD_TESTS_DIR_ROOT . "/_files/images/alexz-johnson.pcx");
60+
}
61+
3662
public function testStyle()
3763
{
3864
$oImage = new PHPWord_Section_Image(\join(

Tests/_files/images/alexz-johnson.pcx

283 KB
Binary file not shown.

Tests/_files/images/angela_merkel.tif

373 KB
Binary file not shown.

Tests/_files/images/mars_noext_jpg

23.9 KB
Binary file not shown.

0 commit comments

Comments
 (0)