tiles.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. <?php
  2. namespace OC\Pictures;
  3. require_once('lib/base.php');
  4. require_once('managers.php');
  5. const TAG = 'Pictures';
  6. const IMAGE_WIDTH = 150;
  7. class TileBase {
  8. public function getWidth() { return false; }
  9. public function getHeight() { return IMAGE_WIDTH; }
  10. public function getOnHoverAction() { return false; }
  11. public function getOnOutAction() { return false; }
  12. public function getOnClickAction() { return false; }
  13. public function getDisplayedLayer() { return false; }
  14. public function getTileProportion() { return false; }
  15. public function get() { return false; }
  16. }
  17. class TilesLine {
  18. public function __construct() {
  19. $this->tiles_array = array();
  20. }
  21. public function setAvailableSpace($space) {
  22. $available_space = $space;
  23. }
  24. public function getTilesCount() {
  25. return count($this->tiles_array);
  26. }
  27. public function addTile($tile) {
  28. array_push($this->tiles_array, $tile);
  29. }
  30. public function getLeftSpace() {
  31. $occupied_space = 0;
  32. for ($i = 0; $i < count($this->tiles_array); $i++) {
  33. $occupied_space += $this->tiles_array[$i]->getWidth();
  34. }
  35. return $this->available_space - $occupied_space;
  36. }
  37. public function tileWillFit($tile) {
  38. return $this->getLeftSpace() > $tile->getWidth();
  39. }
  40. public function get() {
  41. $r = '<div class="line gallery_div">';
  42. for ($i = 0; $i < count($this->tiles_array); $i++) {
  43. $img_w = $this->tiles_array[$i]->getWidth();
  44. $extra = '';
  45. if ($img_w != IMAGE_WIDTH) $extra = ' style="width:'.$img_w.'px"';
  46. $r .= '<div class="gallery_div" '.$extra.' onmouseover="'.$this->tiles_array[$i]->getOnHoverAction().'" onmouseout="'.$this->tiles_array[$i]->getOnOutAction().'" onclick="'.$this->tiles_array[$i]->getOnClickAction().'">'.$this->tiles_array[$i]->get().'</div>';
  47. }
  48. $r .= '</div>';
  49. return $r;
  50. }
  51. private $tiles_array;
  52. private $available_space;
  53. }
  54. class TileSingle extends TileBase {
  55. public function __construct($path) {
  56. \OC_Log::write(TAG, 'Loading file from path '.$path, \OC_Log::DEBUG);
  57. $this->file_path = $path;
  58. /* $this->image = new \OC_Image();
  59. if (!$this->image->loadFromFile($this->file_path)) {
  60. \OC_Log::write(TAG, 'Loading file filed', \OC_Log::ERROR);
  61. return;
  62. }
  63. $this->image->fixOrientation();*/
  64. }
  65. public function getWidth() {
  66. $a = ThumbnailsManager::getInstance()->getThumbnailInfo($this->file_path);
  67. return $a['width'];
  68. }
  69. public function get($extra = '') {
  70. // !HACK! file path needs to be encoded twice because files app decode twice url, so any special chars like + or & in filename
  71. // !HACK! will result in failing of opening them
  72. return '<a rel="images" title="'.basename($this->getPath()).'" href="'.\OCP\Util::linkTo('files', 'download.php').'?file='.urlencode(urlencode($this->getPath())).'"><img rel="images" src="'.\OCP\Util::linkTo('gallery', 'ajax/thumbnail.php').'&filepath='.urlencode($this->getPath()).'" '.$extra.'></a>';
  73. }
  74. public function getMiniatureSrc() {
  75. return \OCP\Util::linkTo('gallery', 'ajax/thumbnail.php').'&filepath='.urlencode($this->getPath());
  76. }
  77. public function getPath() {
  78. return $this->file_path;
  79. }
  80. public function getOnClickAction() {
  81. return '';//'javascript:openFile(\''.$this->file_path.'\');';
  82. }
  83. private $file_path;
  84. private $image;
  85. }
  86. class TileStack extends TileBase {
  87. const STACK_REPRESENTATIVES = 3;
  88. public function __construct($path_array, $stack_name) {
  89. $this->tiles_array = array();
  90. $this->stack_name = $stack_name;
  91. for ($i = 0; $i < count($path_array) && $i < self::STACK_REPRESENTATIVES; $i++) {
  92. $tile = new TileSingle($path_array[$i]);
  93. array_push($this->tiles_array, $tile);
  94. }
  95. }
  96. public function forceSize($width_must_fit=false) {
  97. for ($i = 0; $i < count($this->tiles_array); $i++)
  98. $this->tiles_array[$i]->forceSize(true);
  99. }
  100. public function getWidth() {
  101. $max = 0;
  102. for ($i = 0; $i < count($this->tiles_array); $i++) {
  103. $max = max($max, $this->tiles_array[$i]->getWidth());
  104. }
  105. return min(IMAGE_WIDTH, $max);
  106. }
  107. public function get() {
  108. $r = '<div class="title gallery_div">'.$this->stack_name.'</div>';
  109. for ($i = 0; $i < count($this->tiles_array); $i++) {
  110. $top = rand(-5, 5);
  111. $left = rand(-5, 5);
  112. $img_w = $this->tiles_array[$i]->getWidth();
  113. $extra = '';
  114. if ($img_w < IMAGE_WIDTH) {
  115. $extra = 'width:'.$img_w.'px;';
  116. }
  117. $r .= '<div class="miniature_border gallery_div" style="background-image:url(\''.$this->tiles_array[$i]->getMiniatureSrc().'\');margin-top:'.$top.'px; margin-left:'.$left.'px;'.$extra.'"></div>';
  118. }
  119. return $r;
  120. }
  121. public function getOnHoverAction() {
  122. return 'javascript:t(this);return false;';
  123. }
  124. public function getOnOutAction() {
  125. return 'javascript:o(this);return false;';
  126. }
  127. public function getCount() {
  128. return count($this->tiles_array);
  129. }
  130. public function getOnClickAction() {
  131. return 'javascript:openNewGal(\''.$this->stack_name.'\');';
  132. }
  133. private $tiles_array;
  134. private $stack_name;
  135. }
  136. ?>