Skip to content

Instantly share code, notes, and snippets.

@neutraltone
Last active July 25, 2016 12:46
Show Gist options
  • Save neutraltone/7465190 to your computer and use it in GitHub Desktop.
Save neutraltone/7465190 to your computer and use it in GitHub Desktop.
Random layout generator for Photoset jQuery plugin (http://stylehatch.github.com/photoset-grid/) based on the number of given images.
<?php
$image_counts = array(1,5,8,4,3,6,2,6,6,5,10);
$possible = array(1,2,4);
$total_counts = array();
foreach ($image_counts as $imgcount) {
$done = false;
$totals = array();
$total = 0;
while ($done === false) {
// Get a random number from possibilities
$qty = $possible[array_rand($possible)];
if ($qty + array_sum($totals) < $imgcount) {
$totals[] = $qty;
} elseif ($qty + array_sum($totals) === $imgcount) {
$totals[] = $qty;
$done = true;
}
}
$total_counts[] = implode('', $totals);
}
var_dump($image_counts, $total_counts);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment