You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

93 lines
2.1 KiB

11 years ago
9 years ago
  1. <?php
  2. namespace dimti\Image;
  3. abstract class Image extends File
  4. {
  5. public $width;
  6. public $height;
  7. public $type;
  8. public $size;
  9. public $original_filename;
  10. /**
  11. * @var ImageVariant[]
  12. */
  13. public $variants = array();
  14. protected static $max_width;
  15. protected static $max_height;
  16. protected static $sizes = array();
  17. protected static $watermarks = array();
  18. public static function getClassName()
  19. {
  20. return get_called_class();
  21. }
  22. /**
  23. * @param $class string|\StdClass
  24. * @throws \ErrorException
  25. */
  26. public static function checkSubClass($class)
  27. {
  28. if (!self::getIsSubClass($class)) {
  29. throw new \ErrorException('Class "' . $class . '" mast be extend of "Image".');
  30. }
  31. }
  32. /**
  33. * @param $class string|\StdClass
  34. * @return bool
  35. */
  36. public static function getIsSubClass($class)
  37. {
  38. $parent_class = get_parent_class($class);
  39. if ($parent_class == __CLASS__) {
  40. return true;
  41. }
  42. if (get_parent_class($parent_class)) {
  43. return self::getIsSubClass($parent_class);
  44. }
  45. return false;
  46. }
  47. public static function getSizes()
  48. {
  49. return static::$sizes;
  50. }
  51. public static function getWatermark($size)
  52. {
  53. return (isset(static::$watermarks[$size])) ? static::$watermarks[$size] : false;
  54. }
  55. public static function getMaxWidth()
  56. {
  57. return static::$max_width;
  58. }
  59. public static function getMaxHeight()
  60. {
  61. return static::$max_height;
  62. }
  63. /**
  64. * @param $size
  65. * @param bool $force_create
  66. * @param bool $important_create
  67. *
  68. * @return ImageVariant
  69. */
  70. public function getVariant($size, $force_create = false, $important_create = null)
  71. {
  72. if ($force_create && $important_create === null && is_array($this->variants) && array_key_exists($size, $this->variants) && is_array($this->variants[$size])
  73. && $this->variants[$size]['size'] === null) {
  74. $important_create = true;
  75. }
  76. return parent::getImageVariant($size, $force_create, $important_create);
  77. }
  78. }