Guest User

Untitled

a guest
Jul 17th, 2018
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.36 KB | None | 0 0
  1. <?php
  2. #! Photo resize controller.
  3. #! Class interface.
  4. interface iResize
  5. {
  6. public function __construct($filename);
  7. public function resize($x, $y);
  8. public function save($locale);
  9. }
  10.  
  11. #! Class.
  12. class resize implements iResize
  13. {
  14. private $image, $type;
  15. public function __construct($filename)
  16. {
  17. $img = getimagesize($filename);
  18. $this->type = $img[2];
  19. switch($this->type)
  20. {
  21. case IMAGETYPE_JPEG:
  22. $this->image = imagecreatefromjpeg($filename);
  23. break;
  24. case IMAGETYPE_GIF:
  25. $this->image = imagecreatefromgif($filename);
  26. break;
  27. case IMAGETYPE_PNG:
  28. $this->image = imagecreatefrompng($filename);
  29. break;
  30. default:
  31. throw new Exception("Invalid image type.");
  32. }
  33. }
  34. private function axis()
  35. {
  36. return (object) array(
  37. "width" => imagesx($this->image),
  38. "height" => imagesy($this->image)
  39. );
  40. }
  41. public function resize($x, $y)
  42. {
  43. $new = imagecreatetruecolor($x, $y);
  44. imagecopyresampled($new, $this->image, 0, 0, 0, 0, $x, $y, $this->axis()->width, $this->axis()->height);
  45. $this->image = $new;
  46. }
  47. public function save($locale)
  48. {
  49. switch($this->type)
  50. {
  51. case IMAGETYPE_JPEG:
  52. imagejpeg($this->image, $locale, 75);
  53. break;
  54. case IMAGETYPE_GIF:
  55. imagegif($this->image, $locale);
  56. break;
  57. case IMAGETYPE_PNG:
  58. imagepng($this->image, $locale);
  59. break;
  60. default:
  61. throw new Exception("Invalid image type.");
  62. }
  63. }
  64. }
Add Comment
Please, Sign In to add comment