Jetpack_Tiled_Gallery_Grouper

The Jetpack by WordPress.com Jetpack Tiled Gallery Grouper class.

Defined (1)

The class is defined in the following location(s).

/modules/tiled-gallery/tiled-gallery/tiled-gallery-rectangular.php  
  1. class Jetpack_Tiled_Gallery_Grouper { 
  2. public $margin = 4; 
  3.  
  4. // This list is ordered. If you put a shape that's likely to occur on top, it will happen all the time. 
  5. public $shapes = array( 
  6. 'Reverse_Symmetric_Row',  
  7. 'Long_Symmetric_Row',  
  8. 'Symmetric_Row',  
  9. 'One_Three',  
  10. 'Three_One',  
  11. 'One_Two',  
  12. 'Five',  
  13. 'Four',  
  14. 'Three',  
  15. 'Two_One',  
  16. 'Panoramic' 
  17. ); 
  18.  
  19. public function __construct( $attachments, $shapes = array() ) { 
  20. $content_width = Jetpack_Tiled_Gallery::get_content_width(); 
  21. $ua_info = new Jetpack_User_Agent_Info(); 
  22.  
  23. $this->overwrite_shapes( $shapes ); 
  24. $this->last_shape = ''; 
  25. $this->images = $this->get_images_with_sizes( $attachments ); 
  26. $this->grouped_images = $this->get_grouped_images(); 
  27. $this->apply_content_width( $content_width ); 
  28.  
  29. public function overwrite_shapes( $shapes ) { 
  30. if ( ! empty( $shapes ) ) { 
  31. $this->shapes = $shapes; 
  32.  
  33. public function get_current_row_size() { 
  34. $images_left = count( $this->images ); 
  35. if ( $images_left < 3 ) 
  36. return array_fill( 0, $images_left, 1 ); 
  37.  
  38. foreach ( $this->shapes as $shape_name ) { 
  39. $class_name = "Jetpack_Tiled_Gallery_$shape_name"; 
  40. $shape = new $class_name( $this->images ); 
  41. if ( $shape->is_possible() ) { 
  42. Jetpack_Tiled_Gallery_Shape::set_last_shape( $class_name ); 
  43. return $shape->shape; 
  44.  
  45. Jetpack_Tiled_Gallery_Shape::set_last_shape( 'Two' ); 
  46. return array( 1, 1 ); 
  47.  
  48. public function get_images_with_sizes( $attachments ) { 
  49. $images_with_sizes = array(); 
  50.  
  51. foreach ( $attachments as $image ) { 
  52. $meta = wp_get_attachment_metadata( $image->ID ); 
  53. $image->width_orig = ( isset( $meta['width'] ) && $meta['width'] > 0 )? $meta['width'] : 1; 
  54. $image->height_orig = ( isset( $meta['height'] ) && $meta['height'] > 0 )? $meta['height'] : 1; 
  55. $image->ratio = $image->width_orig / $image->height_orig; 
  56. $image->ratio = $image->ratio? $image->ratio : 1; 
  57. $images_with_sizes[] = $image; 
  58.  
  59. return $images_with_sizes; 
  60.  
  61. public function read_row() { 
  62. $vector = $this->get_current_row_size(); 
  63.  
  64. $row = array(); 
  65. foreach ( $vector as $group_size ) { 
  66. $row[] = new Jetpack_Tiled_Gallery_Group( array_splice( $this->images, 0, $group_size ) ); 
  67.  
  68. return $row; 
  69.  
  70. public function get_grouped_images() { 
  71. $grouped_images = array(); 
  72.  
  73. while( !empty( $this->images ) ) { 
  74. $grouped_images[] = new Jetpack_Tiled_Gallery_Row( $this->read_row() ); 
  75.  
  76. return $grouped_images; 
  77.  
  78. // todo: split in functions 
  79. // todo: do not stretch images 
  80. public function apply_content_width( $width ) { 
  81. foreach ( $this->grouped_images as $row ) { 
  82. $row->width = $width; 
  83. $row->raw_height = 1 / $row->ratio * ( $width - $this->margin * ( count( $row->groups ) - $row->weighted_ratio ) ); 
  84. $row->height = round( $row->raw_height ); 
  85.  
  86. $this->calculate_group_sizes( $row ); 
  87.  
  88. public function calculate_group_sizes( $row ) { 
  89. // Storing the calculated group heights in an array for rounding them later while preserving their sum 
  90. // This fixes the rounding error that can lead to a few ugly pixels sticking out in the gallery 
  91. $group_widths_array = array(); 
  92. foreach ( $row->groups as $group ) { 
  93. $group->height = $row->height; 
  94. // Storing the raw calculations in a separate property to prevent rounding errors from cascading down and for diagnostics 
  95. $group->raw_width = ( $row->raw_height - $this->margin * count( $group->images ) ) * $group->ratio + $this->margin; 
  96. $group_widths_array[] = $group->raw_width; 
  97. $rounded_group_widths_array = Jetpack_Constrained_Array_Rounding::get_rounded_constrained_array( $group_widths_array, $row->width ); 
  98.  
  99. foreach ( $row->groups as $group ) { 
  100. $group->width = array_shift( $rounded_group_widths_array ); 
  101. $this->calculate_image_sizes( $group ); 
  102.  
  103. public function calculate_image_sizes( $group ) { 
  104. // Storing the calculated image heights in an array for rounding them later while preserving their sum 
  105. // This fixes the rounding error that can lead to a few ugly pixels sticking out in the gallery 
  106. $image_heights_array = array(); 
  107. foreach ( $group->images as $image ) { 
  108. $image->width = $group->width - $this->margin; 
  109. // Storing the raw calculations in a separate property for diagnostics 
  110. $image->raw_height = ( $group->raw_width - $this->margin ) / $image->ratio; 
  111. $image_heights_array[] = $image->raw_height; 
  112.  
  113. $image_height_sum = $group->height - count( $image_heights_array ) * $this->margin; 
  114. $rounded_image_heights_array = Jetpack_Constrained_Array_Rounding::get_rounded_constrained_array( $image_heights_array, $image_height_sum ); 
  115.  
  116. foreach ( $group->images as $image ) { 
  117. $image->height = array_shift( $rounded_image_heights_array );