MediaItem.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. <?php
  2. namespace common\modules\attachment\models;
  3. class MediaItem
  4. {
  5. const TYPE_FILE = 'file';
  6. const TYPE_FOLDER = 'folder';
  7. const FILE_TYPE_IMAGE = 'image';
  8. const FILE_TYPE_VIDEO = 'video';
  9. const FILE_TYPE_AUDIO = 'audio';
  10. const FILE_TYPE_DOCUMENT = 'document';
  11. /**
  12. * @var string Specifies the item path relative to the Library root.
  13. */
  14. public $path;
  15. /**
  16. * @var integer Specifies the item size.
  17. * For files the item size is measured in bytes. For folders it
  18. * contains the number of files in the folder.
  19. */
  20. public $size;
  21. /**
  22. * @var integer Contains the last modification time (Unix timestamp).
  23. */
  24. public $lastModified;
  25. /**
  26. * @var string Specifies the item type.
  27. */
  28. public $type;
  29. /**
  30. * @var string Specifies the public URL of the item.
  31. */
  32. public $publicUrl;
  33. /**
  34. * @var array Contains a default list of files and directories to ignore.
  35. * The list can be customized with the following configuration options:
  36. * - cms.storage.media.image_extensions
  37. * - cms.storage.media.video_extensions
  38. * - cms.storage.media.audo_extensions
  39. */
  40. protected static $defaultTypeExtensions = [
  41. 'image' => ['gif', 'png', 'jpg', 'jpeg', 'bmp'],
  42. 'video' => ['mp4', 'avi', 'mov', 'mpg', 'mpeg', 'mkv', 'webm'],
  43. 'audio' => ['mp3', 'wav', 'wma', 'm4a', 'ogg']
  44. ];
  45. protected static $imageExtensions;
  46. protected static $videoExtensions;
  47. protected static $audioExtensions;
  48. public function __construct($path, $size, $lastModified, $type, $publicUrl)
  49. {
  50. $this->path = $path;
  51. $this->size = $size;
  52. $this->lastModified = $lastModified;
  53. $this->type = $type;
  54. $this->publicUrl = $publicUrl;
  55. }
  56. /**
  57. *
  58. * @param Attachment $attachment
  59. */
  60. public static function createFromAttachment($attachment)
  61. {
  62. $item = new static($attachment->path,$attachment->size, $attachment->updated_at, 'file', $attachment->url);
  63. return $item;
  64. }
  65. public function isFile()
  66. {
  67. return $this->type == self::TYPE_FILE;
  68. }
  69. public function isImage()
  70. {
  71. return $this->getFileType() === self::FILE_TYPE_IMAGE;
  72. }
  73. /**
  74. * Returns the file type by its name.
  75. * The known file types are: image, video, audio, document
  76. * @return string Returns the file type or NULL if the item is a folder.
  77. */
  78. public function getFileType()
  79. {
  80. if (!$this->isFile()) {
  81. return null;
  82. }
  83. if (!self::$imageExtensions) {
  84. self::$imageExtensions = \Yii::$app->get("config")->get('attachment.image_extensions', self::$defaultTypeExtensions['image']);
  85. self::$videoExtensions = \Yii::$app->get("config")->get('attachment.video_extensions', self::$defaultTypeExtensions['video']);
  86. self::$audioExtensions = \Yii::$app->get("config")->get('attachment.audio_extensions', self::$defaultTypeExtensions['audio']);
  87. }
  88. $extension = pathinfo($this->path, PATHINFO_EXTENSION);
  89. if (!strlen($extension)) {
  90. return self::FILE_TYPE_DOCUMENT;
  91. }
  92. if (in_array($extension, self::$imageExtensions)) {
  93. return self::FILE_TYPE_IMAGE;
  94. }
  95. if (in_array($extension, self::$videoExtensions)) {
  96. return self::FILE_TYPE_VIDEO;
  97. }
  98. if (in_array($extension, self::$audioExtensions)) {
  99. return self::FILE_TYPE_AUDIO;
  100. }
  101. return self::FILE_TYPE_DOCUMENT;
  102. }
  103. /**
  104. * Returns the item size as string.
  105. * For file-type items the size is the number of bytes. For folder-type items
  106. * the size is the number of items contained by the item.
  107. * @return string Returns the size as string.
  108. */
  109. public function sizeToString()
  110. {
  111. return \Yii::$app->getFormatter()->asShortSize($this->size);
  112. }
  113. public function getResolution()
  114. {
  115. $size = getimagesize($this->publicUrl);
  116. return $size[0] . "x" . $size[1];
  117. }
  118. /**
  119. * Returns the item last modification date as string.
  120. * @return string Returns the item's last modification date as string.
  121. */
  122. public function lastModifiedAsString()
  123. {
  124. if (!$this->lastModified) {
  125. return null;
  126. }
  127. return \Yii::$app->getFormatter()->asDatetime($this->lastModified);
  128. }
  129. }