/[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 113 - (hide annotations) (download)
Sat Dec 31 20:36:51 2016 UTC (7 years, 11 months ago) by dashley
File size: 12675 byte(s)
Minor changes to resurrect this script.
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 113 // pics_filenames_canonize.php (this program):
33     // 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     // original image file with the suffix "_small". Only 20 files are converted on each
39     // invocation of the program, to avoid the involuntary process termination that typically
40     // occurs in a shared hosting environment when a process uses too much CPU time. The program
41     // should be run repeatedly until it indicates that it has no more thumbnails to create.
42 dashley 107 //
43 dashley 113 // If any full-sized photos are modified, any corresponding thumbnails should be deleted and
44     // pics_thumbnails_make.php and pics_indexfile_make.php should be run again.
45 dashley 107 //
46 dashley 113 // pics_indexfile_make.php
47     // Scans a directory and makes an index file ("index2.php") displaying all the thumbnail
48     // images, each of which link to the corresponding full-sized image. The index file is
49     // tailored to Dave Ashley's needs, but the created file can be edited and most of the
50     // content pasted into an HTML file. To avoid the accidental loss of information, any
51     // existing "index2.php" file is renamed out of the way.
52 dashley 107 //
53 dashley 113 //This script is designed to be run manually (rather than automatically invoked as a result of a
54     //web page request). It was written in PHP for convenience simply because DreamHost (the web
55     //hosting company Dave Ashley used at the time these scripts were written) has as part of its
56     //hosting environment PHP with the ImageMagick library compiled in.
57 dashley 107 //
58 dashley 113 //Usually, this script is invoked using "php <path>/filenames_canonize.php", but the method of
59     //invocation may vary based on computing platform details.
60     //-------------------------------------------------------------------------------------------------
61 dashley 107 //C O N F I G U R A T I O N
62     //--------------------------------------------------------------------------------
63     //Configuration switch combinations were not tested. E-mail me any program
64     //corrections (dashley@gmail.com).
65 dashley 113 define ("CFG_PROGNAME", "pics_filenames_canonize.php");
66 dashley 107 //Number of characters per line preferred for console output.
67     define ("CFG_CONSOLE_STD_LINE_LEN", 78);
68     //Number of characters per line preferred for console output.
69     define (
70     "CFG_FILENAME_SUBSTITUTION_MAP",
71     ".." //Dots unchanged.
72     .
73     "aabbccddeeffgghhiijjkkllmmnnooppqqrrssttuuvvwwxxyyzz" //LC letters unchanged.
74     .
75     "AaBbCcDdEeFfGgHhIiJjKkLlMmNnOoPpQqRrSsTtUuVvWwXxYyZz" //UC converted to LC.
76     .
77     "00112233445566778899" //Digits unchanged.
78     .
79     "-_" //Hyphens become underscores,
80     //and anything unmapped
81     //becomes an underscore.
82     );
83     //Pairs of characters, the first is the character to match and the second
84     //is the replacement. Any characters not found in the map will be replaced
85     //by the last character of the constant.
86     //--------------------------------------------------------------------------------
87 dashley 113 //Repeats a character $c to the console output $n times.
88 dashley 107 function rep_char_con($c, $n)
89     {
90     while ($n--)
91     echo $c;
92     }
93     //--------------------------------------------------------------------------------
94     //Writes a standard thick horizontal line to the console.
95     function hor_line_thick()
96     {
97     rep_char_con("=", CFG_CONSOLE_STD_LINE_LEN);
98     echo "\n";
99     }
100     //--------------------------------------------------------------------------------
101     //Writes a standard thin horizontal line to the console.
102     function hor_line_thin()
103     {
104     rep_char_con("-", CFG_CONSOLE_STD_LINE_LEN);
105     echo "\n";
106     }
107     //--------------------------------------------------------------------------------
108     //Returns an array of all files in the working directory.
109     //If no files can be found, returns FALSE.
110     function get_file_names_in_dir()
111     {
112     //Get directory list.
113     $rv = scandir (".");
114    
115     //If the list is empty, something went wrong. Return FALSE.
116     if ($rv === FALSE)
117     return FALSE;
118    
119     return $rv;
120     }
121     //--------------------------------------------------------------------------------
122     //Canonizes a filename by performing character substitutions.
123     function canonize($in_fname)
124     {
125     //Well, the algorithm below is O(N^2) or worse ... I knew there was a reason
126 dashley 113 //that computers get faster every year. (Yes, that was a joke.)
127 dashley 107 $out_fname = "";
128    
129     for ($i = 0; $i < strlen($in_fname); $i++)
130     {
131     $map = CFG_FILENAME_SUBSTITUTION_MAP;
132     $c = substr($in_fname, $i, 1);
133    
134     $sub_made = FALSE;
135     while (strlen($map) >= 2)
136     {
137     if (substr($map, 0, 1) == $c)
138     {
139     $c = substr($map, 1, 1);
140     $sub_made = TRUE;
141     break;
142     }
143     else
144     {
145     $map = substr($map, 2);
146     }
147     }
148    
149     if (! $sub_made)
150     {
151     $c = substr(CFG_FILENAME_SUBSTITUTION_MAP, strlen(CFG_FILENAME_SUBSTITUTION_MAP) - 1, 1);
152     //echo "Default sub made: " . $c . "\n";
153     }
154    
155     $out_fname = $out_fname . $c;
156     }
157    
158     return $out_fname;
159     }
160     //--------------------------------------------------------------------------------
161     //Write introductory message.
162     hor_line_thick();
163 dashley 113 echo CFG_PROGNAME . ", Copyright (c) 2016 David T. Ashley\n";
164     echo "This program comes with ABSOLUTELY NO WARRANTY; and is licensed under the\n";
165     echo "MIT License. A copy of this license is provided in the source code\n";
166     echo "of this program.\n";
167 dashley 107 hor_line_thin();
168     //-----------------------------------------------------------------------------
169     //Get and emit the names of everything in the directory.
170     $file_list = get_file_names_in_dir();
171     if ($file_list === FALSE)
172     {
173 dashley 113 echo "List of files from PHP function scandir() is empty (rv === FALSE).\n";
174 dashley 107 echo "Serious internal error, or nothing to do. Script cannot continue.\n";
175     hor_line_thick();
176     exit(1);
177     }
178     else
179     {
180     echo "Files in working directory (unsorted, unfiltered, "
181     .
182     count($file_list)
183     .
184     " files):\n";
185     for ($i = 0; $i < count($file_list); $i++)
186     echo " " . sprintf("[%5d]", $i) . " " . $file_list[$i] . "\n";
187     }
188     hor_line_thin();
189     //-----------------------------------------------------------------------------
190     //Remove everything that is not a file we can rename.
191     $temp_list = $file_list;
192     unset($file_list);
193     $n = 0;
194     for ($i = 0; $i < count($temp_list); $i++)
195     {
196     //echo "Checking " . $temp_list[$i] . "\n";
197    
198     if (strcmp($temp_list[$i], ".") == 0)
199     {
200     //. entry, not a file.
201     }
202     else if (strcmp($temp_list[$i], "..") == 0)
203     {
204     //.. entry, not a file.
205     }
206     else if (is_file($temp_list[$i]))
207     {
208     //This is a regular file.
209     $file_list[] = $temp_list[$i];
210     $n++;
211     }
212     }
213    
214     if ($n == 0)
215     $file_list = FALSE;
216    
217     unset($n);
218     unset($temp_list);
219     //-----------------------------------------------------------------------------
220     //If there is nothing to do, end the script.
221     if ($file_list === FALSE)
222     {
223     echo "No files to rename.\n";
224     hor_line_thick();
225     exit(0);
226     }
227     //-----------------------------------------------------------------------------
228     //Sort the list. This is a non-event. The only rationale for sorting is that
229     //it ensures that the same set of files will be processed in the same order,
230     //regardless of the order provided by the underlying OS internals.
231     sort($file_list);
232     //-----------------------------------------------------------------------------
233     //Emit the names we now have.
234     echo "Files in working directory (directory entries removed, sorted, "
235     .
236     count($file_list)
237     .
238     " files):\n";
239     for ($i = 0; $i < count($file_list); $i++)
240     echo " " . sprintf("[%5d]", $i) . " " . $file_list[$i] . "\n";
241     hor_line_thin();
242     //-----------------------------------------------------------------------------
243     //For each file in the directory, form a filename which would be the result
244     //of applying character substitutions and any other corrections.
245     for ($i = 0; $i < count($file_list); $i++)
246     {
247     $rename_target[$i] = canonize($file_list[$i]);
248    
249     //echo $file_list[$i] . " " . $rename_target[$i] . "\n";
250     }
251     //-----------------------------------------------------------------------------
252     //In all those cases where the name of the file as we would like it to be is
253     //different from the name of the file as it exists, rename it. Watch for
254     //collisions, because renaming on top of a file represents the loss of
255     //data.
256     $n = 0;
257     for ($i = 0; $i < count($file_list); $i++)
258     {
259     if (strcmp($file_list[$i], $rename_target[$i]) != 0)
260     $n++;
261     }
262    
263     if ($n == 0)
264     {
265     echo "No files need to be renamed.\n";
266     hor_line_thin();
267     }
268     else
269     {
270     echo "Renaming " . $n . " file(s).\n";
271     hor_line_thin();
272    
273     $count = 0;
274     for ($i = 0; $i < count($file_list); $i++)
275     {
276     if (strcmp($file_list[$i], $rename_target[$i]) != 0)
277     {
278     $count++;
279     echo "[" . sprintf("%5d/%5d", $count, $n) . "]"
280     .
281     " Renaming " . $file_list[$i] . " to " . $rename_target[$i] . " ... ";
282     if (file_exists($rename_target[$i]))
283     {
284     echo "Aborted.\nRename target already exists.\n";
285     echo "Choose non-colliding filenames and run this script again.\n";
286     echo "Script terminating.\n";
287     hor_line_thin();
288     exit(1);
289     }
290    
291     $result = rename($file_list[$i], $rename_target[$i]);
292     if ($result)
293     {
294     echo "Done.\n";
295     hor_line_thin();
296     }
297     else
298     {
299     echo "Failed.\nScript must terminate.\n";
300     hor_line_thick();
301     exit(1);
302     }
303     }
304     }
305     }
306     //-----------------------------------------------------------------------------
307     echo CFG_PROGNAME . " execution ends.\n";
308     hor_line_thick();
309     //--------------------------------------------------------------------------------
310     //End of File
311     //--------------------------------------------------------------------------------
312     ?>

Properties

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

dashley@gmail.com
ViewVC Help
Powered by ViewVC 1.1.25