root/devel-backup/units/phpthumb/phpThumb.demo.cacheconvert.php

Revision 45, 9.8 kB (checked in by sven, 3 years ago)

dos2unix to clean line endings, set svn property eol-style native, some whitespace homogenisation

  • Property svn:eol-style set to native
Line 
1 <?php
2 //////////////////////////////////////////////////////////////
3 ///  phpThumb() by James Heinrich <info@silisoftware.com>   //
4 //        available at http://phpthumb.sourceforge.net     ///
5 //////////////////////////////////////////////////////////////
6 ///                                                         //
7 // See: phpthumb.readme.txt for usage instructions          //
8 //                                                         ///
9 //////////////////////////////////////////////////////////////
10 //                                                          //
11 // phpThumb.demo.cacheconvert.php                           //
12 // James Heinrich <info@silisoftware.com>                   //
13 // 28 June 2004                                             //
14 //                                                          //
15 // phpThumb() cache filename converter                      //
16 // based on an idea by Josh Gruenberg (joshgØtwcny*rr*com)  //
17 // Can convert cached files from phpThumb() v1.2.4 and      //
18 // newer (v1.1.2 introduced caching, but the cached         //
19 // filenames weren't structured in a manner suitable for    //
20 // conversion until v1.2.4)                                 //
21 //                                                          //
22 //////////////////////////////////////////////////////////////
23
24 function RenameFileIfNeccesary($oldfilename) {
25     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');
26     $FileData = array();
27     $oldbasefilename = basename($oldfilename);
28
29     $output  = 'Found: <font color="blue"><b>'.htmlentities($oldfilename, ENT_QUOTES).'</b></font><br>';
30
31     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)) {
32
33         $output .= '<font color="darkgreen">matched filename structure for v1.2.4 - v1.3.1</font><br>';
34
35         // v1.2.4
36         $FileData['src']       = urldecode($matches[1]);
37         $FileData['h']         = (($matches[2] > 0) ? $matches[2] : null);
38         $FileData['w']         = (($matches[3] > 0) ? $matches[3] : null);
39         $FileData['sx']        = (($matches[4] > 0) ? $matches[4] : null);
40         $FileData['sy']        = (($matches[5] > 0) ? $matches[5] : null);
41         $FileData['sw']        = (($matches[6] > 0) ? $matches[6] : null);
42         $FileData['sh']        = (($matches[7] > 0) ? $matches[7] : null);
43         $FileData['filemtime'] = $matches[8];
44         $FileData['q']         = (($matches[9] == 75) ? null : $matches[9]);
45         $FileData['format']    = $matches[10];
46
47     } 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)) {
48
49         $output .= '<font color="darkgreen">matched filename structure for v1.3.2 - v1.3.5</font><br>';
50
51         // v1.3.2:
52         $FileData['src']       = urldecode($matches[1]);
53         $FileData['h']         = (($matches[2] > 0) ? $matches[2] : null);
54         $FileData['w']         = (($matches[3] > 0) ? $matches[3] : null);
55         $FileData['sx']        = (($matches[4] > 0) ? $matches[4] : null);
56         $FileData['sy']        = (($matches[5] > 0) ? $matches[5] : null);
57         $FileData['sw']        = (($matches[6] > 0) ? $matches[6] : null);
58         $FileData['sh']        = (($matches[7] > 0) ? $matches[7] : null);
59         $FileData['bw']        = (($matches[8] > 0) ? $matches[8] : null);
60         $FileData['bg']        = null;           // cached filename value was incorrectly stored
61         $FileData['bc']        = null;           // cached filename value was incorrectly stored
62         $FileData['usa']       = (($matches[11] > 0) ? $matches[11] : null);
63         $FileData['usr']       = (($matches[12] > 0) ? $matches[12] : null);
64         $FileData['ust']       = (($matches[13] > 0) ? $matches[13] : null);
65         $FileData['wmf']       = null;           // cached filename value was incorrectly stored
66         $FileData['wmp']       = null;           // cached filename value was ok, but 'wmf' was incorrectly stored making this useless
67         $FileData['wmm']       = null;           // cached filename value was ok, but 'wmf' was incorrectly stored making this useless
68         $FileData['wma']       = null;           // cached filename value was incorrectly stored
69         $FileData['filemtime'] = $matches[18];
70         $FileData['quality']   = $matches[19];
71         $FileData['format']    = $matches[20];
72
73     } elseif (eregi('^phpThumb_cache\.(.*)\.([0-9]+)\.([0-9]+)\.(jpeg|png|gif)$', $oldbasefilename, $matches)) {
74
75         $output .= '<font color="darkgreen">matched filename structure for v1.3.6 - v1.4.0</font><br>';
76
77         // v1.3.6:
78         $SRCandParameters = $matches[1];
79         foreach ($FilenameParameters as $parameter) {
80             $SRCandParameters = str_replace('.'.$parameter, "\t".$parameter, $SRCandParameters);
81         }
82         $SRCandParametersArray = explode("\t", $SRCandParameters);
83         $FileData['src'] = urldecode(array_shift($SRCandParametersArray));
84         for ($i = 4; $i >= 1; $i--) {
85             $MatchedKeys = array();
86             foreach ($SRCandParametersArray as $key => $parametervaluepair) {
87                 if (in_array(substr($parametervaluepair, 0, $i), $FilenameParameters)) {
88                     $FileData[substr($parametervaluepair, 0, $i)] = substr($parametervaluepair, $i);
89                     $MatchedKeys[] = $key;
90                 }
91             }
92             foreach ($MatchedKeys as $key) {
93                 unset($SRCandParametersArray[$key]);
94             }
95             if (empty($SRCandParametersArray)) {
96                 break;
97             }
98         }
99
100         $FileData['filemtime'] = $matches[2];
101         $FileData['quality']   = $matches[3];
102         $FileData['format']    = $matches[4];
103
104     } elseif (eregi('^phpThumb_cache_(.*)_([0-9]+)_([0-9]+)_(jpeg|png|gif)$', $oldbasefilename, $matches)) {
105
106         $output .= '<font color="darkgreen">matched filename structure for v1.4.1 - v1.4.5</font><br>';
107
108         // v1.4.1:
109         $SRCandParameters = $matches[1];
110         foreach ($FilenameParameters as $parameter) {
111             $SRCandParameters = str_replace('_'.$parameter, "\t".$parameter, $SRCandParameters);
112         }
113         $SRCandParametersArray = explode("\t", $SRCandParameters);
114         $FileData['src'] = urldecode(array_shift($SRCandParametersArray));
115         for ($i = 4; $i >= 1; $i--) {
116             $MatchedKeys = array();
117             foreach ($SRCandParametersArray as $key => $parametervaluepair) {
118                 if (in_array(substr($parametervaluepair, 0, $i), $FilenameParameters)) {
119                     $FileData[substr($parametervaluepair, 0, $i)] = substr($parametervaluepair, $i);
120                     $MatchedKeys[] = $key;
121                 }
122             }
123             foreach ($MatchedKeys as $key) {
124                 unset($SRCandParametersArray[$key]);
125             }
126             if (empty($SRCandParametersArray)) {
127                 break;
128             }
129         }
130
131         // unneccesary default values removed in v1.4.6
132         if (@$FileData['sx'] == '0') {
133             unset($FileData['sx']);
134         }
135         if (@$FileData['sy'] == '0') {
136             unset($FileData['sy']);
137         }
138         if (@$FileData['bg'] == 'FFFFFF') {
139             unset($FileData['bg']);
140         }
141         if (@$FileData['bc'] == '000000') {
142             unset($FileData['bc']);
143         }
144         if (@$FileData['wmp'] == '50') {
145             unset($FileData['wmp']);
146         }
147         if (@$FileData['wmm'] == '5') {
148             unset($FileData['wmm']);
149         }
150         if (@$FileData['wma'] == 'BR') {
151             unset($FileData['wma']);
152         }
153         if (@$FileData['iar'] == '') {
154             unset($FileData['iar']);
155         }
156
157         $FileData['filemtime'] = $matches[2];
158         $FileData['quality']   = $matches[3];
159         $FileData['format']    = $matches[4];
160
161
162     } elseif (eregi('^phpThumb_cache_(.*)_([0-9]+)_q([0-9]+)_(jpeg|png|gif)$', $oldbasefilename, $matches)) {
163
164         $output .= '<font color="green">matched filename structure for v1.4.6+ (no need to rename)</font><br>';
165
166     } else {
167
168         $output .= '<font color="orange">did not match any know filename structure (although could be from v1.1.2 - v1.2.3) - cannot use this file</font><br>';
169
170     }
171
172     if (!empty($FileData)) {
173         // v1.4.6 onwards
174         $cache_filename  = 'phpThumb_cache';
175         $cache_filename .= '_'.urlencode($FileData['src']);
176         foreach ($FilenameParameters as $key) {
177             if (isset($FileData[$key])) {
178                 $cache_filename .= '_'.$key.$FileData[$key];
179             }
180         }
181         $cache_filename .= '_'.$FileData['filemtime'];
182         $cache_filename .= '_q'.$FileData['quality'];
183         $cache_filename .= '_'.$FileData['format'];
184
185         $output .= 'attempting to rename to "'.htmlentities($cache_filename, ENT_QUOTES).'"<br>';
186         if (file_exists(dirname($oldfilename).'/'.$cache_filename)) {
187
188             $output .= '<font color="red">destination file already exists! cannot rename</a><br><br>';
189             echo $output;
190             return false;
191
192         } elseif (rename($oldfilename, dirname($oldfilename).'/'.$cache_filename)) {
193
194             $output .= '<font color="green">success!</a><br><br>';
195             echo $output;
196             return true;
197
198         }
199         $output .= '<font color="red">failed to rename! (check permissions?)</a><br><br>';
200         echo $output;
201         return false;
202     }
203     $output .= '<font color="orange">not renaming this file</font><br><br>';
204     echo $output;
205     return true;
206 }
207
208
209 echo '<html><head><title>phpThumb() cache converter</title></head><body style="font-family: sans-serif; font-size: 9pt;">';
210
211 if (!empty($_POST['cachedir'])) {
212     $cachedir = realpath($_POST['cachedir']);
213     $skipped = 0;
214     if (is_dir($cachedir)) {
215         if ($dir = opendir($cachedir)) {
216             echo 'Processing directory <b>'.htmlentities($cachedir).'</b><br><br>';
217             while ($fileName = readdir($dir)) {
218                 if (ereg('^phpThumb_cache', $fileName)) {
219                     RenameFileIfNeccesary($cachedir.'/'.$fileName);
220                 } elseif (!is_dir($cachedir.'/'.$fileName)) {
221                     $skipped++;
222                 }
223             }
224         } else {
225             echo 'Cannot open directory "<b>'.htmlentities($cachedir).'</b>"<br>';
226         }
227     } else {
228         echo '"<b>'.htmlentities($cachedir).'</b>" is not a directory!<br>';
229     }
230     if ($skipped > 0) {
231         echo '<i>skipped '.$skipped.' files</i><br>';
232     }
233     echo '<hr>';
234 }
235
236 echo '<form method="post" action="'.$_SERVER['PHP_SELF'].'">';
237 echo 'Enter the directory you wish to convert from old-style phpThumb() cache filenames to the current naming standard:<br>';
238 echo '<input type="text" name="cachedir" value="'.@$_POST['cachedir'].'"> ';
239 echo '<input type="submit" value="Convert">';
240 echo '</form></body></html>';
241
242 ?>
Note: See TracBrowser for help on using the browser.