9
9
*/
10
10
class ImageResize
11
11
{
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 ;
18
18
19
19
public $ quality_jpg = 75 ;
20
20
public $ quality_png = 0 ;
@@ -45,9 +45,9 @@ class ImageResize
45
45
* @return ImageResize
46
46
* @throws \Exception
47
47
*/
48
- public static function createFromFile ($ filename ){
48
+ public static function createFromFile ($ filename ) {
49
49
$ s = new self ();
50
- $ s ->load ($ filename );
50
+ $ s ->loadFromFile ($ filename );
51
51
return $ s ;
52
52
}
53
53
@@ -58,7 +58,7 @@ public static function createFromFile($filename){
58
58
* @return ImageResize
59
59
* @throws \exception
60
60
*/
61
- public static function createFromString ($ imageData ){
61
+ public static function createFromString ($ imageData ) {
62
62
$ s = new self ();
63
63
$ s ->loadFromString ($ imageData );
64
64
return $ s ;
@@ -70,21 +70,19 @@ public static function createFromString($imageData){
70
70
* @param string|null $filename
71
71
* @throws \Exception
72
72
*/
73
- public function __construct ($ filename= null )
73
+ public function __construct ($ filename = null )
74
74
{
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 ();
79
81
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
+ }
88
86
}
89
87
90
88
/**
@@ -96,18 +94,29 @@ protected function getImagesizeFromString($imagedata){
96
94
*/
97
95
public function loadFromString ($ imagedata )
98
96
{
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 ) {
101
100
throw new \Exception ('Could not load image from string ' );
102
101
}
103
102
104
103
list (
105
104
$ this ->original_w ,
106
105
$ this ->original_h ,
107
106
$ this ->source_type
108
- ) = $ image_info ;
107
+ ) = $ image_info ;
109
108
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
+ }
111
120
112
121
return $ this ->resize ($ this ->getSourceWidth (), $ this ->getSourceHeight ());
113
122
}
@@ -118,12 +127,12 @@ public function loadFromString($imagedata)
118
127
* @return \static
119
128
* @throws Exception
120
129
*/
121
- public function load ($ filename )
130
+ public function loadFromFile ($ filename )
122
131
{
123
- $ image_info = getimagesize ($ filename );
132
+ $ image_info = @ getimagesize ($ filename );
124
133
125
134
if (!$ image_info ) {
126
- throw new \Exception ('Could not read ' . $ filename );
135
+ throw new \Exception ('Could not read ' . ( $ filename ?: ' file ' ) );
127
136
}
128
137
129
138
list (
@@ -135,18 +144,19 @@ public function load($filename)
135
144
switch ($ this ->source_type ) {
136
145
case IMAGETYPE_GIF :
137
146
$ this ->source_image = imagecreatefromgif ($ filename );
138
- break ;
147
+ break ;
139
148
140
149
case IMAGETYPE_JPEG :
141
150
$ this ->source_image = imagecreatefromjpeg ($ filename );
142
- break ;
151
+ break ;
143
152
144
153
case IMAGETYPE_PNG :
145
154
$ this ->source_image = imagecreatefrompng ($ filename );
146
- break ;
155
+ break ;
147
156
148
157
default :
149
158
throw new \Exception ('Unsupported image type ' );
159
+ break ;
150
160
}
151
161
152
162
return $ this ->resize ($ this ->getSourceWidth (), $ this ->getSourceHeight ());
@@ -172,17 +182,17 @@ public function save($filename, $image_type = null, $quality = null, $permission
172
182
imagecolortransparent ($ dest_image , $ background );
173
183
imagefill ($ dest_image , 0 , 0 , $ background );
174
184
imagesavealpha ($ dest_image , true );
175
- break ;
185
+ break ;
176
186
177
187
case IMAGETYPE_JPEG :
178
188
$ background = imagecolorallocate ($ dest_image , 255 , 255 , 255 );
179
189
imagefilledrectangle ($ dest_image , 0 , 0 , $ this ->getDestWidth (), $ this ->getDestHeight (), $ background );
180
- break ;
190
+ break ;
181
191
182
192
case IMAGETYPE_PNG :
183
193
imagealphablending ($ dest_image , false );
184
194
imagesavealpha ($ dest_image , true );
185
- break ;
195
+ break ;
186
196
}
187
197
188
198
imagecopyresampled (
@@ -201,23 +211,23 @@ public function save($filename, $image_type = null, $quality = null, $permission
201
211
switch ($ image_type ) {
202
212
case IMAGETYPE_GIF :
203
213
imagegif ($ dest_image , $ filename );
204
- break ;
214
+ break ;
205
215
206
216
case IMAGETYPE_JPEG :
207
217
if ($ quality === null ) {
208
218
$ quality = $ this ->quality_jpg ;
209
219
}
210
220
211
221
imagejpeg ($ dest_image , $ filename , $ quality );
212
- break ;
222
+ break ;
213
223
214
224
case IMAGETYPE_PNG :
215
225
if ($ quality === null ) {
216
226
$ quality = $ this ->quality_png ;
217
227
}
218
228
219
229
imagepng ($ dest_image , $ filename , $ quality );
220
- break ;
230
+ break ;
221
231
}
222
232
223
233
if ($ permissions ) {
@@ -228,25 +238,31 @@ public function save($filename, $image_type = null, $quality = null, $permission
228
238
}
229
239
230
240
/**
231
- * Get image as string
241
+ * Convert the image to string
232
242
*
233
243
* @param int $image_type
234
244
* @param int $quality
235
245
* @return string
236
246
*/
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 ;
241
257
}
242
258
243
259
/**
244
260
* Convert the image to string with the current settings
245
261
*
246
262
* @return string
247
263
*/
248
- public function __toString (){
249
- return $ this ->get ();
264
+ public function __toString () {
265
+ return $ this ->getImageAsString ();
250
266
}
251
267
252
268
/**
@@ -351,7 +367,7 @@ public function resize($width, $height, $allow_enlarge = false)
351
367
* @param integer $position
352
368
* @return \static
353
369
*/
354
- public function crop ($ width , $ height , $ allow_enlarge = false , $ position = self ::cropCENTER )
370
+ public function crop ($ width , $ height , $ allow_enlarge = false , $ position = self ::CROPCENTER )
355
371
{
356
372
if (!$ allow_enlarge ) {
357
373
// this logic is slightly different to resize(),
@@ -435,17 +451,18 @@ public function getDestHeight()
435
451
* @param integer $position
436
452
* @return integer
437
453
*/
438
- protected function getCropPosition ($ expectedSize , $ position = self ::cropCENTER )
454
+ protected function getCropPosition ($ expectedSize , $ position = self ::CROPCENTER )
439
455
{
440
456
$ size = 0 ;
441
457
switch ($ position ) {
442
- case self ::cropBOTTOM :
443
- case self ::cropRIGHT :
458
+ case self ::CROPBOTTOM :
459
+ case self ::CROPRIGHT :
444
460
$ size = $ expectedSize ;
445
461
break ;
446
- case self ::cropCENTER :
447
- case self ::cropCENTRE :
462
+ case self ::CROPCENTER :
463
+ case self ::CROPCENTRE :
448
464
$ size = $ expectedSize / 2 ;
465
+ break ;
449
466
}
450
467
return $ size ;
451
468
}
0 commit comments