Created
January 17, 2014 20:14
-
-
Save mkay/8480595 to your computer and use it in GitHub Desktop.
MODX snippet to generate a phpthumb sized random image from a given directory.
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 | |
/* | |
Requirements: MODX, phpthumbof | |
*/ | |
$folder = 'img/random/'; // change to your random img dir | |
$height = 120; // desired height in px | |
$width = 940; // desired width in px | |
$crop = 'C'; // zoom crop where? T,R,B,L,C (top,right,bottom,left,center) | |
// space seperated list of extensions | |
$exts = 'jpg jpeg png'; | |
$files = array(); $i = -1; | |
if ('' == $folder) $folder = './'; | |
$handle = opendir($folder); | |
$exts = explode(' ', $exts); | |
while (false !== ($file = readdir($handle))) { | |
foreach($exts as $ext) { // for each extension check the extension | |
if (preg_match('/\.'.$ext.'$/i', $file, $test)) { // faster than ereg, case insensitive | |
$files[] = $file; // it’s good | |
++$i; | |
} | |
} | |
} | |
closedir($handle); // we’re not using it anymore | |
mt_srand((double)microtime()*1000000); // seed for PHP < 4.2 | |
$rand = mt_rand(0, $i); // $i was incremented as we went along | |
// feed it to phpthumbof | |
$output = $modx->runSnippet('phpthumbof',array( | |
'input' => $folder.$files[$rand], | |
'options' => '&w='.$width.'&h='.$height.'&zc='.$crop.'&aoe=0&far=0' | |
)); | |
// output | |
echo '<img src="'.$output.'" alt="" />'; | |
//?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment