// // available at http://phpthumb.sourceforge.net /// ////////////////////////////////////////////////////////////// /// // // See: phpthumb.readme.txt for usage instructions // // /// ////////////////////////////////////////////////////////////// // // // phpThumb.demo.cacheconvert.php // // James Heinrich // // 28 June 2004 // // // // phpThumb() cache filename converter // // based on an idea by Josh Gruenberg (joshgŘtwcny*rr*com) // // Can convert cached files from phpThumb() v1.2.4 and // // newer (v1.1.2 introduced caching, but the cached // // filenames weren't structured in a manner suitable for // // conversion until v1.2.4) // // // ////////////////////////////////////////////////////////////// function RenameFileIfNeccesary($oldfilename) { static $FilenameParameters = array('h', 'w', 'sx', 'sy', 'sw', 'sh', 'bw', 'brx', 'bry', 'bg', 'bgt', 'bc', 'usa', 'usr', 'ust', 'wmf', 'wmp', 'wmm', 'wma', 'xto', 'ra', 'ar', 'iar', 'maxb'); $FileData = array(); $oldbasefilename = basename($oldfilename); $output = 'Found: '.htmlentities($oldfilename, ENT_QUOTES).'
'; if (eregi('^phpThumb_cache\.(.*)\.([0-9]+)\.([0-9]+)\.([0-9]+)\.([0-9]+)\.([0-9]+)\.([0-9]+)\.([0-9]+)\.([0-9]+)\.(jpeg|png|gif)$', $oldbasefilename, $matches)) { $output .= 'matched filename structure for v1.2.4 - v1.3.1
'; // v1.2.4 $FileData['src'] = urldecode($matches[1]); $FileData['h'] = (($matches[2] > 0) ? $matches[2] : null); $FileData['w'] = (($matches[3] > 0) ? $matches[3] : null); $FileData['sx'] = (($matches[4] > 0) ? $matches[4] : null); $FileData['sy'] = (($matches[5] > 0) ? $matches[5] : null); $FileData['sw'] = (($matches[6] > 0) ? $matches[6] : null); $FileData['sh'] = (($matches[7] > 0) ? $matches[7] : null); $FileData['filemtime'] = $matches[8]; $FileData['q'] = (($matches[9] == 75) ? null : $matches[9]); $FileData['format'] = $matches[10]; } elseif (eregi('^phpThumb_cache\.(.*)\.([0-9]+)\.([0-9]+)\.([0-9]+)\.([0-9]+)\.([0-9]+)\.([0-9]+)\.([0-9]+)\.(.*)\.([0-9]+)\.([0-9]+)\.([0-9]+)\.([0-9]+)\.([0-9]+)\.([0-9]+)\.([0-9]+)\.([0-9]+)\.([0-9]+)\.([0-9]+)\.([0-9]+)\.(jpeg|png|gif)$', $oldbasefilename, $matches)) { $output .= 'matched filename structure for v1.3.2 - v1.3.5
'; // v1.3.2: $FileData['src'] = urldecode($matches[1]); $FileData['h'] = (($matches[2] > 0) ? $matches[2] : null); $FileData['w'] = (($matches[3] > 0) ? $matches[3] : null); $FileData['sx'] = (($matches[4] > 0) ? $matches[4] : null); $FileData['sy'] = (($matches[5] > 0) ? $matches[5] : null); $FileData['sw'] = (($matches[6] > 0) ? $matches[6] : null); $FileData['sh'] = (($matches[7] > 0) ? $matches[7] : null); $FileData['bw'] = (($matches[8] > 0) ? $matches[8] : null); $FileData['bg'] = null; // cached filename value was incorrectly stored $FileData['bc'] = null; // cached filename value was incorrectly stored $FileData['usa'] = (($matches[11] > 0) ? $matches[11] : null); $FileData['usr'] = (($matches[12] > 0) ? $matches[12] : null); $FileData['ust'] = (($matches[13] > 0) ? $matches[13] : null); $FileData['wmf'] = null; // cached filename value was incorrectly stored $FileData['wmp'] = null; // cached filename value was ok, but 'wmf' was incorrectly stored making this useless $FileData['wmm'] = null; // cached filename value was ok, but 'wmf' was incorrectly stored making this useless $FileData['wma'] = null; // cached filename value was incorrectly stored $FileData['filemtime'] = $matches[18]; $FileData['quality'] = $matches[19]; $FileData['format'] = $matches[20]; } elseif (eregi('^phpThumb_cache\.(.*)\.([0-9]+)\.([0-9]+)\.(jpeg|png|gif)$', $oldbasefilename, $matches)) { $output .= 'matched filename structure for v1.3.6 - v1.4.0
'; // v1.3.6: $SRCandParameters = $matches[1]; foreach ($FilenameParameters as $parameter) { $SRCandParameters = str_replace('.'.$parameter, "\t".$parameter, $SRCandParameters); } $SRCandParametersArray = explode("\t", $SRCandParameters); $FileData['src'] = urldecode(array_shift($SRCandParametersArray)); for ($i = 4; $i >= 1; $i--) { $MatchedKeys = array(); foreach ($SRCandParametersArray as $key => $parametervaluepair) { if (in_array(substr($parametervaluepair, 0, $i), $FilenameParameters)) { $FileData[substr($parametervaluepair, 0, $i)] = substr($parametervaluepair, $i); $MatchedKeys[] = $key; } } foreach ($MatchedKeys as $key) { unset($SRCandParametersArray[$key]); } if (empty($SRCandParametersArray)) { break; } } $FileData['filemtime'] = $matches[2]; $FileData['quality'] = $matches[3]; $FileData['format'] = $matches[4]; } elseif (eregi('^phpThumb_cache_(.*)_([0-9]+)_([0-9]+)_(jpeg|png|gif)$', $oldbasefilename, $matches)) { $output .= 'matched filename structure for v1.4.1 - v1.4.5
'; // v1.4.1: $SRCandParameters = $matches[1]; foreach ($FilenameParameters as $parameter) { $SRCandParameters = str_replace('_'.$parameter, "\t".$parameter, $SRCandParameters); } $SRCandParametersArray = explode("\t", $SRCandParameters); $FileData['src'] = urldecode(array_shift($SRCandParametersArray)); for ($i = 4; $i >= 1; $i--) { $MatchedKeys = array(); foreach ($SRCandParametersArray as $key => $parametervaluepair) { if (in_array(substr($parametervaluepair, 0, $i), $FilenameParameters)) { $FileData[substr($parametervaluepair, 0, $i)] = substr($parametervaluepair, $i); $MatchedKeys[] = $key; } } foreach ($MatchedKeys as $key) { unset($SRCandParametersArray[$key]); } if (empty($SRCandParametersArray)) { break; } } // unneccesary default values removed in v1.4.6 if (@$FileData['sx'] == '0') { unset($FileData['sx']); } if (@$FileData['sy'] == '0') { unset($FileData['sy']); } if (@$FileData['bg'] == 'FFFFFF') { unset($FileData['bg']); } if (@$FileData['bc'] == '000000') { unset($FileData['bc']); } if (@$FileData['wmp'] == '50') { unset($FileData['wmp']); } if (@$FileData['wmm'] == '5') { unset($FileData['wmm']); } if (@$FileData['wma'] == 'BR') { unset($FileData['wma']); } if (@$FileData['iar'] == '') { unset($FileData['iar']); } $FileData['filemtime'] = $matches[2]; $FileData['quality'] = $matches[3]; $FileData['format'] = $matches[4]; } elseif (eregi('^phpThumb_cache_(.*)_([0-9]+)_q([0-9]+)_(jpeg|png|gif)$', $oldbasefilename, $matches)) { $output .= 'matched filename structure for v1.4.6+ (no need to rename)
'; } else { $output .= 'did not match any know filename structure (although could be from v1.1.2 - v1.2.3) - cannot use this file
'; } if (!empty($FileData)) { // v1.4.6 onwards $cache_filename = 'phpThumb_cache'; $cache_filename .= '_'.urlencode($FileData['src']); foreach ($FilenameParameters as $key) { if (isset($FileData[$key])) { $cache_filename .= '_'.$key.$FileData[$key]; } } $cache_filename .= '_'.$FileData['filemtime']; $cache_filename .= '_q'.$FileData['quality']; $cache_filename .= '_'.$FileData['format']; $output .= 'attempting to rename to "'.htmlentities($cache_filename, ENT_QUOTES).'"
'; if (file_exists(dirname($oldfilename).'/'.$cache_filename)) { $output .= 'destination file already exists! cannot rename

