Image.php 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. <?php
  2. namespace fphammerle\helpers;
  3. class Image
  4. {
  5. protected $resource = null;
  6. private function __construct()
  7. {
  8. }
  9. public function __destruct()
  10. {
  11. if($this->resource) {
  12. imagedestroy($this->resource);
  13. }
  14. }
  15. /**
  16. * @param string $path
  17. * @return Image
  18. */
  19. public static function fromFile($path)
  20. {
  21. switch(exif_imagetype($path)) {
  22. case IMAGETYPE_JPEG:
  23. $image = new self;
  24. $image->resource = imagecreatefromjpeg($path);
  25. return $image;
  26. default:
  27. throw new \InvalidArgumentException("type of '$path' is not supported");
  28. }
  29. }
  30. /**
  31. * @param float $angle
  32. * @return void
  33. */
  34. public function rotate($angle)
  35. {
  36. $resource = imagerotate($this->resource, $angle, 0);
  37. imagedestroy($this->resource);
  38. $this->resource = $resource;
  39. }
  40. /**
  41. * @return void
  42. */
  43. public function rotateLeft()
  44. {
  45. $this->rotate(90);
  46. }
  47. /**
  48. * @return void
  49. */
  50. public function rotateRight()
  51. {
  52. $this->rotate(270);
  53. }
  54. /**
  55. * @param string $path
  56. * @return void
  57. */
  58. public function saveJpeg($path)
  59. {
  60. imagejpeg($this->resource, $path);
  61. }
  62. }