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.

97 lines
2.3 KiB

  1. <?php
  2. class Image
  3. {
  4. public $width;
  5. public $height;
  6. public $type;
  7. public $size;
  8. public $filename;
  9. public $original_filename;
  10. public $path;
  11. public $variants = array();
  12. private $_is_empty = true;
  13. const SIZE_100_80 = '100x80';
  14. const SIZE_200_160 = '200x160';
  15. const SIZE_300_240 = '300x240';
  16. const SIZE_550_440 = '550x440';
  17. const SIZE_SKI = '1000x';
  18. public static $sizes = array(
  19. self::SIZE_100_80,
  20. self::SIZE_200_160,
  21. self::SIZE_300_240,
  22. self::SIZE_550_440
  23. );
  24. /**
  25. * @param array|string $data
  26. * @return Image
  27. * @throws ErrorException
  28. */
  29. public static function getInstance($data)
  30. {
  31. $instance = new self($data);
  32. if (is_null($data)) {
  33. throw new ErrorException('Empty data for Image::getInstance().');
  34. }
  35. if (!is_array($data)) {
  36. $data = json_decode($data, true);
  37. if (json_last_error() != JSON_ERROR_NONE) {
  38. throw new ErrorException('Unable to convert json-string to array.');
  39. }
  40. }
  41. foreach ($data as $attribute_name => $attribute_value) {
  42. if (property_exists(__CLASS__, $attribute_name)) {
  43. $instance->{$attribute_name} = $attribute_value;
  44. }
  45. }
  46. $instance->_is_empty = false;
  47. return $instance;
  48. }
  49. /**
  50. * @return string
  51. */
  52. public function getWebName()
  53. {
  54. return $this->path . '/' . $this->filename;
  55. }
  56. /**
  57. * @param $size string
  58. * @return Image
  59. */
  60. public function getVariant($size)
  61. {
  62. if (!array_key_exists($size, $this->variants)) {
  63. $this->variants[$size] = new Image;
  64. } else {
  65. if (!is_object($this->variants[$size])) {
  66. $this->variants[$size] = self::getInstance($this->variants[$size]);
  67. }
  68. }
  69. return $this->variants[$size];
  70. }
  71. public function getIsEmpty()
  72. {
  73. return (bool) $this->_is_empty;
  74. }
  75. public function setIsNotEmpty()
  76. {
  77. $this->_is_empty = false;
  78. }
  79. public function __toString()
  80. {
  81. $data = array();
  82. foreach ($this as $attribute_name => $attribute_value) {
  83. $data[$attribute_name] = $attribute_value;
  84. }
  85. return json_encode($data);
  86. }
  87. }