/modules/tiled-gallery/tiled-gallery/tiled-gallery-rectangular.php

  1. <?php 
  2. require_once dirname( __FILE__ ) . '/tiled-gallery-layout.php'; 
  3. require_once dirname( __FILE__ ) . '/tiled-gallery-shape.php'; 
  4. require_once dirname( __FILE__ ) . '/tiled-gallery-item.php'; 
  5.  
  6. class Jetpack_Tiled_Gallery_Layout_Rectangular extends Jetpack_Tiled_Gallery_Layout { 
  7. protected $type = 'rectangular'; 
  8.  
  9. public function HTML( $context = array() ) { 
  10. $grouper = new Jetpack_Tiled_Gallery_Grouper( $this->attachments ); 
  11. Jetpack_Tiled_Gallery_Shape::reset_last_shape(); 
  12.  
  13. return parent::HTML( array( 'rows' => $grouper->grouped_images ) ); 
  14.  
  15. class Jetpack_Tiled_Gallery_Layout_Columns extends Jetpack_Tiled_Gallery_Layout { 
  16. protected $type = 'rectangular'; // It doesn't need separate template for now 
  17.  
  18. public function HTML( $context = array() ) { 
  19. $grouper = new Jetpack_Tiled_Gallery_Grouper( $this->attachments, array( 'Three_Columns', 'Two' ) ); 
  20.  
  21. return parent::HTML( array( 'rows' => $grouper->grouped_images ) ); 
  22.  
  23. // Alias 
  24. class Jetpack_Tiled_Gallery_Layout_Rectangle extends Jetpack_Tiled_Gallery_Layout_Rectangular {} 
  25.  
  26. // Image grouping and HTML generation logic 
  27. class Jetpack_Tiled_Gallery_Grouper { 
  28. public $margin = 4; 
  29.  
  30. // This list is ordered. If you put a shape that's likely to occur on top, it will happen all the time. 
  31. public $shapes = array( 
  32. 'Reverse_Symmetric_Row',  
  33. 'Long_Symmetric_Row',  
  34. 'Symmetric_Row',  
  35. 'One_Three',  
  36. 'Three_One',  
  37. 'One_Two',  
  38. 'Five',  
  39. 'Four',  
  40. 'Three',  
  41. 'Two_One',  
  42. 'Panoramic' 
  43. ); 
  44.  
  45. public function __construct( $attachments, $shapes = array() ) { 
  46. $content_width = Jetpack_Tiled_Gallery::get_content_width(); 
  47. $ua_info = new Jetpack_User_Agent_Info(); 
  48.  
  49. $this->overwrite_shapes( $shapes ); 
  50. $this->last_shape = ''; 
  51. $this->images = $this->get_images_with_sizes( $attachments ); 
  52. $this->grouped_images = $this->get_grouped_images(); 
  53. $this->apply_content_width( $content_width ); 
  54.  
  55. public function overwrite_shapes( $shapes ) { 
  56. if ( ! empty( $shapes ) ) { 
  57. $this->shapes = $shapes; 
  58.  
  59. public function get_current_row_size() { 
  60. $images_left = count( $this->images ); 
  61. if ( $images_left < 3 ) 
  62. return array_fill( 0, $images_left, 1 ); 
  63.  
  64. foreach ( $this->shapes as $shape_name ) { 
  65. $class_name = "Jetpack_Tiled_Gallery_$shape_name"; 
  66. $shape = new $class_name( $this->images ); 
  67. if ( $shape->is_possible() ) { 
  68. Jetpack_Tiled_Gallery_Shape::set_last_shape( $class_name ); 
  69. return $shape->shape; 
  70.  
  71. Jetpack_Tiled_Gallery_Shape::set_last_shape( 'Two' ); 
  72. return array( 1, 1 ); 
  73.  
  74. public function get_images_with_sizes( $attachments ) { 
  75. $images_with_sizes = array(); 
  76.  
  77. foreach ( $attachments as $image ) { 
  78. $meta = wp_get_attachment_metadata( $image->ID ); 
  79. $image->width_orig = ( isset( $meta['width'] ) && $meta['width'] > 0 )? $meta['width'] : 1; 
  80. $image->height_orig = ( isset( $meta['height'] ) && $meta['height'] > 0 )? $meta['height'] : 1; 
  81. $image->ratio = $image->width_orig / $image->height_orig; 
  82. $image->ratio = $image->ratio? $image->ratio : 1; 
  83. $images_with_sizes[] = $image; 
  84.  
  85. return $images_with_sizes; 
  86.  
  87. public function read_row() { 
  88. $vector = $this->get_current_row_size(); 
  89.  
  90. $row = array(); 
  91. foreach ( $vector as $group_size ) { 
  92. $row[] = new Jetpack_Tiled_Gallery_Group( array_splice( $this->images, 0, $group_size ) ); 
  93.  
  94. return $row; 
  95.  
  96. public function get_grouped_images() { 
  97. $grouped_images = array(); 
  98.  
  99. while( !empty( $this->images ) ) { 
  100. $grouped_images[] = new Jetpack_Tiled_Gallery_Row( $this->read_row() ); 
  101.  
  102. return $grouped_images; 
  103.  
  104. // todo: split in functions 
  105. // todo: do not stretch images 
  106. public function apply_content_width( $width ) { 
  107. foreach ( $this->grouped_images as $row ) { 
  108. $row->width = $width; 
  109. $row->raw_height = 1 / $row->ratio * ( $width - $this->margin * ( count( $row->groups ) - $row->weighted_ratio ) ); 
  110. $row->height = round( $row->raw_height ); 
  111.  
  112. $this->calculate_group_sizes( $row ); 
  113.  
  114. public function calculate_group_sizes( $row ) { 
  115. // Storing the calculated group heights in an array for rounding them later while preserving their sum 
  116. // This fixes the rounding error that can lead to a few ugly pixels sticking out in the gallery 
  117. $group_widths_array = array(); 
  118. foreach ( $row->groups as $group ) { 
  119. $group->height = $row->height; 
  120. // Storing the raw calculations in a separate property to prevent rounding errors from cascading down and for diagnostics 
  121. $group->raw_width = ( $row->raw_height - $this->margin * count( $group->images ) ) * $group->ratio + $this->margin; 
  122. $group_widths_array[] = $group->raw_width; 
  123. $rounded_group_widths_array = Jetpack_Constrained_Array_Rounding::get_rounded_constrained_array( $group_widths_array, $row->width ); 
  124.  
  125. foreach ( $row->groups as $group ) { 
  126. $group->width = array_shift( $rounded_group_widths_array ); 
  127. $this->calculate_image_sizes( $group ); 
  128.  
  129. public function calculate_image_sizes( $group ) { 
  130. // Storing the calculated image heights in an array for rounding them later while preserving their sum 
  131. // This fixes the rounding error that can lead to a few ugly pixels sticking out in the gallery 
  132. $image_heights_array = array(); 
  133. foreach ( $group->images as $image ) { 
  134. $image->width = $group->width - $this->margin; 
  135. // Storing the raw calculations in a separate property for diagnostics 
  136. $image->raw_height = ( $group->raw_width - $this->margin ) / $image->ratio; 
  137. $image_heights_array[] = $image->raw_height; 
  138.  
  139. $image_height_sum = $group->height - count( $image_heights_array ) * $this->margin; 
  140. $rounded_image_heights_array = Jetpack_Constrained_Array_Rounding::get_rounded_constrained_array( $image_heights_array, $image_height_sum ); 
  141.  
  142. foreach ( $group->images as $image ) { 
  143. $image->height = array_shift( $rounded_image_heights_array ); 
  144.  
  145. class Jetpack_Tiled_Gallery_Row { 
  146. public function __construct( $groups ) { 
  147. $this->groups = $groups; 
  148. $this->ratio = $this->get_ratio(); 
  149. $this->weighted_ratio = $this->get_weighted_ratio(); 
  150.  
  151. public function get_ratio() { 
  152. $ratio = 0; 
  153. foreach ( $this->groups as $group ) { 
  154. $ratio += $group->ratio; 
  155. return $ratio > 0? $ratio : 1; 
  156.  
  157. public function get_weighted_ratio() { 
  158. $weighted_ratio = 0; 
  159. foreach ( $this->groups as $group ) { 
  160. $weighted_ratio += $group->ratio * count( $group->images ); 
  161. return $weighted_ratio > 0 ? $weighted_ratio : 1; 
  162.  
  163. class Jetpack_Tiled_Gallery_Group { 
  164. public function __construct( $images ) { 
  165. $this->images = $images; 
  166. $this->ratio = $this->get_ratio(); 
  167.  
  168. public function get_ratio() { 
  169. $ratio = 0; 
  170. foreach ( $this->images as $image ) { 
  171. if ( $image->ratio ) 
  172. $ratio += 1/$image->ratio; 
  173. if ( !$ratio ) 
  174. return 1; 
  175.  
  176. return 1/$ratio; 
  177.  
  178. public function items( $needs_attachment_link, $grayscale ) { 
  179. $items = array(); 
  180. foreach ( $this->images as $image ) { 
  181. $items[] = new Jetpack_Tiled_Gallery_Rectangular_Item( $image, $needs_attachment_link, $grayscale ); 
  182.  
  183. return $items; 
.