/[dtapublic]/projs/dtats/trunk/projs/20161008_web_page_thumbnail_make/pics_filenames_canonize.php
ViewVC logotype

Annotation of /projs/dtats/trunk/projs/20161008_web_page_thumbnail_make/pics_filenames_canonize.php

Parent Directory Parent Directory | Revision Log Revision Log


Revision 117 - (hide annotations) (download)
Sun Jan 1 04:06:02 2017 UTC (7 years, 5 months ago) by dashley
File size: 12729 byte(s)
Correct minor typo.
1 dashley 107 <?php
2     //-------------------------------------------------------------------------------------------------
3 dashley 110 //$Header$
4 dashley 107 //-------------------------------------------------------------------------------------------------
5 dashley 110 //This source code and any program in which it is compiled/used is provided under the MIT
6     //LICENSE (full license text below).
7 dashley 107 //-------------------------------------------------------------------------------------------------
8 dashley 113 //pics_filenames_canonize.php, Copyright (c) 2016 David T. Ashley
9 dashley 107 //
10 dashley 110 //Permission is hereby granted, free of charge, to any person obtaining a copy of this software and
11     //associated documentation files (the "Software"), to deal in the Software without restriction,
12     //including without limitation the rights to use, copy, modify, merge, publish, distribute,
13     //sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
14     //furnished to do so, subject to the following conditions:
15 dashley 107 //
16 dashley 110 //The above copyright notice and this permission notice shall be included in all copies or
17     //substantial portions of the Software.
18 dashley 107 //
19 dashley 110 //THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT
20     //NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
21     //NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
22     //DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23     //OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24 dashley 107 //-------------------------------------------------------------------------------------------------
25 dashley 113 //This program, a PHP script, modifies all filenames in a directory to a canonical form (no
26     //upper-case letters or unusual characters). This program is part of a 3-program suite designed to
27     //collectively create a web page directly from digital camera files (and of course the web page can
28     //be customized by hand-editing after it is automatically generated).
29 dashley 107 //
30     //The 3 programs in the 3-program suite are:
31     //
32 dashley 115 // pics_filenames_canonize.php (this script):
33 dashley 113 // Converts all names of files in a directory to lower-case, and makes substitutions for any
34     // unusual characters.
35 dashley 107 //
36 dashley 113 // pics_thumbnails_make.php:
37     // Creates thumbnails from recognized image types. The thumbnails are named relative to the
38 dashley 115 // original image file with the suffix "_small". To support shared hosting environments where
39     // CPU utilization of a script may be capped, there is a configuration constant in the script
40     // that will perform only a fixed number of conversions per invocation. If this configuration
41     // option is used, the script should be run repeatedly until it indicates that it has no more
42     // thumbnails to create.
43 dashley 107 //
44 dashley 113 // If any full-sized photos are modified, any corresponding thumbnails should be deleted and
45     // pics_thumbnails_make.php and pics_indexfile_make.php should be run again.
46 dashley 107 //
47 dashley 117 // pics_indexfile_make.php:
48 dashley 113 // Scans a directory and makes an index file ("index2.php") displaying all the thumbnail
49     // images, each of which link to the corresponding full-sized image. The index file is
50     // tailored to Dave Ashley's needs, but the created file can be edited and most of the
51     // content pasted into an HTML file. To avoid the accidental loss of information, any
52     // existing "index2.php" file is renamed out of the way.
53 dashley 107 //
54 dashley 113 //This script is designed to be run manually (rather than automatically invoked as a result of a
55     //web page request). It was written in PHP for convenience simply because DreamHost (the web
56     //hosting company Dave Ashley used at the time these scripts were written) has as part of its
57     //hosting environment PHP with the ImageMagick library compiled in.
58 dashley 107 //
59 dashley 114 //Usually, this script is invoked using "php <path>/pics_filenames_canonize.php", but the method of
60 dashley 113 //invocation may vary based on computing platform details.
61     //-------------------------------------------------------------------------------------------------
62 dashley 107 //C O N F I G U R A T I O N
63     //--------------------------------------------------------------------------------
64     //Configuration switch combinations were not tested. E-mail me any program
65     //corrections (dashley@gmail.com).
66 dashley 113 define ("CFG_PROGNAME", "pics_filenames_canonize.php");
67 dashley 107 //Number of characters per line preferred for console output.
68     define ("CFG_CONSOLE_STD_LINE_LEN", 78);
69     //Number of characters per line preferred for console output.
70     define (
71     "CFG_FILENAME_SUBSTITUTION_MAP",
72     ".." //Dots unchanged.
73     .
74     "aabbccddeeffgghhiijjkkllmmnnooppqqrrssttuuvvwwxxyyzz" //LC letters unchanged.
75     .
76     "AaBbCcDdEeFfGgHhIiJjKkLlMmNnOoPpQqRrSsTtUuVvWwXxYyZz" //UC converted to LC.
77     .
78     "00112233445566778899" //Digits unchanged.
79     .
80     "-_" //Hyphens become underscores,
81     //and anything unmapped
82     //becomes an underscore.
83     );
84     //Pairs of characters, the first is the character to match and the second
85     //is the replacement. Any characters not found in the map will be replaced
86     //by the last character of the constant.
87     //--------------------------------------------------------------------------------
88 dashley 113 //Repeats a character $c to the console output $n times.
89 dashley 107 function rep_char_con($c, $n)
90     {
91     while ($n--)
92     echo $c;
93     }
94     //--------------------------------------------------------------------------------
95     //Writes a standard thick horizontal line to the console.
96     function hor_line_thick()
97     {
98     rep_char_con("=", CFG_CONSOLE_STD_LINE_LEN);
99     echo "\n";
100     }
101     //--------------------------------------------------------------------------------
102     //Writes a standard thin horizontal line to the console.
103     function hor_line_thin()
104     {
105     rep_char_con("-", CFG_CONSOLE_STD_LINE_LEN);
106     echo "\n";
107     }
108     //--------------------------------------------------------------------------------
109     //Returns an array of all files in the working directory.
110     //If no files can be found, returns FALSE.
111     function get_file_names_in_dir()
112     {
113     //Get directory list.
114     $rv = scandir (".");
115    
116     //If the list is empty, something went wrong. Return FALSE.
117     if ($rv === FALSE)
118     return FALSE;
119    
120     return $rv;
121     }
122     //--------------------------------------------------------------------------------
123     //Canonizes a filename by performing character substitutions.
124     function canonize($in_fname)
125     {
126     //Well, the algorithm below is O(N^2) or worse ... I knew there was a reason
127 dashley 113 //that computers get faster every year. (Yes, that was a joke.)
128 dashley 107 $out_fname = "";
129    
130     for ($i = 0; $i < strlen($in_fname); $i++)
131     {
132     $map = CFG_FILENAME_SUBSTITUTION_MAP;
133     $c = substr($in_fname, $i, 1);
134    
135     $sub_made = FALSE;
136     while (strlen($map) >= 2)
137     {
138     if (substr($map, 0, 1) == $c)
139     {
140     $c = substr($map, 1, 1);
141     $sub_made = TRUE;
142     break;
143     }
144     else
145     {
146     $map = substr($map, 2);
147     }
148     }
149    
150     if (! $sub_made)
151     {
152     $c = substr(CFG_FILENAME_SUBSTITUTION_MAP, strlen(CFG_FILENAME_SUBSTITUTION_MAP) - 1, 1);
153     //echo "Default sub made: " . $c . "\n";
154     }
155    
156     $out_fname = $out_fname . $c;
157     }
158    
159     return $out_fname;
160     }
161     //--------------------------------------------------------------------------------
162     //Write introductory message.
163     hor_line_thick();
164 dashley 113 echo CFG_PROGNAME . ", Copyright (c) 2016 David T. Ashley\n";
165     echo "This program comes with ABSOLUTELY NO WARRANTY; and is licensed under the\n";
166     echo "MIT License. A copy of this license is provided in the source code\n";
167     echo "of this program.\n";
168 dashley 107 hor_line_thin();
169     //-----------------------------------------------------------------------------
170     //Get and emit the names of everything in the directory.
171     $file_list = get_file_names_in_dir();
172     if ($file_list === FALSE)
173     {
174 dashley 113 echo "List of files from PHP function scandir() is empty (rv === FALSE).\n";
175 dashley 107 echo "Serious internal error, or nothing to do. Script cannot continue.\n";
176     hor_line_thick();
177     exit(1);
178     }
179     else
180     {
181     echo "Files in working directory (unsorted, unfiltered, "
182     .
183     count($file_list)
184     .
185     " files):\n";
186     for ($i = 0; $i < count($file_list); $i++)
187     echo " " . sprintf("[%5d]", $i) . " " . $file_list[$i] . "\n";
188     }
189     hor_line_thin();
190     //-----------------------------------------------------------------------------
191     //Remove everything that is not a file we can rename.
192     $temp_list = $file_list;
193     unset($file_list);
194     $n = 0;
195     for ($i = 0; $i < count($temp_list); $i++)
196     {
197     //echo "Checking " . $temp_list[$i] . "\n";
198    
199     if (strcmp($temp_list[$i], ".") == 0)
200     {
201     //. entry, not a file.
202     }
203     else if (strcmp($temp_list[$i], "..") == 0)
204     {
205     //.. entry, not a file.
206     }
207     else if (is_file($temp_list[$i]))
208     {
209     //This is a regular file.
210     $file_list[] = $temp_list[$i];
211     $n++;
212     }
213     }
214    
215     if ($n == 0)
216     $file_list = FALSE;
217    
218     unset($n);
219     unset($temp_list);
220     //-----------------------------------------------------------------------------
221     //If there is nothing to do, end the script.
222     if ($file_list === FALSE)
223     {
224     echo "No files to rename.\n";
225     hor_line_thick();
226     exit(0);
227     }
228     //-----------------------------------------------------------------------------
229     //Sort the list. This is a non-event. The only rationale for sorting is that
230     //it ensures that the same set of files will be processed in the same order,
231     //regardless of the order provided by the underlying OS internals.
232     sort($file_list);
233     //-----------------------------------------------------------------------------
234     //Emit the names we now have.
235     echo "Files in working directory (directory entries removed, sorted, "
236     .
237     count($file_list)
238     .
239     " files):\n";
240     for ($i = 0; $i < count($file_list); $i++)
241     echo " " . sprintf("[%5d]", $i) . " " . $file_list[$i] . "\n";
242     hor_line_thin();
243     //-----------------------------------------------------------------------------
244     //For each file in the directory, form a filename which would be the result
245     //of applying character substitutions and any other corrections.
246     for ($i = 0; $i < count($file_list); $i++)
247     {
248     $rename_target[$i] = canonize($file_list[$i]);
249    
250     //echo $file_list[$i] . " " . $rename_target[$i] . "\n";
251     }
252     //-----------------------------------------------------------------------------
253     //In all those cases where the name of the file as we would like it to be is
254     //different from the name of the file as it exists, rename it. Watch for
255     //collisions, because renaming on top of a file represents the loss of
256     //data.
257     $n = 0;
258     for ($i = 0; $i < count($file_list); $i++)
259     {
260     if (strcmp($file_list[$i], $rename_target[$i]) != 0)
261     $n++;
262     }
263    
264     if ($n == 0)
265     {
266     echo "No files need to be renamed.\n";
267     hor_line_thin();
268     }
269     else
270     {
271     echo "Renaming " . $n . " file(s).\n";
272     hor_line_thin();
273    
274     $count = 0;
275     for ($i = 0; $i < count($file_list); $i++)
276     {
277     if (strcmp($file_list[$i], $rename_target[$i]) != 0)
278     {
279     $count++;
280     echo "[" . sprintf("%5d/%5d", $count, $n) . "]"
281     .
282     " Renaming " . $file_list[$i] . " to " . $rename_target[$i] . " ... ";
283     if (file_exists($rename_target[$i]))
284     {
285     echo "Aborted.\nRename target already exists.\n";
286     echo "Choose non-colliding filenames and run this script again.\n";
287     echo "Script terminating.\n";
288     hor_line_thin();
289     exit(1);
290     }
291    
292     $result = rename($file_list[$i], $rename_target[$i]);
293     if ($result)
294     {
295     echo "Done.\n";
296     hor_line_thin();
297     }
298     else
299     {
300     echo "Failed.\nScript must terminate.\n";
301     hor_line_thick();
302     exit(1);
303     }
304     }
305     }
306     }
307     //-----------------------------------------------------------------------------
308     echo CFG_PROGNAME . " execution ends.\n";
309     hor_line_thick();
310     //--------------------------------------------------------------------------------
311     //End of File
312     //--------------------------------------------------------------------------------
313     ?>

Properties

Name Value
svn:eol-style native
svn:executable *
svn:keywords Header

dashley@gmail.com
ViewVC Help
Powered by ViewVC 1.1.25