Skip to content

Commit 373c468

Browse files
committed
Merge pull request #19 from cainmi/cleanup
Code cleanup and slight refactor
2 parents b2a160d + bc87124 commit 373c468

File tree

2 files changed

+136
-71
lines changed

2 files changed

+136
-71
lines changed

src/ImageResize.php

Lines changed: 68 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,12 @@
99
*/
1010
class ImageResize
1111
{
12-
const cropTOP = 1;
13-
const cropCENTRE = 2;
14-
const cropCENTER = 2;
15-
const cropBOTTOM = 3;
16-
const cropLEFT = 4;
17-
const cropRIGHT = 5;
12+
const CROPTOP = 1;
13+
const CROPCENTRE = 2;
14+
const CROPCENTER = 2;
15+
const CROPBOTTOM = 3;
16+
const CROPLEFT = 4;
17+
const CROPRIGHT = 5;
1818

1919
public $quality_jpg = 75;
2020
public $quality_png = 0;
@@ -45,9 +45,9 @@ class ImageResize
4545
* @return ImageResize
4646
* @throws \Exception
4747
*/
48-
public static function createFromFile($filename){
48+
public static function createFromFile($filename) {
4949
$s = new self();
50-
$s->load($filename);
50+
$s->loadFromFile($filename);
5151
return $s;
5252
}
5353

@@ -58,7 +58,7 @@ public static function createFromFile($filename){
5858
* @return ImageResize
5959
* @throws \exception
6060
*/
61-
public static function createFromString($imageData){
61+
public static function createFromString($imageData) {
6262
$s = new self();
6363
$s->loadFromString($imageData);
6464
return $s;
@@ -70,21 +70,19 @@ public static function createFromString($imageData){
7070
* @param string|null $filename
7171
* @throws \Exception
7272
*/
73-
public function __construct($filename=null)
73+
public function __construct($filename = null)
7474
{
75-
if(!empty($filename)) {
76-
$this->load($filename);
77-
}
78-
}
75+
if ($filename !== null) {
76+
$this->loadFromFile($filename);
77+
} else {
78+
// if no filename is provided, we want to throw an exception if
79+
// the object was not created in one of it's static method
80+
$backtrace = debug_backtrace();
7981

80-
/**
81-
* Get image size from string
82-
*
83-
* @param string $imagedata
84-
* @return array
85-
*/
86-
protected function getImagesizeFromString($imagedata){
87-
return @getimagesize('data://application/octet-stream;base64,' . base64_encode($imagedata));
82+
if (!isset($backtrace[1]['class']) || $backtrace[1]['class'] != __CLASS__) {
83+
throw new \Exception('No image provided');
84+
}
85+
}
8886
}
8987

9088
/**
@@ -96,18 +94,29 @@ protected function getImagesizeFromString($imagedata){
9694
*/
9795
public function loadFromString($imagedata)
9896
{
99-
$image_info = $this->getImagesizeFromString($imagedata);
100-
if(!$image_info) {
97+
$image_info = @getimagesize('data://application/octet-stream;base64,' . base64_encode($imagedata));
98+
99+
if (!$image_info) {
101100
throw new \Exception('Could not load image from string');
102101
}
103102

104103
list (
105104
$this->original_w,
106105
$this->original_h,
107106
$this->source_type
108-
) = $image_info;
107+
) = $image_info;
109108

110-
$this->source_image = imagecreatefromstring($imagedata);
109+
switch ($this->source_type) {
110+
case IMAGETYPE_GIF:
111+
case IMAGETYPE_JPEG:
112+
case IMAGETYPE_PNG:
113+
$this->source_image = imagecreatefromstring($imagedata);
114+
break;
115+
116+
default:
117+
throw new \Exception('Unsupported image type');
118+
break;
119+
}
111120

112121
return $this->resize($this->getSourceWidth(), $this->getSourceHeight());
113122
}
@@ -118,12 +127,12 @@ public function loadFromString($imagedata)
118127
* @return \static
119128
* @throws Exception
120129
*/
121-
public function load($filename)
130+
public function loadFromFile($filename)
122131
{
123-
$image_info = getimagesize($filename);
132+
$image_info = @getimagesize($filename);
124133

125134
if (!$image_info) {
126-
throw new \Exception('Could not read ' . $filename);
135+
throw new \Exception('Could not read ' . ($filename ?: 'file'));
127136
}
128137

129138
list (
@@ -135,18 +144,19 @@ public function load($filename)
135144
switch ($this->source_type) {
136145
case IMAGETYPE_GIF:
137146
$this->source_image = imagecreatefromgif($filename);
138-
break;
147+
break;
139148

140149
case IMAGETYPE_JPEG:
141150
$this->source_image = imagecreatefromjpeg($filename);
142-
break;
151+
break;
143152

144153
case IMAGETYPE_PNG:
145154
$this->source_image = imagecreatefrompng($filename);
146-
break;
155+
break;
147156

148157
default:
149158
throw new \Exception('Unsupported image type');
159+
break;
150160
}
151161

152162
return $this->resize($this->getSourceWidth(), $this->getSourceHeight());
@@ -172,17 +182,17 @@ public function save($filename, $image_type = null, $quality = null, $permission
172182
imagecolortransparent($dest_image, $background);
173183
imagefill($dest_image, 0, 0 , $background);
174184
imagesavealpha($dest_image, true);
175-
break;
185+
break;
176186

177187
case IMAGETYPE_JPEG:
178188
$background = imagecolorallocate($dest_image, 255, 255, 255);
179189
imagefilledrectangle($dest_image, 0, 0, $this->getDestWidth(), $this->getDestHeight(), $background);
180-
break;
190+
break;
181191

182192
case IMAGETYPE_PNG:
183193
imagealphablending($dest_image, false);
184194
imagesavealpha($dest_image, true);
185-
break;
195+
break;
186196
}
187197

188198
imagecopyresampled(
@@ -201,23 +211,23 @@ public function save($filename, $image_type = null, $quality = null, $permission
201211
switch ($image_type) {
202212
case IMAGETYPE_GIF:
203213
imagegif($dest_image, $filename);
204-
break;
214+
break;
205215

206216
case IMAGETYPE_JPEG:
207217
if ($quality === null) {
208218
$quality = $this->quality_jpg;
209219
}
210220

211221
imagejpeg($dest_image, $filename, $quality);
212-
break;
222+
break;
213223

214224
case IMAGETYPE_PNG:
215225
if ($quality === null) {
216226
$quality = $this->quality_png;
217227
}
218228

219229
imagepng($dest_image, $filename, $quality);
220-
break;
230+
break;
221231
}
222232

223233
if ($permissions) {
@@ -228,25 +238,31 @@ public function save($filename, $image_type = null, $quality = null, $permission
228238
}
229239

230240
/**
231-
* Get image as string
241+
* Convert the image to string
232242
*
233243
* @param int $image_type
234244
* @param int $quality
235245
* @return string
236246
*/
237-
public function get($image_type = null, $quality = null){
238-
ob_start();
239-
$this->save(null, $image_type, $quality);
240-
return ob_get_clean();
247+
public function getImageAsString($image_type = null, $quality = null) {
248+
$string_temp = tempnam('', '');
249+
250+
$this->save($string_temp, $image_type, $quality);
251+
252+
$string = file_get_contents($string_temp);
253+
254+
unlink($string_temp);
255+
256+
return $string;
241257
}
242258

243259
/**
244260
* Convert the image to string with the current settings
245261
*
246262
* @return string
247263
*/
248-
public function __toString(){
249-
return $this->get();
264+
public function __toString() {
265+
return $this->getImageAsString();
250266
}
251267

252268
/**
@@ -351,7 +367,7 @@ public function resize($width, $height, $allow_enlarge = false)
351367
* @param integer $position
352368
* @return \static
353369
*/
354-
public function crop($width, $height, $allow_enlarge = false, $position = self::cropCENTER)
370+
public function crop($width, $height, $allow_enlarge = false, $position = self::CROPCENTER)
355371
{
356372
if (!$allow_enlarge) {
357373
// this logic is slightly different to resize(),
@@ -435,17 +451,18 @@ public function getDestHeight()
435451
* @param integer $position
436452
* @return integer
437453
*/
438-
protected function getCropPosition($expectedSize, $position = self::cropCENTER)
454+
protected function getCropPosition($expectedSize, $position = self::CROPCENTER)
439455
{
440456
$size = 0;
441457
switch ($position) {
442-
case self::cropBOTTOM:
443-
case self::cropRIGHT:
458+
case self::CROPBOTTOM:
459+
case self::CROPRIGHT:
444460
$size = $expectedSize;
445461
break;
446-
case self::cropCENTER:
447-
case self::cropCENTRE:
462+
case self::CROPCENTER:
463+
case self::CROPCENTRE:
448464
$size = $expectedSize / 2;
465+
break;
449466
}
450467
return $size;
451468
}

0 commit comments

Comments
 (0)