5 |
//This source code and any program in which it is compiled/used is provided under the MIT |
//This source code and any program in which it is compiled/used is provided under the MIT |
6 |
//LICENSE (full license text below). |
//LICENSE (full license text below). |
7 |
//------------------------------------------------------------------------------------------------- |
//------------------------------------------------------------------------------------------------- |
8 |
//Copyright (c) 2016 David T. Ashley |
//pics_thumbnails_make.php, Copyright (c) 2016 David T. Ashley |
9 |
// |
// |
10 |
//Permission is hereby granted, free of charge, to any person obtaining a copy of this software and |
//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, |
//associated documentation files (the "Software"), to deal in the Software without restriction, |
22 |
//DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
//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. |
//OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
24 |
//------------------------------------------------------------------------------------------------- |
//------------------------------------------------------------------------------------------------- |
25 |
//-------------------------------------------------------------------------------- |
//This program, a PHP script, creates thumbnails from recognized image types. This program is part |
26 |
|
//of a 3-program suite designed to collectively create a web page directly from digital camera |
27 |
|
//files (and of course the web page can be customized by hand-editing after it is automatically |
28 |
|
//generated). |
29 |
|
// |
30 |
|
//The 3 programs in the 3-program suite are: |
31 |
|
// |
32 |
|
// pics_filenames_canonize.php: |
33 |
|
// Converts all names of files in a directory to lower-case, and makes substitutions for any |
34 |
|
// unusual characters. |
35 |
|
// |
36 |
|
// pics_thumbnails_make.php (this script): |
37 |
|
// Creates thumbnails from recognized image types. The thumbnails are named relative to the |
38 |
|
// 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 |
|
// |
44 |
|
// 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 |
|
// |
47 |
|
// pics_indexfile_make.php |
48 |
|
// 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 |
|
// |
54 |
|
//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 |
|
// |
59 |
|
//Usually, this script is invoked using "php <path>/pics_thumbnails_make.php", but the method of |
60 |
|
//invocation may vary based on computing platform details. |
61 |
|
// |
62 |
|
//Using Fedora Linux, I had trouble getting PHP to use ImageMagick (the error was something like |
63 |
|
//"Class Imagick not found"). Finally, I figured out that the missing package was |
64 |
|
//php-pecl-imagemagick and "dnf install php-pecl-imagick" too care of it. |
65 |
|
// |
66 |
|
//After all conversions are complete, this script terminates with a segmentation fault under |
67 |
|
//Linux. The segmentation fault is definitely tied to ImageMagick. The reason is not known, but |
68 |
|
//the problem appears here and there on the Internet. All of the photo conversions occur |
69 |
|
//correctly, and the problem seems to occur during the termination phase of the PHP script. |
70 |
|
//------------------------------------------------------------------------------------------------- |
71 |
//C O N F I G U R A T I O N |
//C O N F I G U R A T I O N |
72 |
//-------------------------------------------------------------------------------- |
//------------------------------------------------------------------------------------------------- |
73 |
//Configuration switch combinations were not tested. E-mail me any program |
//Configuration switch combinations were not tested. E-mail me any program |
74 |
//corrections (dashley@gmail.com). |
//corrections (dashley@gmail.com). |
75 |
define ("CFG_PROGNAME", "thumbnails_make.php"); |
define ("CFG_PROGNAME", "pics_thumbnails_make.php"); |
76 |
//Number of characters per line preferred for console output. |
//Program name. |
77 |
define ("CFG_CONSOLE_STD_LINE_LEN", 78); |
define ("CFG_CONSOLE_STD_LINE_LEN", 78); |
78 |
//Number of characters per line preferred for console output. |
//Number of characters per line preferred for console output. |
79 |
define ("CFG_THUMBNAIL_DIMENSION_MAX", 125); |
define ("CFG_THUMBNAIL_DIMENSION_MAX", 125); |
90 |
//of the thumbnails, but the filter was applied in the PHP example I found |
//of the thumbnails, but the filter was applied in the PHP example I found |
91 |
//online. Applying the Lanczos filter typically adds a few seconds to the |
//online. Applying the Lanczos filter typically adds a few seconds to the |
92 |
//time required to create each thumbnail. |
//time required to create each thumbnail. |
93 |
define ("CFG_MAX_THUMBNAILS_PER_INVOCATION", 20); |
define ("CFG_MAX_THUMBNAILS_PER_INVOCATION", 0); |
94 |
//The maximum number of thumbnails that should be created per invocation. |
//The maximum number of thumbnails that should be created per invocation. |
95 |
//This is to prevent the program from being involuntarily terminated |
//This is to prevent the program from being involuntarily terminated |
96 |
//for taking too much CPU time. |
//for consuming too much CPU time (mostly for use in shared hosting |
97 |
|
//environments that cap CPU usage). Setting this to 0 means no limit is |
98 |
|
//applied (unlimited number of photos processed per invocation). |
99 |
//-------------------------------------------------------------------------------- |
//-------------------------------------------------------------------------------- |
100 |
//Calculates important integer indices related to creating a thumbnail, in order |
//Calculates important integer indices related to creating a thumbnail, in order |
101 |
//to minimize distortion in the thumbnail. |
//to minimize distortion in the thumbnail. |
218 |
{ |
{ |
219 |
//Convert the string name to all lower case. This will do for |
//Convert the string name to all lower case. This will do for |
220 |
//comparisons and tests. |
//comparisons and tests. |
221 |
$s = strtolower($s); |
$in_filename = strtolower($in_filename); |
222 |
|
|
223 |
//Attempt to split the name into a base and an extension. Any failure |
//Attempt to split the name into a base and an extension. Any failure |
224 |
//means it is an unsuitable name. |
//means it is an unsuitable name. |
236 |
$filename_base = substr($in_filename, 0, $extension_start); |
$filename_base = substr($in_filename, 0, $extension_start); |
237 |
$filename_extension = substr($in_filename, $extension_start + 1); |
$filename_extension = substr($in_filename, $extension_start + 1); |
238 |
|
|
239 |
//If the extension is neither "jpg" nor "jpeg", it is unsuitable. |
//If the extension is not "jpg", "jpeg", "gif", or "png"; it isn't anything we recognize. |
240 |
if (($filename_extension != "jpg") && ($filename_extension != "jpeg")) |
if ( |
241 |
|
($filename_extension != "jpg") |
242 |
|
&& |
243 |
|
($filename_extension != "jpeg") |
244 |
|
&& |
245 |
|
($filename_extension != "gif") |
246 |
|
&& |
247 |
|
($filename_extension != "png") |
248 |
|
) |
249 |
return FALSE; |
return FALSE; |
250 |
|
|
251 |
//If the filename base is empty, the filename is unsuitable. |
//If the filename base is empty, the filename is unsuitable. |
428 |
//Write the thumbnail. |
//Write the thumbnail. |
429 |
$imagick->writeImage($in_thumbnailname); |
$imagick->writeImage($in_thumbnailname); |
430 |
|
|
431 |
//Destroy to prevent memory leak. |
//Destroy to prevent possible memory leak. |
432 |
$imagick->clear(); |
$imagick->destroy(); |
|
$imagick->destroy(); |
|
433 |
|
|
434 |
//All of the writing is done. Try to obtain the file sizes. |
//All of the writing is done. Try to obtain the file sizes. |
435 |
$fsize = filesize($in_filename); |
$fsize = filesize($in_filename); |
442 |
//-------------------------------------------------------------------------------- |
//-------------------------------------------------------------------------------- |
443 |
//Write introductory message. |
//Write introductory message. |
444 |
hor_line_thick(); |
hor_line_thick(); |
445 |
echo CFG_PROGNAME . " Copyright (C) 2015 David T. Ashley\n"; |
echo CFG_PROGNAME . ", Copyright (c) 2016 David T. Ashley\n"; |
446 |
echo "This program comes with ABSOLUTELY NO WARRANTY; and is licensed under\n"; |
echo "This program comes with ABSOLUTELY NO WARRANTY; and is licensed under the\n"; |
447 |
echo "the GNU General Public License, Version 3. A copy of this license is\n"; |
echo "MIT License. A copy of this license is provided in the source code\n"; |
448 |
echo "provided in the source code of this program.\n"; |
echo "of this program.\n"; |
449 |
hor_line_thin(); |
hor_line_thin(); |
450 |
//----------------------------------------------------------------------------- |
//----------------------------------------------------------------------------- |
451 |
//Get and emit the names of everything in the directory. |
//Get and emit the names of everything in the directory. |
452 |
$file_list = get_file_names_in_dir(); |
$file_list = get_file_names_in_dir(); |
453 |
if ($file_list === FALSE) |
if ($file_list === FALSE) |
454 |
{ |
{ |
455 |
echo "List of files from PHP function scandir() is empty.\n"; |
echo "List of files from PHP function scandir() is empty (rv === FALSE).\n"; |
456 |
echo "Serious internal error, or nothing to do. Script cannot continue.\n"; |
echo "Serious internal error, or nothing to do. Script cannot continue.\n"; |
457 |
hor_line_thick(); |
hor_line_thick(); |
458 |
exit(1); |
exit(1); |
528 |
//exist, up to the maximum we may do in one invocation, create the thumbnail. |
//exist, up to the maximum we may do in one invocation, create the thumbnail. |
529 |
$i = 0; |
$i = 0; |
530 |
$completed = 0; |
$completed = 0; |
531 |
while (($completed < CFG_MAX_THUMBNAILS_PER_INVOCATION) && ($i < count($file_list))) |
while ( |
532 |
|
( |
533 |
|
(CFG_MAX_THUMBNAILS_PER_INVOCATION == 0) |
534 |
|
|| |
535 |
|
($completed < CFG_MAX_THUMBNAILS_PER_INVOCATION) |
536 |
|
) |
537 |
|
&& |
538 |
|
($i < count($file_list)) |
539 |
|
) |
540 |
{ |
{ |
541 |
if (is_full_sized_image_file_name($file_list[$i])) |
if (is_full_sized_image_file_name($file_list[$i])) |
542 |
{ |
{ |