File.php 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  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. }
  37. }
  38. $parts = explode('.', $this->file);
  39. $ext = strtolower($parts[count($parts) - 1]);
  40. if (isset(Image::$types[$ext])) {
  41. return Image::$types[$ext];
  42. }
  43. return 'jpeg';
  44. }
  45. public function getInfos()
  46. {
  47. return $this->file;
  48. }
  49. }