File.php 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. <?php
  2. namespace Gregwar\Image\Source;
  3. use Gregwar\Image\Image;
  4. /**
  5. * Open an image from a file.
  6. */
  7. class File extends Source
  8. {
  9. protected $file;
  10. public function __construct($file)
  11. {
  12. $this->file = $file;
  13. }
  14. public function getFile()
  15. {
  16. return $this->file;
  17. }
  18. public function correct()
  19. {
  20. return false !== @exif_imagetype($this->file);
  21. }
  22. public function guessType()
  23. {
  24. if (function_exists('exif_imagetype')) {
  25. $type = @exif_imagetype($this->file);
  26. if (false !== $type) {
  27. if ($type == IMAGETYPE_JPEG) {
  28. return 'jpeg';
  29. }
  30. if ($type == IMAGETYPE_GIF) {
  31. return 'gif';
  32. }
  33. if ($type == IMAGETYPE_PNG) {
  34. return 'png';
  35. }
  36. if ($type == IMAGETYPE_WEBP) {
  37. return 'webp';
  38. }
  39. }
  40. }
  41. $parts = explode('.', $this->file);
  42. $ext = strtolower($parts[count($parts) - 1]);
  43. if (isset(Image::$types[$ext])) {
  44. return Image::$types[$ext];
  45. }
  46. return 'jpeg';
  47. }
  48. public function getInfos()
  49. {
  50. return $this->file;
  51. }
  52. }