Skip to content

Instantly share code, notes, and snippets.

@drmmr763
Created September 9, 2014 20:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save drmmr763/e9592e5b44f71d8872a0 to your computer and use it in GitHub Desktop.
Save drmmr763/e9592e5b44f71d8872a0 to your computer and use it in GitHub Desktop.
Rename K2 images using their file name structure
<?php
/*
* Author Chad Windnagle
* Handy script for renemaing k2 item images
* useful for when you've integrated two k2 databases and had colliding item IDs
* lets you batch rename tons of file names with K2s md5 naming strategy
*
*/
$start = 15; // start id number of the k2 item
$end = 116; // end id number of the k2 items
$upNum = 190; // used to offset the IDs to avoid collisions
$basePath = __DIR__; // set a base path. I ran along side the images
for ($i = $start; $i < $end+1; $i++)
{
$oldmd5 = md5('Image' . $i); // the old file name md5 hash is Image + ID
$newmd5 = md5('Image' . ($i + $upNum)); // new file name is the same, offset the ID to avoid colliding
$oldImagePath = __DIR__ . '/' . $oldmd5 . '.jpg'; // use the size when renaming cached images
$newImagePath = __DIR__ . '/' . $newmd5 . '.jpg'; // don't use anything when renaming src files
echo $i . ' - ';
if (file_exists($oldImagePath))
{
echo $oldImagePath;
echo 'renaming to ' . $newImagePath;
rename($oldImagePath, $newImagePath);
}
echo "<br />";
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment