Skip to content

Commit 4536668

Browse files
Merge pull request #83 from christof-b/master
Improved file validation and error handling.
2 parents b2189fb + c224860 commit 4536668

File tree

3 files changed

+32
-8
lines changed

3 files changed

+32
-8
lines changed

lib/ImageResize.php

+20-5
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@ class ImageResize
3939

4040
protected $source_w;
4141
protected $source_h;
42+
43+
protected $source_info;
4244

4345
/**
4446
* Create instance from a strng
@@ -49,6 +51,9 @@ class ImageResize
4951
*/
5052
public static function createFromString($image_data)
5153
{
54+
if(empty($image_data) || $image_data === null) {
55+
throw new ImageResizeException('image_data must not be empty');
56+
}
5257
$resize = new self('data://application/octet-stream;base64,' . base64_encode($image_data));
5358
return $resize;
5459
}
@@ -62,7 +67,17 @@ public static function createFromString($image_data)
6267
*/
6368
public function __construct($filename)
6469
{
65-
$image_info = @getimagesize($filename);
70+
71+
if($filename === null || empty($filename) || (substr($filename,0,7) !== 'data://' && !is_file($filename))) {
72+
throw new ImageResizeException('File does not exist');
73+
}
74+
75+
$finfo = finfo_open(FILEINFO_MIME_TYPE);
76+
if(strstr(finfo_file($finfo, $filename),'image') === false) {
77+
throw new ImageResizeException('Unsupported file type');
78+
}
79+
80+
$image_info = getimagesize($filename,$this->source_info);
6681

6782
if (!$image_info) {
6883
throw new ImageResizeException('Could not read file');
@@ -107,12 +122,12 @@ public function __construct($filename)
107122
// http://stackoverflow.com/a/28819866
108123
public function imageCreateJpegfromExif($filename){
109124
$img = imagecreatefromjpeg($filename);
110-
111-
if (!function_exists('exif_read_data')) {
125+
126+
if (!function_exists('exif_read_data') || !isset($this->source_info['APP1']) || strpos ($this->source_info['APP1'], 'Exif') !== 0) {
112127
return $img;
113128
}
114-
115-
$exif = @exif_read_data($filename);
129+
130+
$exif = exif_read_data($filename);
116131

117132
if (!$exif || !isset($exif['Orientation'])){
118133
return $img;

test/Test.php

+12-3
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,15 @@ public function testLoadJpg()
4343
$this->assertEquals(IMAGETYPE_JPEG, $resize->source_type);
4444
$this->assertInstanceOf('\Eventviva\ImageResize', $resize);
4545
}
46+
47+
public function testLoadIgnoreXmpExifJpg()
48+
{
49+
$image = __DIR__.'/ressources/test_xmp.jpg';
50+
$resize = new ImageResize($image);
51+
52+
$this->assertEquals(IMAGETYPE_JPEG, $resize->source_type);
53+
$this->assertInstanceOf('\Eventviva\ImageResize', $resize);
54+
}
4655

4756
public function testLoadPng()
4857
{
@@ -67,7 +76,7 @@ public function testLoadString()
6776

6877
/**
6978
* @expectedException \Eventviva\ImageResizeException
70-
* @expectedExceptionMessage Could not read file
79+
* @expectedExceptionMessage File does not exist
7180
*/
7281
public function testLoadNoFile()
7382
{
@@ -76,7 +85,7 @@ public function testLoadNoFile()
7685

7786
/**
7887
* @expectedException \Eventviva\ImageResizeException
79-
* @expectedExceptionMessage Could not read file
88+
* @expectedExceptionMessage Unsupported file type
8089
*/
8190
public function testLoadUnsupportedFile()
8291
{
@@ -85,7 +94,7 @@ public function testLoadUnsupportedFile()
8594

8695
/**
8796
* @expectedException \Eventviva\ImageResizeException
88-
* @expectedExceptionMessage Could not read file
97+
* @expectedExceptionMessage image_data must not be empty
8998
*/
9099
public function testLoadUnsupportedFileString()
91100
{

test/ressources/test_xmp.jpg

13.8 KB
Loading

0 commit comments

Comments
 (0)