1 | <?php |
---|
2 | |
---|
3 | class Resize_image_helper |
---|
4 | { |
---|
5 | |
---|
6 | protected $_units = array("B", "KB", "MB", "GB"); |
---|
7 | protected $_allowed_extensions = array("jpeg", "jpg", "png", "gif"); |
---|
8 | private $_jpg_quality = 100; |
---|
9 | private $_png_compression = 0; |
---|
10 | private $_width; |
---|
11 | private $_height; |
---|
12 | private $_aspect; |
---|
13 | /** |
---|
14 | * ["dirname"] : Full folder path of image |
---|
15 | * ["fullpath] : Same as dirname but includes the filename and extension |
---|
16 | * ["filename] : Image name without extension |
---|
17 | * ["extension] : Image extension |
---|
18 | * ["filesize"] : Image filesize |
---|
19 | * @var array |
---|
20 | * |
---|
21 | */ |
---|
22 | private $file = array(); |
---|
23 | protected $_resource; |
---|
24 | function __construct($src_path_img = NULL,$dest_path_img=NULL) |
---|
25 | { |
---|
26 | if(!empty($src_path_img)){ |
---|
27 | if(!empty($dest_path_img)) |
---|
28 | $this->dest_path_img= $dest_path_img; |
---|
29 | else |
---|
30 | $this->dest_path_img= $src_path_img; |
---|
31 | $this->load_image($src_path_img); |
---|
32 | } |
---|
33 | $this->_reflex = new ReflectionClass('Resize_image_helper'); |
---|
34 | } |
---|
35 | /** |
---|
36 | * |
---|
37 | * Verifies that the file extension is supported |
---|
38 | * @param string $ext |
---|
39 | * @return boolean |
---|
40 | */ |
---|
41 | private function _is_allowed_extension($ext) |
---|
42 | { |
---|
43 | try{ |
---|
44 | if(empty($ext)){ |
---|
45 | throw new Exception("Invalid Argument"); |
---|
46 | } |
---|
47 | |
---|
48 | $ext = strtolower($ext); |
---|
49 | return in_array($ext, $this->_allowed_extensions); |
---|
50 | |
---|
51 | }catch(Exception $e){ |
---|
52 | echo $e->getMessage(); |
---|
53 | } |
---|
54 | } |
---|
55 | |
---|
56 | /** |
---|
57 | * Returns a value with two decimals from Megabyte. |
---|
58 | * If the conversion is in the order of 1E-3 returns zero |
---|
59 | * @param string |
---|
60 | * @return int|float |
---|
61 | */ |
---|
62 | private function _convert_size($unit) |
---|
63 | { |
---|
64 | try{ |
---|
65 | |
---|
66 | if(empty($unit)){ |
---|
67 | throw new Exception("Invalid Argument"); |
---|
68 | } |
---|
69 | |
---|
70 | switch($unit){ |
---|
71 | case "B":{ |
---|
72 | return $this->file["filesize"]; |
---|
73 | } |
---|
74 | case "KB":{ |
---|
75 | $size = floor($this->file["filesize"] / 1024 ); |
---|
76 | return $size; |
---|
77 | } |
---|
78 | case "MB":{ |
---|
79 | $size = round( |
---|
80 | ($this->file["filesize"] / 1048576 ), |
---|
81 | 2, PHP_ROUND_HALF_DOWN |
---|
82 | ); |
---|
83 | return $size; |
---|
84 | } |
---|
85 | case "GB":{ |
---|
86 | $size = round( |
---|
87 | ($this->file["filesize"] / 1073741824 ), |
---|
88 | 2, PHP_ROUND_HALF_DOWN |
---|
89 | ); |
---|
90 | return $size; |
---|
91 | } |
---|
92 | } |
---|
93 | }catch(Exception $e){ |
---|
94 | echo $e->getMessage(); |
---|
95 | } |
---|
96 | } |
---|
97 | |
---|
98 | /** |
---|
99 | * Assumes the existing resource and extension validates |
---|
100 | * Generates a new image |
---|
101 | * $output = NULL : the raw image stream will be outputted directly |
---|
102 | * $output = filepath : the raw image will be saved in a file |
---|
103 | * |
---|
104 | * @param resource $container |
---|
105 | * @param string $ext |
---|
106 | * @param $output |
---|
107 | */ |
---|
108 | private function _sample($container, $ext, $output = NULL) |
---|
109 | { |
---|
110 | try{ |
---|
111 | if(empty($container) || empty($ext)){ |
---|
112 | throw new Exception("Invalid Argument"); |
---|
113 | } |
---|
114 | $ext = strtolower($ext); |
---|
115 | switch($ext) |
---|
116 | { |
---|
117 | case "jpg":{ |
---|
118 | $resample = imageJPEG( |
---|
119 | $container, |
---|
120 | $output, |
---|
121 | $this->_jpg_quality |
---|
122 | ); |
---|
123 | break; |
---|
124 | } |
---|
125 | case "jpge":{ |
---|
126 | $resample = imageJPEG( |
---|
127 | $container, |
---|
128 | $output, |
---|
129 | $this->_jpg_quality |
---|
130 | ); |
---|
131 | break; |
---|
132 | } |
---|
133 | case "png":{ |
---|
134 | $resample = imagepng( |
---|
135 | $container, |
---|
136 | $output, |
---|
137 | $this->_png_compression |
---|
138 | ); |
---|
139 | break; |
---|
140 | } |
---|
141 | case "gif":{ |
---|
142 | $resample = imagegif($container, $output); |
---|
143 | } |
---|
144 | } |
---|
145 | |
---|
146 | if(empty($resample)){ |
---|
147 | throw new Exception("Cannot Render the Image"); |
---|
148 | } |
---|
149 | |
---|
150 | return $resample; |
---|
151 | |
---|
152 | }catch(Extension $e){ |
---|
153 | echo $e->getMessage(); |
---|
154 | } |
---|
155 | } |
---|
156 | |
---|
157 | /** |
---|
158 | * Initializes an image |
---|
159 | * @param string |
---|
160 | */ |
---|
161 | function load_image($src_path_image) |
---|
162 | { |
---|
163 | try{ |
---|
164 | if(empty($src_path_image)){ |
---|
165 | |
---|
166 | throw new Exception("Invalid Argument"); |
---|
167 | } |
---|
168 | |
---|
169 | $foo = pathinfo($src_path_image); |
---|
170 | |
---|
171 | if(!isset($foo["extension"])){ |
---|
172 | throw new Exception( |
---|
173 | "Could not retrieve information from the image" |
---|
174 | ); |
---|
175 | } |
---|
176 | |
---|
177 | if(!$this->_is_allowed_extension($foo["extension"])){ |
---|
178 | throw new Exception("Unsupported extension"); |
---|
179 | } |
---|
180 | |
---|
181 | $this->file["fullpath"] = $src_path_image; |
---|
182 | $this->file["dirname"] = $foo["dirname"]; |
---|
183 | $this->file["basename"] = $foo["basename"]; |
---|
184 | $this->file["extension"] = strtoupper( |
---|
185 | $foo["extension"] |
---|
186 | ); |
---|
187 | $this->file["filename"] = $foo["filename"]; |
---|
188 | $this->file["filesize"] = filesize($src_path_image); |
---|
189 | |
---|
190 | switch($this->file["extension"]){ |
---|
191 | |
---|
192 | case "JPEG":{ |
---|
193 | $this->_resource = ImageCreateFromJPEG( |
---|
194 | $src_path_image |
---|
195 | ); |
---|
196 | break; |
---|
197 | } |
---|
198 | |
---|
199 | case "JPG":{ |
---|
200 | $this->_resource = ImageCreateFromJPEG( |
---|
201 | $src_path_image |
---|
202 | ); |
---|
203 | break; |
---|
204 | } |
---|
205 | |
---|
206 | case "PNG":{ |
---|
207 | $this->_resource = ImageCreateFromPNG( |
---|
208 | $src_path_image |
---|
209 | ); |
---|
210 | break; |
---|
211 | } |
---|
212 | |
---|
213 | case "GIF":{ |
---|
214 | $this->_resource = ImageCreateFromGIF( |
---|
215 | $src_path_image |
---|
216 | ); |
---|
217 | break; |
---|
218 | } |
---|
219 | } |
---|
220 | |
---|
221 | if($this->_resource === FALSE){ |
---|
222 | |
---|
223 | throw new Exception("Could not create the resource"); |
---|
224 | }else{ |
---|
225 | $this->_width = imagesx($this->_resource); |
---|
226 | // Get Original Image Width |
---|
227 | $this->_height = imagesy($this->_resource); |
---|
228 | // Get Original Image Height |
---|
229 | if(!empty($this->_width) && !empty($this->_height)){ |
---|
230 | |
---|
231 | $this->aspect = round( |
---|
232 | ($this->_width / $this->_height), |
---|
233 | 2, PHP_ROUND_HALF_DOWN |
---|
234 | ); |
---|
235 | return true; |
---|
236 | }else{ |
---|
237 | |
---|
238 | throw new Exception( |
---|
239 | "Could not recover the dimensions of the image" |
---|
240 | ); |
---|
241 | } |
---|
242 | } |
---|
243 | }catch(Exception $e){ |
---|
244 | echo $e->getMessage(); |
---|
245 | } |
---|
246 | } |
---|
247 | |
---|
248 | /** |
---|
249 | * Convert the format of an image. |
---|
250 | * $reload = true : Load the resulting image on the object |
---|
251 | * @param string $ext |
---|
252 | * @param boolena $reload |
---|
253 | */ |
---|
254 | function convert_as($ext, $reload = FALSE, $save_as_file = TRUE, $save_name = NULL) |
---|
255 | { |
---|
256 | try{ |
---|
257 | if(empty($ext)){ |
---|
258 | throw new Exception("Extension cannot be NULL"); |
---|
259 | } |
---|
260 | |
---|
261 | if(empty($this->_resource)){ |
---|
262 | throw new Exception("Resource is needed"); |
---|
263 | } |
---|
264 | |
---|
265 | strtolower($ext); |
---|
266 | |
---|
267 | if($this->_is_allowed_extension($ext)){ |
---|
268 | |
---|
269 | $temp_image = @imagecreatetruecolor($this->_width, $this->_height); |
---|
270 | @imagecopyresampled( |
---|
271 | $temp_image, |
---|
272 | $this->_resource, 0, 0, 0, 0, |
---|
273 | $this->_width, $this->_height, |
---|
274 | $this->_width, $this->_height |
---|
275 | ); |
---|
276 | |
---|
277 | if($save_as_file){ |
---|
278 | $output = $this->file["dirname"] . "/"; |
---|
279 | |
---|
280 | if(!empty($save_name)){ |
---|
281 | $output .= $save_name . "." . $ext; |
---|
282 | } |
---|
283 | else{ |
---|
284 | $output .= $this->file["filename"] . "." . $ext; |
---|
285 | } |
---|
286 | } |
---|
287 | else{ |
---|
288 | $output = NULL; |
---|
289 | } |
---|
290 | |
---|
291 | if($this->_sample($temp_image, $ext, $output)){ |
---|
292 | |
---|
293 | if($reload === TRUE){ |
---|
294 | return $this->load_image($output); |
---|
295 | } |
---|
296 | else{ |
---|
297 | return true; |
---|
298 | } |
---|
299 | } |
---|
300 | else{ |
---|
301 | throw new Exception("Cannot Render the Image"); |
---|
302 | } |
---|
303 | } |
---|
304 | else{ |
---|
305 | throw new Exception("Unsupported extension"); |
---|
306 | } |
---|
307 | }catch(Exception $e){ |
---|
308 | echo $e->getMessage(); |
---|
309 | } |
---|
310 | } |
---|
311 | |
---|
312 | |
---|
313 | |
---|
314 | |
---|
315 | /** |
---|
316 | * |
---|
317 | * Resize height of image |
---|
318 | * |
---|
319 | * $respect_proportion = TRUE : |
---|
320 | * Adjusts the image heigh; |
---|
321 | * Adjusts the image width based on the original aspect ratio |
---|
322 | * |
---|
323 | * $respect_proportion = FALSE: |
---|
324 | * Adjusts the image height |
---|
325 | * |
---|
326 | * WARNING: |
---|
327 | * If the $overwrite option is active the original image will be replace. |
---|
328 | * |
---|
329 | * $overwrite = TRUE: replace the original image |
---|
330 | * $overwrite = FALSE (default): create a copy with the original |
---|
331 | * name and suffix w{$width} |
---|
332 | * |
---|
333 | * @param int $width |
---|
334 | * @param boolean $width |
---|
335 | * @param boolean $respect_proportion |
---|
336 | * @return boolean |
---|
337 | */ |
---|
338 | function resize_width($width, $overwrite = FALSE, $respect_proportion = TRUE) |
---|
339 | { |
---|
340 | try{ |
---|
341 | if(empty($width)){ |
---|
342 | throw new Exception("Invalid Argument"); |
---|
343 | } |
---|
344 | |
---|
345 | if(empty($this->_resource)){ |
---|
346 | throw new Exception("Resource is needed"); |
---|
347 | } |
---|
348 | |
---|
349 | if($respect_proportion){ |
---|
350 | $height = (int)($width / $this->aspect); |
---|
351 | }else{ |
---|
352 | $height = $this->_height; |
---|
353 | } |
---|
354 | |
---|
355 | $temp_image = @imagecreatetruecolor($width, $height); |
---|
356 | @imagecopyresampled( |
---|
357 | $temp_image, |
---|
358 | $this->_resource, 0, 0, 0, 0, |
---|
359 | $width, $height, // Resize dimensions |
---|
360 | $this->_width, $this->_height // Original dimensions |
---|
361 | ); |
---|
362 | |
---|
363 | if(!$overwrite){ |
---|
364 | $output = "{$this->file["dirname"]}/{$this->file["filename"]}_w{$height}.{$this->file["extension"]}"; |
---|
365 | } |
---|
366 | else{ |
---|
367 | $output = $this->file["fullpath"]; |
---|
368 | } |
---|
369 | $output = $this->dest_path_img; |
---|
370 | |
---|
371 | if($this->_sample($temp_image, $this->file["extension"], $output)){ |
---|
372 | return TRUE; |
---|
373 | }else{ |
---|
374 | return FALSE; |
---|
375 | } |
---|
376 | |
---|
377 | }catch(Exception $e){ |
---|
378 | echo $e->getMessage(); |
---|
379 | } |
---|
380 | } |
---|
381 | |
---|
382 | |
---|
383 | |
---|
384 | /** |
---|
385 | * Destroy the image resource |
---|
386 | * @return boolean |
---|
387 | */ |
---|
388 | function free_image_mem() |
---|
389 | { |
---|
390 | if(!empty($this->_resource)){ |
---|
391 | return @imagedestroy($this->_resource); |
---|
392 | }else{ |
---|
393 | return true; |
---|
394 | } |
---|
395 | } |
---|
396 | |
---|
397 | /** |
---|
398 | * Returns the width if it exists, false otherwise |
---|
399 | * @return int|FALSE |
---|
400 | */ |
---|
401 | function get_width() |
---|
402 | { |
---|
403 | if(empty($this->_width)){ |
---|
404 | return false; |
---|
405 | }else{ |
---|
406 | return $this->_width; |
---|
407 | } |
---|
408 | } |
---|
409 | |
---|
410 | /** |
---|
411 | * Returns the heigth if it exists, false otherwise |
---|
412 | * @return int|FALSE |
---|
413 | */ |
---|
414 | function get_height() |
---|
415 | { |
---|
416 | if(empty($this->_height)){ |
---|
417 | return false; |
---|
418 | }else{ |
---|
419 | return $this->_height; |
---|
420 | } |
---|
421 | } |
---|
422 | |
---|
423 | /** |
---|
424 | * Returns the aspect if it exists, false otherwise |
---|
425 | * @return float|FALSE |
---|
426 | */ |
---|
427 | function get_aspect() |
---|
428 | { |
---|
429 | if(empty($this->aspect)){ |
---|
430 | return false; |
---|
431 | }else{ |
---|
432 | return $this->aspect; |
---|
433 | } |
---|
434 | } |
---|
435 | |
---|
436 | /** |
---|
437 | * Returns the file array if it exists, false otherwise |
---|
438 | * @return array|FALSE |
---|
439 | */ |
---|
440 | function get_fileinfo() |
---|
441 | { |
---|
442 | if(empty($this->file)){ |
---|
443 | return false; |
---|
444 | }else{ |
---|
445 | return $this->file; |
---|
446 | } |
---|
447 | } |
---|
448 | |
---|
449 | /** |
---|
450 | * Returns the dirname of a image, false otherwise |
---|
451 | * @return string|FALSE |
---|
452 | */ |
---|
453 | function get_dirname() |
---|
454 | { |
---|
455 | if(!empty($this->file["dirname"])){ |
---|
456 | return $this->file["dirname"]; |
---|
457 | }else{ |
---|
458 | return false; |
---|
459 | } |
---|
460 | } |
---|
461 | |
---|
462 | /** |
---|
463 | * Returns the fullpath of a image, false otherwise |
---|
464 | * @return string|FALSE |
---|
465 | */ |
---|
466 | function get_fullpath() |
---|
467 | { |
---|
468 | if(!empty($this->file["fullpath"])){ |
---|
469 | return $this->file["fullpath"]; |
---|
470 | }else{ |
---|
471 | return false; |
---|
472 | } |
---|
473 | } |
---|
474 | |
---|
475 | /** |
---|
476 | * Returns the filename of a image, false otherwise |
---|
477 | * @return string|FALSE |
---|
478 | */ |
---|
479 | function get_filename() |
---|
480 | { |
---|
481 | if(!empty($this->file["filename"])){ |
---|
482 | return $this->file["filename"]; |
---|
483 | }else{ |
---|
484 | return false; |
---|
485 | } |
---|
486 | } |
---|
487 | |
---|
488 | /** |
---|
489 | * Returns the extension of a image, false otherwise |
---|
490 | * @return string|FALSE |
---|
491 | */ |
---|
492 | function get_extension() |
---|
493 | { |
---|
494 | if(!empty($this->file["extension"])){ |
---|
495 | $ext = strtoupper($this->file["extension"]); |
---|
496 | return $ext; |
---|
497 | }else{ |
---|
498 | return false; |
---|
499 | } |
---|
500 | } |
---|
501 | |
---|
502 | /** |
---|
503 | * Retrieves the file size |
---|
504 | * @param string [optional] |
---|
505 | */ |
---|
506 | function get_filesize($unit = "B") |
---|
507 | { |
---|
508 | try{ |
---|
509 | if(empty($unit)){ |
---|
510 | throw new Exception("Invalid Argument"); |
---|
511 | } |
---|
512 | |
---|
513 | $unit = strtoupper($unit); |
---|
514 | |
---|
515 | if(in_array($unit, $this->_units)){ |
---|
516 | |
---|
517 | return $this->_convert_size($unit); |
---|
518 | } |
---|
519 | }catch(Exception $e){ |
---|
520 | echo $e->getMessage(); |
---|
521 | } |
---|
522 | } |
---|
523 | |
---|
524 | /** |
---|
525 | * Return jpg_quality used to sample |
---|
526 | * @return int |
---|
527 | */ |
---|
528 | function get_jpg_quality() |
---|
529 | { |
---|
530 | return $this->_jpg_quality; |
---|
531 | } |
---|
532 | |
---|
533 | |
---|
534 | /** |
---|
535 | * Set jpg Quality used to sample |
---|
536 | * @param int $quality |
---|
537 | */ |
---|
538 | function set_jpg_quality($quality) |
---|
539 | { |
---|
540 | try{ |
---|
541 | |
---|
542 | if(isset($quality) && $quality === 0){ |
---|
543 | $this->_jpg_quality = 0; |
---|
544 | return true; |
---|
545 | } |
---|
546 | |
---|
547 | if(empty($quality)){ |
---|
548 | throw new Exception("Invalid Argument - Accepted: (int)0~100"); |
---|
549 | } |
---|
550 | |
---|
551 | if($quality<1 || $quality>100){ |
---|
552 | throw new Exception( |
---|
553 | "Invalid value to JPG quality - Accepted: (int)0~100" |
---|
554 | ); |
---|
555 | } |
---|
556 | |
---|
557 | $this->_jpg_quality = $quality; |
---|
558 | |
---|
559 | }catch(Exception $e){ |
---|
560 | echo $e->getMessage(); |
---|
561 | } |
---|
562 | } |
---|
563 | |
---|
564 | /** |
---|
565 | * Return PNG compression used to sample |
---|
566 | * @return int |
---|
567 | */ |
---|
568 | function get_png_compression() |
---|
569 | { |
---|
570 | return $this->_png_compression; |
---|
571 | } |
---|
572 | |
---|
573 | /** |
---|
574 | * Set PNG compression used to sample |
---|
575 | * @param int $compression |
---|
576 | * @return boolean |
---|
577 | */ |
---|
578 | function set_png_compression($compression) |
---|
579 | { |
---|
580 | try{ |
---|
581 | if(isset($compression) && $compression === 0){ |
---|
582 | $this->_png_compression = 0; |
---|
583 | return true; |
---|
584 | } |
---|
585 | |
---|
586 | if(empty($compression)){ |
---|
587 | throw new Exception("Invalid Argument - Accepted: (int)1~9"); |
---|
588 | } |
---|
589 | |
---|
590 | if($compression<1 || $compression>9){ |
---|
591 | throw new Exception( |
---|
592 | "Invalid value to PNG compression - Accepted: (int)1~9" |
---|
593 | ); |
---|
594 | } |
---|
595 | |
---|
596 | $this->_png_compression = (int)$compression; |
---|
597 | return true; |
---|
598 | }catch(Exception $e){ |
---|
599 | echo $e->getMessage(); |
---|
600 | } |
---|
601 | } |
---|
602 | |
---|
603 | function __destruct() |
---|
604 | { |
---|
605 | $this->free_image_mem(); |
---|
606 | } |
---|
607 | } |
---|