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.

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