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.

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