Image.php 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  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. * @param string $path
  42. * @return void
  43. */
  44. public function saveJpeg($path)
  45. {
  46. imagejpeg($this->resource, $path);
  47. }
  48. }