2
2
3
3
namespace Eventviva ;
4
4
5
+ use \Exception ;
6
+
7
+ /**
8
+ * Handles thumb image(s) according to the original source given.
9
+ */
5
10
class ImageResize
6
11
{
7
-
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
+
8
19
public $ quality_jpg = 75 ;
9
20
public $ quality_png = 0 ;
10
21
@@ -27,11 +38,21 @@ class ImageResize
27
38
protected $ source_w ;
28
39
protected $ source_h ;
29
40
41
+ /**
42
+ * Constructor
43
+ * @param string $filename
44
+ */
30
45
public function __construct ($ filename )
31
46
{
32
47
$ this ->load ($ filename );
33
48
}
34
49
50
+ /**
51
+ * Loads image source and its properties to the instanciated object
52
+ * @param string $filename
53
+ * @return \static
54
+ * @throws Exception
55
+ */
35
56
protected function load ($ filename )
36
57
{
37
58
$ image_info = getimagesize ($ filename );
@@ -61,12 +82,19 @@ protected function load($filename)
61
82
62
83
default :
63
84
throw new \Exception ('Unsupported image type ' );
64
- break ;
65
85
}
66
86
67
87
return $ this ->resize ($ this ->getSourceWidth (), $ this ->getSourceHeight ());
68
88
}
69
-
89
+
90
+ /**
91
+ * Saves new image
92
+ * @param string $filename
93
+ * @param string $image_type
94
+ * @param integer $quality
95
+ * @param integer $permissions
96
+ * @return \static
97
+ */
70
98
public function save ($ filename , $ image_type = null , $ quality = null , $ permissions = null )
71
99
{
72
100
$ image_type = $ image_type ?: $ this ->source_type ;
@@ -133,7 +161,12 @@ public function save($filename, $image_type = null, $quality = null, $permission
133
161
134
162
return $ this ;
135
163
}
136
-
164
+
165
+ /**
166
+ * Outpus image source to browser
167
+ * @param string $image_type
168
+ * @param integer $quality
169
+ */
137
170
public function output ($ image_type = null , $ quality = null )
138
171
{
139
172
$ image_type = $ image_type ?: $ this ->source_type ;
@@ -142,7 +175,13 @@ public function output($image_type = null, $quality = null)
142
175
143
176
$ this ->save (null , $ image_type , $ quality );
144
177
}
145
-
178
+
179
+ /**
180
+ * Resizes image according to the given height. Width is proportional.
181
+ * @param integer $height
182
+ * @param boolean $allow_enlarge
183
+ * @return \static
184
+ */
146
185
public function resizeToHeight ($ height , $ allow_enlarge = false )
147
186
{
148
187
$ ratio = $ height / $ this ->getSourceHeight ();
@@ -152,7 +191,13 @@ public function resizeToHeight($height, $allow_enlarge = false)
152
191
153
192
return $ this ;
154
193
}
155
-
194
+
195
+ /**
196
+ * Resizes image according to the given width. Height is proportional.
197
+ * @param integer $width
198
+ * @param boolean $allow_enlarge
199
+ * @return \static
200
+ */
156
201
public function resizeToWidth ($ width , $ allow_enlarge = false )
157
202
{
158
203
$ ratio = $ width / $ this ->getSourceWidth ();
@@ -163,6 +208,11 @@ public function resizeToWidth($width, $allow_enlarge = false)
163
208
return $ this ;
164
209
}
165
210
211
+ /**
212
+ * Resizes image according to given scale (proportionally)
213
+ * @param type $scale
214
+ * @return \Eventviva\ImageResize
215
+ */
166
216
public function scale ($ scale )
167
217
{
168
218
$ width = $ this ->getSourceWidth () * $ scale / 100 ;
@@ -173,6 +223,13 @@ public function scale($scale)
173
223
return $ this ;
174
224
}
175
225
226
+ /**
227
+ * Resizes image according to the given width and height
228
+ * @param integer $width
229
+ * @param integer $height
230
+ * @param boolean $allow_enlarge
231
+ * @return \static
232
+ */
176
233
public function resize ($ width , $ height , $ allow_enlarge = false )
177
234
{
178
235
if (!$ allow_enlarge ) {
@@ -197,8 +254,17 @@ public function resize($width, $height, $allow_enlarge = false)
197
254
198
255
return $ this ;
199
256
}
200
-
201
- public function crop ($ width , $ height , $ allow_enlarge = false )
257
+
258
+ /**
259
+ * Crops image according to the given width and height for the new saved
260
+ * image. Crop's position may be configured.
261
+ * @param integer $width
262
+ * @param integer $height
263
+ * @param boolean $allow_enlarge
264
+ * @param integer $position
265
+ * @return \static
266
+ */
267
+ public function crop ($ width , $ height , $ allow_enlarge = false , $ position = self ::cropCENTER)
202
268
{
203
269
if (!$ allow_enlarge ) {
204
270
// this logic is slightly different to resize(),
@@ -213,17 +279,17 @@ public function crop($width, $height, $allow_enlarge = false)
213
279
$ height = $ this ->getSourceHeight ();
214
280
}
215
281
}
216
-
282
+
217
283
$ ratio_source = $ this ->getSourceWidth () / $ this ->getSourceHeight ();
218
284
$ ratio_dest = $ width / $ height ;
219
-
285
+
220
286
if ($ ratio_dest < $ ratio_source ) {
221
287
$ this ->resizeToHeight ($ height , $ allow_enlarge );
222
288
223
289
$ excess_width = ($ this ->getDestWidth () - $ width ) / $ this ->getDestWidth () * $ this ->getSourceWidth ();
224
290
225
291
$ this ->source_w = $ this ->getSourceWidth () - $ excess_width ;
226
- $ this ->source_x = $ excess_width / 2 ;
292
+ $ this ->source_x = $ this -> getCropPosition ( $ excess_width, $ position ) ;
227
293
228
294
$ this ->dest_w = $ width ;
229
295
} else {
@@ -232,32 +298,68 @@ public function crop($width, $height, $allow_enlarge = false)
232
298
$ excess_height = ($ this ->getDestHeight () - $ height ) / $ this ->getDestHeight () * $ this ->getSourceHeight ();
233
299
234
300
$ this ->source_h = $ this ->getSourceHeight () - $ excess_height ;
235
- $ this ->source_y = $ excess_height / 2 ;
301
+ $ this ->source_y = $ this -> getCropPosition ( $ excess_height, $ position ) ;
236
302
237
303
$ this ->dest_h = $ height ;
238
304
}
239
305
240
306
return $ this ;
241
307
}
242
-
308
+
309
+ /**
310
+ * Gets source width
311
+ * @return integer
312
+ */
243
313
public function getSourceWidth ()
244
314
{
245
315
return $ this ->original_w ;
246
316
}
247
-
317
+
318
+ /**
319
+ * Gets source height
320
+ * @return integer
321
+ */
248
322
public function getSourceHeight ()
249
323
{
250
324
return $ this ->original_h ;
251
325
}
252
326
327
+ /**
328
+ * Gets width of the destination image
329
+ * @return integer
330
+ */
253
331
public function getDestWidth ()
254
332
{
255
333
return $ this ->dest_w ;
256
334
}
257
335
336
+ /**
337
+ * Gets height of the destination image
338
+ * @return integer
339
+ */
258
340
public function getDestHeight ()
259
341
{
260
342
return $ this ->dest_h ;
261
343
}
262
-
344
+
345
+ /**
346
+ * Gets crop position (X or Y) according to the given position.
347
+ * @param integer $expectedSize
348
+ * @param integer $position
349
+ * @return integer
350
+ */
351
+ protected function getCropPosition ($ expectedSize , $ position = self ::cropCENTER)
352
+ {
353
+ $ size = 0 ;
354
+ switch ($ position ) {
355
+ case self ::cropBOTTOM:
356
+ case self ::cropRIGHT:
357
+ $ size = $ expectedSize ;
358
+ break ;
359
+ case self ::cropCENTER:
360
+ case self ::cropCENTRE:
361
+ $ size = $ expectedSize / 2 ;
362
+ }
363
+ return $ size ;
364
+ }
263
365
}
0 commit comments