Last active
July 25, 2016 12:46
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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