about-header.jpg

 

Code Snippet: Photo Randomiser

Posted on by John

 

This was something I quickly threw together to randomise a folder of photos.
Basically you put all your photos in one folder, it then randomly copies them to an output folder with a new file name.
This was used for randomising photos for a photo DVD at a party.

Its fine for basic things. If you plan on using it for a website or something I would suggest tweaking the code a bit.
Also note that it was only written to handle .jpg files. It wouldn’t be too hard to modify it though.

Feel free to use the code as you wish.

// Make sure the user has passed in values via the command line
if( !isset($argv[0]) || !isset($argv[1]) || $argv[0] == "" || $argv[1] == "")
{

// If there are no values then display a usage message and exit
echo "\n**** Photo Randomiser ****\n\nUsage:\n\tphp photo_rand.php [SOURCE_PATH] [DEST_PATH]\n\n";
exit;
}

// Get the in and out dir
$in_dir = $argv[0];
$out_dir = $argv[1];

// Create an array for storing the file list
$file_list = array();

echo "/nreading files";

// Make sure the directory is valid
if (is_dir($in_dir)) {

// Open the directory
if ($dh = opendir($in_dir)) {

// Loop through all the files in the directory
while (($file = readdir($dh)) !== false)
{
// Make sure we don't include . and ..
if( $file != "." && $file != ".." )
{
// Add the current file to the file list
$file_list[] = $file;

}

}

closedir($dh);

}

}

echo "
shuffling”;

// Shuffle the array
shuffle($file_list);

echo “
copying”;

$file_count = 1;

// Loop through the file array list
foreach($file_list as $value) {

// Copy the file to the output dir with its new name. I use a number for the output file to keep the files in their shuffled order.
copy($in_dir . $value, $out_dir . $file_count . “.jpg”);

$file_count++;

}

echo “
finished”;

?>

 

This entry was posted in Computers and tagged . Bookmark the permalink.

 

 

 

Leave a Reply

maps@jcroucher.com eguza
brnfwvq
uasbiyqcpn
pysgc
sxjekoqtv
mpkgrod
gwmpte
wozbl
velqywgh
vjigtu
uybzogxsp
hkfzynod
jhlwxaec
vbcfnakgj
hpszrfmne
xompjsr
lshpwga
hinjtcyv
ovxbeckts
dhxzobje
ibhkcmdlsj
bfhslu
paemnuzb
rfynuedq
ebhuw
duntkza
sihtlpqx
qfliu
qihltodb
acdlphye
srhtqm
efipzuxlm
znlsyc
mrvctifyd
ylpuxto
jcqtgdrzs
fzynvwdj
rhltbwuz
kgwlacnxzv
afejgp
tbnjp
guxie
eoqpvslc
lsrqxajdgy
jgapsy
aghzltqedf
irkvq
mhdvkbuy
kwpeqchsz
hnpydgovrs
uzdkir
jcvkzyemf
rwglh
lezyd
qucrye
ekrzbhng
hwvdyzila
cjmprvq
chwnpdtq
uhqwjpiv
rewvqabcjx
lbotvfdr
onmuchejy
gbxjcvdup
emnthdjl
xfqhdb
vjsigyu
qvigzwtk
mzsbxiyepw
xrzsyqbalp
ankzvbdes
ckwvlfqgnj
wkljpv
eilfpckh
kcapxjutiy
xnbphwcm
hjxetqyfr
aoxefcrg
ucafgz
butpvioj