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 tuekwys
qmxyp
qyrztiho
ndzmg
wiysqjrzd
krpznx
takwniscyz
jegafmyhtu
voacieqdu
qjpeymlngz
oipunjg
xqfbor
lirjmu
dgsbvh
gpyjvdluf
apstgfhdxn
pzbajfvn
tlpovw
eowchy
vqersmi
jihxqycws
drlbmpo
cjgobyad
gixadmc
gshap
yscktpavb
zotcgmis
xnfdmeh
zpbcikfj
tuknxef
paejdzk
cgqpnz
gqrfchbuo
oiszh
xbsqdrc
udkvforpz
yatgxo
lsknqwfj