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.

153 lines
4.1 KiB

  1. <?php
  2. namespace dimti\Image;
  3. /**
  4. * @author Alexander Demidov <demidov@dimti.ru>
  5. * Full paths to directories and files contains slash as first symbol
  6. * Dir name not contains slashes on first or last symbols
  7. */
  8. use Majestic\Config;
  9. /**
  10. * Class File
  11. */
  12. abstract class File
  13. {
  14. public $path;
  15. public $filename;
  16. private $logger;
  17. private $error_stream;
  18. private $owner;
  19. public function __construct()
  20. {
  21. }
  22. public function setOwner($owner)
  23. {
  24. $this->owner = $owner;
  25. }
  26. public function getOwner()
  27. {
  28. return $this->owner;
  29. }
  30. public function log($message)
  31. {
  32. if (\Majestic\Config::get('LOGGING')) {
  33. if (is_null($this->logger)) {
  34. $this->logger = \Majestic\Logger\Logger::getInstance();
  35. }
  36. $this->logger->log($message);
  37. } else {
  38. $this->error_stream = \Majestic\Config::get('ErrorStream', 'php://stderr');
  39. file_put_contents($this->error_stream, PHP_EOL . 'Log ' . '#' . __CLASS__ . ': ' . $message . PHP_EOL, FILE_APPEND);
  40. }
  41. }
  42. /**
  43. * @param array|string|null $data
  44. * @return Image
  45. * @throws \ErrorException
  46. */
  47. public static function getInstance($data = null)
  48. {
  49. $instance = new static;
  50. if (!(is_null($data) || $data == '')) {
  51. if (!is_array($data)) {
  52. $data = json_decode($data, true);
  53. if (json_last_error() != JSON_ERROR_NONE) {
  54. $instance->log('Unable to convert json-string to array. Data ' . print_r($data, true));
  55. }
  56. }
  57. if (is_array($data)) {
  58. foreach ($data as $attribute_name => $attribute_value) {
  59. if (property_exists($instance, $attribute_name)) {
  60. $instance->{$attribute_name} = $attribute_value;
  61. }
  62. }
  63. }
  64. }
  65. return $instance;
  66. }
  67. /**
  68. * @param $size
  69. * @param bool $force_create
  70. * @param bool $important_create
  71. *
  72. * @return ImageVariant
  73. * @throws \ErrorException
  74. */
  75. public function getImageVariant($size, $force_create = false, $important_create = false)
  76. {
  77. /**
  78. * @var $this Image
  79. */
  80. if (!array_key_exists($size, $this->variants) || $important_create) {
  81. $original_file_path = \Majestic\Config::get('PATH_WEB_ROOT') . '/' . $this->getWebName();
  82. //TODO:Сложно понять логику этого выражения
  83. if (($important_create && $this->getIsNoEmpty() && file_exists($original_file_path)) || ($force_create && $this->getIsNoEmpty() && file_exists($original_file_path))) {
  84. Upload::imageVariant($this, $size);
  85. } else {
  86. $this->variants[$size] = new ImageVariant();
  87. }
  88. } else {
  89. if (!is_object($this->variants[$size])) {
  90. $this->variants[$size] = ImageVariant::getInstance($this->variants[$size]);
  91. }
  92. }
  93. return $this->variants[$size];
  94. }
  95. /**
  96. * Если объект содержит информацию о файле/изображение вернет - true
  97. * Если объект пуст - вернет false
  98. * @return bool
  99. */
  100. public function getIsNoEmpty()
  101. {
  102. return (bool) $this->size;
  103. }
  104. public function delete()
  105. {
  106. /**
  107. * @var $image Image
  108. */
  109. if ($this->getIsNoEmpty()) {
  110. @unlink($this->getRealPath());
  111. }
  112. if (get_parent_class($this) == 'dimti\Image\Image') {
  113. $image = $this;
  114. foreach ($image->getSizes() as $size) {
  115. $image_variant = $image->getVariant($size);
  116. if ($image_variant->getIsNoEmpty()) {
  117. @unlink($image_variant->getRealPath());
  118. }
  119. }
  120. }
  121. }
  122. public function getRealPath()
  123. {
  124. return Config::get('PATH_WEB_ROOT') . '/' . $this->path . '/' . $this->filename;
  125. }
  126. /**
  127. * @return string
  128. */
  129. public function getWebName()
  130. {
  131. return (($this->getIsNoEmpty()) ? ($this->path . '/' . $this->filename) : '_.gif');
  132. }
  133. public function __toString()
  134. {
  135. return FileHelper::toString($this);
  136. }
  137. }