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.

148 lines
3.8 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 string
  68. * @param $force_create bool
  69. * @return ImageVariant
  70. */
  71. public function getImageVariant($size, $force_create = false)
  72. {
  73. /**
  74. * @var $this Image
  75. */
  76. if (!array_key_exists($size, $this->variants)) {
  77. $original_file_path = \Majestic\Config::get('PATH_WEB_ROOT') . '/' . $this->getWebName();
  78. if ($force_create && $this->getIsNoEmpty() && file_exists($original_file_path)) {
  79. Upload::imageVariant($this, $size);
  80. } else {
  81. $this->variants[$size] = new ImageVariant();
  82. }
  83. } else {
  84. if (!is_object($this->variants[$size])) {
  85. $this->variants[$size] = ImageVariant::getInstance($this->variants[$size]);
  86. }
  87. }
  88. return $this->variants[$size];
  89. }
  90. /**
  91. * Если объект содержит информацию о файле/изображение вернет - true
  92. * Если объект пуст - вернет false
  93. * @return bool
  94. */
  95. public function getIsNoEmpty()
  96. {
  97. return (bool) $this->size;
  98. }
  99. public function delete()
  100. {
  101. /**
  102. * @var $image Image
  103. */
  104. if ($this->getIsNoEmpty()) {
  105. @unlink($this->getRealPath());
  106. }
  107. if (get_parent_class($this) == 'Image') {
  108. $image = $this;
  109. foreach ($image->getSizes() as $size) {
  110. $image_variant = $image->getVariant($size);
  111. if ($image_variant->getIsNoEmpty()) {
  112. @unlink($image->getRealPath());
  113. }
  114. }
  115. }
  116. }
  117. public function getRealPath()
  118. {
  119. return PATH_WEB_ROOT . '/' . $this->path . '/' . $this->filename;
  120. }
  121. /**
  122. * @return string
  123. */
  124. public function getWebName()
  125. {
  126. return (($this->getIsNoEmpty()) ? ($this->path . '/' . $this->filename) : '_.gif');
  127. }
  128. public function __toString()
  129. {
  130. return FileHelper::toString($this);
  131. }
  132. }