Skip to content

Instantly share code, notes, and snippets.

@pogla
Last active January 21, 2020 10:49
Show Gist options
  • Save pogla/2cb652b44d8aa06c1121ad0e483de8f7 to your computer and use it in GitHub Desktop.
Save pogla/2cb652b44d8aa06c1121ad0e483de8f7 to your computer and use it in GitHub Desktop.
Rename all images in a folder based on seed keyword array
<?php
$seed_words = [
'word1',
'word2',
'word3',
];
$files = [];
if ($handle = opendir('.')) {
while (false !== ($entry = readdir($handle))) {
if ($entry != "." && $entry != ".." && $entry[0] != '.') {
if ( strpos( strtolower( $entry ) , '.jpg') !== false || strpos( strtolower( $entry ) , '.png') !== false ) {
$files[] = $entry;
}
}
}
closedir($handle);
}
foreach ( $files as $key => $file ) {
rename( $file, $key . $seed_words[ rand(0, count( $seed_words ) - 1 ) ] . '.jpg' );
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment