Skip to content

Commit a3c572d

Browse files
author
Mitch
committed
Refactor createFromString() and drop loadFromString(), update tests related to same
1 parent 4b9dd89 commit a3c572d

File tree

2 files changed

+9
-56
lines changed

2 files changed

+9
-56
lines changed

src/ImageResize.php

Lines changed: 6 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -45,11 +45,10 @@ class ImageResize
4545
* @return ImageResize
4646
* @throws \exception
4747
*/
48-
public static function createFromString($imageData)
48+
public static function createFromString($image_data)
4949
{
50-
$s = new self();
51-
$s->loadFromString($imageData);
52-
return $s;
50+
$resize = new self('data://application/octet-stream;base64,' . base64_encode($image_data));
51+
return $resize;
5352
}
5453

5554
/**
@@ -58,55 +57,9 @@ public static function createFromString($imageData)
5857
* @param string|null $filename
5958
* @throws \Exception
6059
*/
61-
public function __construct($filename = null)
60+
public function __construct($filename)
6261
{
63-
if ($filename !== null) {
64-
$this->loadFromFile($filename);
65-
} else {
66-
// if no filename is provided, we want to throw an exception if
67-
// the object was not created in one of it's static method
68-
$backtrace = debug_backtrace();
69-
70-
if (!isset($backtrace[1]['class']) || $backtrace[1]['class'] != __CLASS__) {
71-
throw new \Exception('No image provided');
72-
}
73-
}
74-
}
75-
76-
/**
77-
* Load image from string
78-
*
79-
* @param string $imagedata
80-
* @return ImageResize
81-
* @throws \Exception
82-
*/
83-
public function loadFromString($imagedata)
84-
{
85-
$image_info = @getimagesize('data://application/octet-stream;base64,' . base64_encode($imagedata));
86-
87-
if (!$image_info) {
88-
throw new \Exception('Could not load image from string');
89-
}
90-
91-
list (
92-
$this->original_w,
93-
$this->original_h,
94-
$this->source_type
95-
) = $image_info;
96-
97-
switch ($this->source_type) {
98-
case IMAGETYPE_GIF:
99-
case IMAGETYPE_JPEG:
100-
case IMAGETYPE_PNG:
101-
$this->source_image = imagecreatefromstring($imagedata);
102-
break;
103-
104-
default:
105-
throw new \Exception('Unsupported image type');
106-
break;
107-
}
108-
109-
return $this->resize($this->getSourceWidth(), $this->getSourceHeight());
62+
$this->loadFromFile($filename);
11063
}
11164

11265
/**
@@ -121,7 +74,7 @@ public function loadFromFile($filename)
12174
$image_info = @getimagesize($filename);
12275

12376
if (!$image_info) {
124-
throw new \Exception('Could not read ' . ($filename ?: 'file'));
77+
throw new \Exception('Could not read file');
12578
}
12679

12780
list (

test/Test.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ public function testLoadString()
6363

6464
/**
6565
* @expectedException Exception
66-
* @expectedExceptionMessage No image provided
66+
* @expectedExceptionMessage Could not read file
6767
*/
6868
public function testLoadNoFile()
6969
{
@@ -72,7 +72,7 @@ public function testLoadNoFile()
7272

7373
/**
7474
* @expectedException Exception
75-
* @expectedExceptionMessage Could not read
75+
* @expectedExceptionMessage Could not read file
7676
*/
7777
public function testLoadUnsupportedFile()
7878
{
@@ -81,7 +81,7 @@ public function testLoadUnsupportedFile()
8181

8282
/**
8383
* @expectedException Exception
84-
* @expectedExceptionMessage Could not load
84+
* @expectedExceptionMessage Could not read file
8585
*/
8686
public function testLoadUnsupportedFileString()
8787
{

0 commit comments

Comments
 (0)