'; echo $output; return false; } elseif (rename($oldfilename, dirname($oldfilename).'/'.$cache_filename)) { $output .= 'success!

'; echo $output; return true; } $output .= 'failed to rename! (check permissions?)

'; echo $output; return false; } $output .= 'not renaming this file

'; echo $output; return true; } echo 'phpThumb() cache converter'; if (!empty($_POST['cachedir'])) { $cachedir = realpath($_POST['cachedir']); $skipped = 0; if (is_dir($cachedir)) { if ($dir = opendir($cachedir)) { echo 'Processing directory '.htmlentities($cachedir).'

'; while ($fileName = readdir($dir)) { if (ereg('^phpThumb_cache', $fileName)) { RenameFileIfNeccesary($cachedir.'/'.$fileName); } elseif (!is_dir($cachedir.'/'.$fileName)) { $skipped++; } } } else { echo 'Cannot open directory "'.htmlentities($cachedir).'"
'; } } else { echo '"'.htmlentities($cachedir).'" is not a directory!
'; } if ($skipped > 0) { echo 'skipped '.$skipped.' files
'; } echo '
'; } echo '
'; echo 'Enter the directory you wish to convert from old-style phpThumb() cache filenames to the current naming standard:
'; echo ' '; echo ''; echo '
'; ?>