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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 118 - (hide annotations) (download)
Sun Jan 1 04:06:35 2017 UTC (7 years, 6 months ago) by dashley
File size: 17701 byte(s)
Edit for resurrection on home server.
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 118 //pics_indexfile_make.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 118 //This program, a PHP script, creates an index file ("index2.php") based on the contents of the
26     //working directory in which it is invoked. The index file created is tailored to Dave Ashley's
27     //needs, but it can easily be hand-edited or this script can be modified. This program is part
28     //of a 3-program suite designed to collectively create a web page directly from digital camera
29     //files (and of course the web page can be customized by hand-editing after it is automatically
30     //generated).
31     //
32     //The 3 programs in the 3-program suite are:
33     //
34     // pics_filenames_canonize.php:
35     // Converts all names of files in a directory to lower-case, and makes substitutions for any
36     // unusual characters.
37     //
38     // pics_thumbnails_make.php:
39     // Creates thumbnails from recognized image types. The thumbnails are named relative to the
40     // original image file with the suffix "_small". To support shared hosting environments where
41     // CPU utilization of a script may be capped, there is a configuration constant in the script
42     // that will perform only a fixed number of conversions per invocation. If this configuration
43     // option is used, the script should be run repeatedly until it indicates that it has no more
44     // thumbnails to create.
45     //
46     // If any full-sized photos are modified, any corresponding thumbnails should be deleted and
47     // pics_thumbnails_make.php and pics_indexfile_make.php should be run again.
48     //
49     // pics_indexfile_make.php (this script):
50     // Scans a directory and makes an index file ("index2.php") displaying all the thumbnail
51     // images, each of which link to the corresponding full-sized image. The index file is
52     // tailored to Dave Ashley's needs, but the created file can be edited and most of the
53     // content pasted into an HTML file. To avoid the accidental loss of information, any
54     // existing "index2.php" file is renamed out of the way.
55     //
56     //This script is designed to be run manually (rather than automatically invoked as a result of a
57     //web page request).
58 dashley 107 //--------------------------------------------------------------------------------
59     //C O N F I G U R A T I O N
60     //--------------------------------------------------------------------------------
61     //Configuration switch combinations were not tested. E-mail me any program
62     //corrections (dashley@gmail.com).
63 dashley 118 define ("CFG_PROGNAME", "pics_indexfile_make.php");
64 dashley 107 //Program name.
65     define ("CFG_CONSOLE_STD_LINE_LEN", 78);
66     //Number of characters per line preferred for console output.
67     define ("CFG_THUMBNAIL_FILENAME_SUFFIX", "_small");
68     //String added just before the filename extension to choose thumbnail names
69     //based on name of full-sized image.
70     define ("CFG_INDEXFILE_NAME", "index2.php");
71     //Name of the file containing the index. PHP code is assumed, but the
72     //file can be harvested for the HTML.
73 dashley 118 define ("CFG_INDEX_LINK_INDENT", 3);
74     //The number of spaces to indent each link.
75     define ("CFG_INDEX_PARAGRAPH_INDENT", 3);
76     //The number of spaces to indent each paragraph.
77 dashley 107 define ("CFG_INDEXFILE_PICREF_INDENT", 3);
78     //The number of spaces to indent each reference to a picture.
79 dashley 118 define ("CFG_FILEEXTENSIONS_PHOTO", "jpg/jpeg/png/gif");
80 dashley 107 //The file extensions that will be recognized as photos.
81     define ("CFG_FILEEXTENSIONS_LINK", "mov");
82     //File extensions that will be linked to. All others will be mentioned
83     //in the generated output but not linked to.
84     //--------------------------------------------------------------------------------
85     //Repeats a character to the console output a certain number of times.
86     function rep_char_con($c, $n)
87     {
88     while ($n--)
89     echo $c;
90     }
91     //--------------------------------------------------------------------------------
92     //Writes a standard thick horizontal line to the console.
93     function hor_line_thick()
94     {
95     rep_char_con("=", CFG_CONSOLE_STD_LINE_LEN);
96     echo "\n";
97     }
98     //--------------------------------------------------------------------------------
99     //Writes a standard thin horizontal line to the console.
100     function hor_line_thin()
101     {
102     rep_char_con("-", CFG_CONSOLE_STD_LINE_LEN);
103     echo "\n";
104     }
105     //--------------------------------------------------------------------------------
106     //Returns an array of all files in the working directory.
107     //If no files can be found, returns FALSE.
108     function get_file_names_in_dir()
109     {
110     //Get directory list.
111     $rv = scandir (".");
112    
113     //If the list is empty, something went wrong. Return FALSE.
114     if ($rv === FALSE)
115     return FALSE;
116    
117     return $rv;
118     }
119     //--------------------------------------------------------------------------------
120     //Accepts a non-negative integer as an integer or a string. Adds commas in the
121     //right places to make it more human readable.
122     function int_nonneg_commanate($in_ui)
123     {
124     //Ensure the integer is an integer from PHP's point of view.
125     $in_ui = (int)$in_ui;
126    
127     //If a negative number was passed in, clip it.
128     if ($in_ui < 0)
129     $in_ui = 0;
130    
131     //Make a proper numerical string with commas out of it.
132     $s_ui = number_format($in_ui);
133    
134     return $s_ui;
135     }
136     //--------------------------------------------------------------------------------
137     //Returns TRUE if a file name appears to be a valid full-sized image name,
138     //or FALSE otherwise.
139     function is_full_sized_image_file_name($in_filename)
140     {
141     //Convert the string name to all lower case. This will do for
142     //comparisons and tests.
143 dashley 118 $in_filename = strtolower($in_filename);
144 dashley 107
145     //Attempt to split the name into a base and an extension. Any failure
146     //means it is an unsuitable name.
147     $extension_start = strrpos($in_filename, ".");
148     //Find position of last "." in string. This should precede the
149     //file extension.
150    
151     if ($extension_start === FALSE)
152     {
153     //Bad name. Unsuitable.
154     return FALSE;
155     }
156    
157     //Calculate the base and extension.
158     $filename_base = substr($in_filename, 0, $extension_start);
159     $filename_extension = substr($in_filename, $extension_start + 1);
160    
161     //If the extension is neither "jpg" nor "jpeg", it is unsuitable.
162     if (($filename_extension != "jpg") && ($filename_extension != "jpeg"))
163     return FALSE;
164    
165     //If the filename base is empty, the filename is unsuitable.
166     if (strlen($filename_base) == 0)
167     return FALSE;
168    
169     //If the last characters of the base are the CFG_THUMBNAIL_FILENAME_SUFFIX,
170     //the name is unsuitable.
171     if (strlen($filename_base) >= strlen(CFG_THUMBNAIL_FILENAME_SUFFIX))
172     {
173     if (substr($filename_base, strlen($filename_base) - strlen(CFG_THUMBNAIL_FILENAME_SUFFIX))
174     == CFG_THUMBNAIL_FILENAME_SUFFIX)
175     return FALSE;
176     }
177    
178     //Looks good.
179     return TRUE;
180     }
181     //--------------------------------------------------------------------------------
182     //As a function of the file name, creates the file name for the thumbnail.
183     function file_name_to_thumbnail_name($in_filename)
184     {
185     $extension_start = strrpos($in_filename, ".");
186     //Find position of last "." in string. This should precede the
187     //file extension.
188     if ($extension_start === FALSE)
189     {
190     //"." not found. Should not happen. Filenames were checked in advance.
191     echo "Fatal internal error at line " . __LINE__ . "\n";
192     exit(1);
193     }
194    
195     $filename_prefix = substr($in_filename, 0, $extension_start);
196     $filename_extension = substr($in_filename, $extension_start);
197    
198     $rv = $filename_prefix . CFG_THUMBNAIL_FILENAME_SUFFIX . $filename_extension;
199    
200     return $rv;
201     }
202     //--------------------------------------------------------------------------------
203     //Writes the index file preamble to the index file. This is very much
204     //dependent on how the thumbnails will be used. This is currently customized
205     //for what Dave Ashley does.
206     function write_index_file_preamble($handle, $idstring)
207     {
208     fwrite($handle, "<" . "?" . "php\n");
209     fwrite($handle, "require_once(\"style/std/stdwpstyle.inc\");\n");
210     fwrite($handle, "?" . ">" . "\n");
211     fwrite($handle, "<" . "?" . "php\n");
212     fwrite($handle, " $" . "thispage = new StdWpStyle();\n");
213     fwrite($handle, "\n");
214     fwrite($handle, " $" . "thispage->static_page_header_title_std(\"" . $idstring . "\",\n");
215     fwrite($handle, " \"" . $idstring . "\",\n");
216     fwrite($handle, " \"\");\n");
217     fwrite($handle, " $" . "thispage->static_page_photo_thumbnail_explanation(TRUE, TRUE);\n");
218     fwrite($handle, "?" . ">" . "\n");
219     for ($i = 0; $i < CFG_INDEX_PARAGRAPH_INDENT; $i++)
220     {
221     fwrite($handle, " ");
222     }
223     fwrite($handle, "\n<p align=\"center\">\n");
224     }
225     //--------------------------------------------------------------------------------
226     //Writes the index file postamble to the index file. This is very much
227     //dependent on how the thumbnails will be used. This is currently customized
228     //for what Dave Ashley does.
229     function write_index_file_postamble($handle)
230     {
231     for ($i = 0; $i < CFG_INDEX_PARAGRAPH_INDENT; $i++)
232     {
233     fwrite($handle, " ");
234     }
235     fwrite($handle, "</p>\n\n");
236     fwrite($handle, "<" . "?" . "php\n");
237     fwrite($handle, " $" . "thispage->static_page_footer_std();\n");
238     fwrite($handle, "?" . ">" . "\n");
239     }
240     //--------------------------------------------------------------------------------
241     //Writes a line to the index file corresponding to a linked larger picture
242     //reprsented by a thumbnail.
243     function write_index_line( $handle,
244     $filename,
245     $thumbnailname,
246     $filename_filesize,
247     $filename_xdim,
248     $filename_ydim,
249     $thumbnailname_filesize,
250     $thumbnailname_xdim,
251     $thumbnailname_ydim)
252     {
253 dashley 118 //<a href="dscn1258.jpg"><img border="0" src="dscn1258_small.jpg" alt="dscn1258.jpg (4932338 bytes)" width="125" height="93"></a>
254 dashley 107 //Indent.
255     for ($i = 0; $i < CFG_INDEX_LINK_INDENT; $i++)
256     {
257     fwrite($handle, " ");
258     }
259    
260     fwrite($handle, "<a href=\"");
261     fwrite($handle, $filename);
262     fwrite($handle, "\"><img border=\"0\" src=\"");
263     fwrite($handle, $thumbnailname);
264     fwrite($handle, "\" alt=\"");
265     fwrite($handle, $filename);
266     fwrite($handle, " (");
267     fwrite($handle, int_nonneg_commanate($filename_filesize));
268     fwrite($handle, " bytes)\" ");
269     fwrite($handle, "title=\"");
270     fwrite($handle, $filename);
271     fwrite($handle, " (");
272     fwrite($handle, int_nonneg_commanate($filename_filesize));
273     fwrite($handle, " bytes)\" width=\"");
274     fwrite($handle, $thumbnailname_xdim);
275     fwrite($handle, "\" height=\"");
276     fwrite($handle, $thumbnailname_ydim);
277     fwrite($handle, "\"></a>");
278     fwrite($handle, "\n");
279     }
280     //--------------------------------------------------------------------------------
281     //Write introductory message.
282     hor_line_thick();
283 dashley 118 echo CFG_PROGNAME . ", Copyright (c) 2016 David T. Ashley\n";
284     echo "This program comes with ABSOLUTELY NO WARRANTY; and is licensed under the\n";
285     echo "MIT License. A copy of this license is provided in the source code\n";
286     echo "of this program.\n";
287 dashley 107 hor_line_thin();
288     //-----------------------------------------------------------------------------
289     //Get the date, time, and PID strings that will be used for the rest of the
290     //script.
291     $scr_datestring = date("Ymd_His");
292     $scr_pid = getmypid();
293     if ($scr_pid === FALSE)
294     $scr_pid = 0;
295     //-----------------------------------------------------------------------------
296     //If the index file exists, rename it out of the way. This is to prevent
297     //the loss of information.
298     if (file_exists(CFG_INDEXFILE_NAME))
299     {
300     rename(CFG_INDEXFILE_NAME, $scr_datestring . "_" . $scr_pid . ".php");
301     }
302     //-----------------------------------------------------------------------------
303     //Open the index file and write the preamble.
304     $indexfile_handle = fopen(CFG_INDEXFILE_NAME, "w");
305     if ($indexfile_handle === FALSE)
306     {
307     echo "FATAL: unable to open index file.\n";
308     exit(1);
309     }
310     write_index_file_preamble($indexfile_handle, $scr_datestring . "_" . $scr_pid);
311     //-----------------------------------------------------------------------------
312     //Get and emit the names of everything in the directory.
313     $file_list = get_file_names_in_dir();
314     if ($file_list === FALSE)
315     {
316     echo "List of files from PHP function scandir() is empty.\n";
317     echo "Serious internal error, or nothing to do. Script cannot continue.\n";
318     hor_line_thick();
319     exit(1);
320     }
321     else
322     {
323     echo "Files in working directory (unsorted, unfiltered, "
324     .
325     count($file_list)
326     .
327     " files):\n";
328     for ($i = 0; $i < count($file_list); $i++)
329     echo " " . sprintf("[%5d]", $i) . " " . $file_list[$i] . "\n";
330     }
331     hor_line_thin();
332     //-----------------------------------------------------------------------------
333     //Remove the standard directory entries "." and ".." from the list, and
334     //remove any directories.
335     $temp_list = $file_list;
336     unset($file_list);
337     $n = 0;
338     for ($i = 0; $i < count($temp_list); $i++)
339     {
340     //echo "Checking " . $temp_list[$i] . "\n";
341    
342     if (strcmp($temp_list[$i], ".") == 0)
343     {
344     //. entry, not a file.
345     }
346     else if (strcmp($temp_list[$i], "..") == 0)
347     {
348     //.. entry, not a file.
349     }
350     else if (is_file($temp_list[$i]))
351     {
352     //This is a regular file.
353     $file_list[] = $temp_list[$i];
354     $n++;
355     }
356     }
357    
358     if ($n == 0)
359     $file_list = FALSE;
360    
361     unset($n);
362     unset($temp_list);
363     //-----------------------------------------------------------------------------
364     //If there is nothing to do, end the script.
365     if ($file_list === FALSE)
366     {
367     echo "No files to process.\n";
368     hor_line_thick();
369     exit(0);
370     }
371     //-----------------------------------------------------------------------------
372     //Sort the list. This is a non-event. The only rationale for sorting is that
373     //it ensures that the same set of files will be processed in the same order,
374     //regardless of the order provided by the underlying OS internals.
375     sort($file_list);
376     //-----------------------------------------------------------------------------
377     //Emit the names we now have.
378     echo "Files in working directory (directory entries removed, sorted, "
379     .
380     count($file_list)
381     .
382     " files):\n";
383     for ($i = 0; $i < count($file_list); $i++)
384     echo " " . sprintf("[%5d]", $i) . " " . $file_list[$i] . "\n";
385     hor_line_thin();
386    
387     //-----------------------------------------------------------------------------
388     //For each file in the list, process it. There are different processing
389     //actions for each one.
390     for ($i = 0; $i < count($file_list); $i++)
391     {
392     if (is_full_sized_image_file_name($file_list[$i]))
393     {
394     $thumbnail_name = file_name_to_thumbnail_name($file_list[$i]);
395    
396     $image_data = getimagesize($file_list[$i]);
397     $thumbnail_data = getimagesize($thumbnail_name);
398    
399     write_index_line( $indexfile_handle,
400     $file_list[$i],
401     $thumbnail_name,
402     filesize($file_list[$i]),
403     $image_data[0],
404     $image_data[1],
405     filesize($thumbnail_name),
406     $thumbnail_data[0],
407     $thumbnail_data[1]);
408    
409     }
410     }
411    
412     //-----------------------------------------------------------------------------
413     //Complete and close up the index file.
414     write_index_file_postamble($indexfile_handle);
415     fclose($indexfile_handle);
416     //--------------------------------------------------------------------------------
417     echo CFG_PROGNAME . " execution ends.\n";
418     hor_line_thick();
419     //--------------------------------------------------------------------------------
420     //End of File
421     //--------------------------------------------------------------------------------
422     ?>

Properties

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

dashley@gmail.com
ViewVC Help
Powered by ViewVC 1.1.25