1 |
<?php
|
2 |
//--------------------------------------------------------------------------------
|
3 |
//index_page_make.php, v0.1a, Copyright David T. Ashley, 2015
|
4 |
//--------------------------------------------------------------------------------
|
5 |
//This program, a PHP script, reduces all .JPG images in a directory to create
|
6 |
//thumbnails, and creates a PHP index script that will display thumbnails of
|
7 |
//all the images with the thumbnails linking to the full-sized images. This
|
8 |
//script is used by David T. Ashley to make web pages out of large numbers of
|
9 |
//images downloaded from digital cameras. The PHP index script created by this
|
10 |
//program is typically manually edited after it is created to add comments about
|
11 |
//what the pictures are.
|
12 |
//
|
13 |
//Although this program is for Dave Ashley's specific uses, it should be easy
|
14 |
//enough to customize, and it provides an example of how to manipulate images
|
15 |
//using the ImageMagick library compiled into PHP.
|
16 |
//
|
17 |
//This script is designed to be run manually (rather than automatically invoked
|
18 |
//as a result of a web page request). It was written in PHP for convenience
|
19 |
//simply because DreamHost (the web hosting company Dave Ashley uses) has as part
|
20 |
//of its hosting environment PHP with the ImageMagick library compiled in.
|
21 |
//
|
22 |
//Usually, invoke this script using "php index_page_make.php", but the method
|
23 |
//of invocation may vary based on computing platform details.
|
24 |
//--------------------------------------------------------------------------------
|
25 |
//TODO for this script:
|
26 |
// a)The configuration options should just be defaults, and it should be
|
27 |
// possible to override them selectively via command-line options.
|
28 |
// b)Many digital cameras and phones include information in the JPG
|
29 |
// headers, and it would be handy to use (and not use) that information
|
30 |
// in the following ways:
|
31 |
// b1)Date taken--appear in ALT and TITLE so it can be seen on
|
32 |
// mouseover of the thumbnail.
|
33 |
// b2)Location--same as (b1).
|
34 |
// b3)Orientation--tempting to use this, but the web page designer
|
35 |
// has to have final say about how to lay the web page out, including
|
36 |
// photo orientation.
|
37 |
//--------------------------------------------------------------------------------
|
38 |
//This program is free software: you can redistribute it and/or modify
|
39 |
//it under the terms of the GNU General Public License as published by
|
40 |
//the Free Software Foundation, either version 3 of the License, or
|
41 |
//(at your option) any later version.
|
42 |
//
|
43 |
//This program is distributed in the hope that it will be useful,
|
44 |
//but WITHOUT ANY WARRANTY; without even the implied warranty of
|
45 |
//MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
46 |
//GNU General Public License for more details.
|
47 |
//
|
48 |
//You should have received a copy of the GNU General Public License
|
49 |
//along with this program. If not, see http://www.gnu.org/licenses/.
|
50 |
//--------------------------------------------------------------------------------
|
51 |
//C O N F I G U R A T I O N
|
52 |
//--------------------------------------------------------------------------------
|
53 |
//Configuration switches were not tested. E-mail me any script corrections
|
54 |
//(dashley@gmail.com).
|
55 |
define ("CFG_THUMBNAIL_DIMENSION_MAX", 125);
|
56 |
//The maximum dimension of the created thumbnails. Thumbnails are sized so
|
57 |
//that the longest dimension is this many pixels.
|
58 |
define ("CFG_IMAGE_LC_CONVERT", TRUE);
|
59 |
//TRUE if should convert existing image file names to lower case, FALSE
|
60 |
//otherwise.
|
61 |
define ("CFG_THUMBNAIL_BEVELED_BORDER_WIDTH", 4);
|
62 |
//The number of pixels that the thumbnail beveled borders should be.
|
63 |
define ("CFG_THUMBNAIL_FILENAME_SUFFIX", "_small");
|
64 |
//String added just before the filename extension to choose thumbnail names
|
65 |
//based on name of full-sized image.
|
66 |
define ("CFG_LANCZOS_FILTER_APPLY", TRUE);
|
67 |
//TRUE if should apply the Lanczos filter when making the thumbnail, FALSE
|
68 |
//otherwise. I have no idea if using a Lanczos filter improves the quality
|
69 |
//of the thumbnails, but the filter was applied in the PHP example I found
|
70 |
//online. Applying the Lanczos filter typically adds a few seconds to the
|
71 |
//time required to create each thumbnail.
|
72 |
define ("CFG_DTA_PHP_INDEX_CREATE", TRUE);
|
73 |
//TRUE if should create a PHP index page that serves Dave Ashley's needs,
|
74 |
//or FALSE to create a plain vanilla text file with HTML.
|
75 |
define ("CFG_PHP_INDEX_FILE_NAME", "index2.php");
|
76 |
define ("CFG_HTML_INDEX_FILE_NAME", "index2.txt");
|
77 |
//File names to use. Only one of the two will be used, depending on the
|
78 |
//CFG_DTA_PHP_INDEX_CREATE setting.
|
79 |
define ("CFG_CONSOLE_STD_LINE_LEN", 78);
|
80 |
//Number of characters per line preferred for console output.
|
81 |
define ("CFG_INDEX_PARAGRAPH_INDENT", 0);
|
82 |
//For the created index file (either PHP or plain HTML), the number of
|
83 |
//characters that the <p> and </p> tags should be indented.
|
84 |
define ("CFG_INDEX_LINK_INDENT", 0);
|
85 |
//For the created index file, the number of characters that the <a>
|
86 |
//tags should be indented.
|
87 |
//--------------------------------------------------------------------------------
|
88 |
//Calculates important integer indices related to creating a thumbnail, in order
|
89 |
//to minimize distortion in the thumbnail.
|
90 |
//
|
91 |
//When creating a thumbnail, the longer side may be relatively short (perhaps
|
92 |
//100 pixels), and the shorter side may be even shorter (perhaps even as short
|
93 |
//as 10 pixels). If the dimensions of the original image were not changed,
|
94 |
//converting the image to a thumbnail may result in distortion of up to about
|
95 |
//one percent. As an example, suppose that a 1600 x 900 image is converted to a
|
96 |
//thumbnail with the longer side of 125 pixels. The ideal dimension for the
|
97 |
//shorter side would be (900/1600) * 125 = 70.3125 pixels. The aspect ratio of
|
98 |
//the thumbnail can't be made to match the aspect ratio of the original image.
|
99 |
//
|
100 |
//To avoid any aspect ratio distortion that might be noticeable, this program
|
101 |
//chooses the shorter dimension for the thumbnail that is the ceiling of the actual
|
102 |
//quotient, then crops the longer dimension of the original image before the
|
103 |
//conversion to try and match the aspect ratios as closely as possible.
|
104 |
//
|
105 |
//In the example a paragraph or two above, the shorter side of the thumbnail
|
106 |
//would be chosen to be 71 pixels.
|
107 |
//
|
108 |
//Once this choice is made, we want to trim the original image before conversion
|
109 |
//so that n/900 is as close as possible to 125/71. n = (900 * 125) / 71 =
|
110 |
//1584.5070 pixels, so we choose 1585. This keeps the aspect ratio of the
|
111 |
//original image and the thumbnail as close as possible.
|
112 |
//
|
113 |
//Although the longer side of the original image may be reduced prior to the
|
114 |
//conversion to a thumbnail, this information is not written to disk, and
|
115 |
//the original image is not modified. The adjustment of the longer side is
|
116 |
//just a conversion trick to hopefully get better thumbnails.
|
117 |
function calc_thumbnail_conversion_pars
|
118 |
(
|
119 |
$in_orig_longer,
|
120 |
//Longer dimension of original image.
|
121 |
$in_orig_shorter,
|
122 |
//Shorter dimension of original image.
|
123 |
$in_thumbnail_longer,
|
124 |
//Longer dimension of desired thumbnail.
|
125 |
& $out_thumbnail_longer,
|
126 |
//Longer dimension of thumbnail that should be created.
|
127 |
& $out_thumbnail_shorter,
|
128 |
//Shorter dimension of thumbnail that should be created.
|
129 |
& $out_orig_crop_dim_longer,
|
130 |
//The size to crop to on the longer axis of the original.
|
131 |
& $out_orig_crop_dim_shorter,
|
132 |
//The size to crop to on the shorter axis of the original.
|
133 |
& $out_orig_crop_start_longer,
|
134 |
//For use with the Imagick:cropImage() method, the start
|
135 |
//position of the crop on the longer axis.
|
136 |
& $out_orig_crop_start_shorter
|
137 |
//For use with the Imagick:cropImage() method, the start
|
138 |
//position of the crop on the shorter axis.
|
139 |
)
|
140 |
{
|
141 |
//Set the thumbnail shorter dimension to the floor. This means would need to trim
|
142 |
//longest dimension of original to match aspect ratio as closely as possible.
|
143 |
$out_thumbnail_longer = $in_thumbnail_longer;
|
144 |
$out_thumbnail_shorter = ceil(((float)$in_thumbnail_longer *
|
145 |
(float)$in_orig_shorter) / (float)$in_orig_longer);
|
146 |
settype($out_thumbnail_shorter, "integer");
|
147 |
|
148 |
//The aspect ratio of the thumbnail is now set. Try to match the aspect ratio of the larger
|
149 |
//image as closely as possible by selecting a smaller number for the long axis of the
|
150 |
//original image.
|
151 |
$out_orig_crop_dim_longer = round(((float)$in_thumbnail_longer *
|
152 |
(float)$in_orig_shorter) / (float)$out_thumbnail_shorter);
|
153 |
settype($out_orig_crop_dim_longer, "integer");
|
154 |
|
155 |
//The original keeps its shorter dimension unchanged.
|
156 |
$out_orig_crop_dim_shorter = $in_orig_shorter;
|
157 |
|
158 |
//Set the cropping of the longer side of the original to cover half the necessary
|
159 |
//reduction.
|
160 |
$out_orig_crop_start_longer = round(((float)$in_orig_longer -
|
161 |
(float)$out_orig_crop_dim_longer) / 2.0);
|
162 |
settype($out_orig_crop_start_longer, "integer");
|
163 |
|
164 |
//No cropping of original shorter side.
|
165 |
$out_orig_crop_start_shorter = 0;
|
166 |
}
|
167 |
//--------------------------------------------------------------------------------
|
168 |
//Repeats a character to the console output a certain number of times.
|
169 |
function rep_char_con($c, $n)
|
170 |
{
|
171 |
while ($n--)
|
172 |
echo $c;
|
173 |
}
|
174 |
//--------------------------------------------------------------------------------
|
175 |
//Writes a standard thick horizontal line to the console.
|
176 |
function hor_line_thick()
|
177 |
{
|
178 |
rep_char_con("=", CFG_CONSOLE_STD_LINE_LEN);
|
179 |
echo "\n";
|
180 |
}
|
181 |
//--------------------------------------------------------------------------------
|
182 |
//Writes a standard thin horizontal line to the console.
|
183 |
function hor_line_thin()
|
184 |
{
|
185 |
rep_char_con("-", CFG_CONSOLE_STD_LINE_LEN);
|
186 |
echo "\n";
|
187 |
}
|
188 |
//--------------------------------------------------------------------------------
|
189 |
//Returns the name of the file that should be used for the index.
|
190 |
function index_file_name()
|
191 |
{
|
192 |
if (CFG_DTA_PHP_INDEX_CREATE)
|
193 |
return CFG_PHP_INDEX_FILE_NAME;
|
194 |
else
|
195 |
return CFG_HTML_INDEX_FILE_NAME;
|
196 |
}
|
197 |
|
198 |
//--------------------------------------------------------------------------------
|
199 |
//Checks if the index file already exists, returns TRUE if so, or FALSE otherwise.
|
200 |
//Because the sole purpose of this function is to refuse to write over an
|
201 |
//existing index file, it will also return true if a directory of the target
|
202 |
//name exists.
|
203 |
function index_file_or_dir_exists()
|
204 |
{
|
205 |
return file_exists(index_file_name());
|
206 |
}
|
207 |
//--------------------------------------------------------------------------------
|
208 |
//Returns an array of all files in the working directory.
|
209 |
//If no files can be found, returns FALSE.
|
210 |
function get_file_names_in_dir()
|
211 |
{
|
212 |
//Get directory list.
|
213 |
$rv = scandir (".");
|
214 |
|
215 |
//If the list is empty, something went wrong. Return FALSE.
|
216 |
if ($rv === FALSE)
|
217 |
return FALSE;
|
218 |
|
219 |
return $rv;
|
220 |
}
|
221 |
//--------------------------------------------------------------------------------
|
222 |
//Returns TRUE if a file name appears to be a valid .JPG file name, or FALSE
|
223 |
//otherwise.
|
224 |
function is_valid_jpg_file_name($s)
|
225 |
{
|
226 |
//First, the file name has to be AT LEAST 5 characters long. This would
|
227 |
//be a short name like "x.jpg".
|
228 |
if (strlen($s) < 5)
|
229 |
return FALSE;
|
230 |
|
231 |
//Convert the string name to all lower case.
|
232 |
$s = strtolower($s);
|
233 |
|
234 |
//If the final 4 characters are .jpg, we call it good. We've already
|
235 |
//verified that the string is at least of length 5.
|
236 |
$s2 = substr($s, strlen($s) - 4);
|
237 |
if (strcmp($s2, ".jpg") == 0)
|
238 |
return TRUE;
|
239 |
|
240 |
//The only remaining possibility for validity is that the string is of the
|
241 |
//form ".jpeg". Check for this.
|
242 |
//The file name has to be AT LEAST 6 characters long. This would
|
243 |
//be a short name like "x.jpeg".
|
244 |
if (strlen($s) < 6)
|
245 |
return FALSE;
|
246 |
|
247 |
//If the final 5 characters are .jpeg, we call it good. We've already
|
248 |
//verified that the string is at least of length 6.
|
249 |
$s2 = substr($s, strlen($s) - 5);
|
250 |
if (strcmp($s2, ".jpeg") == 0)
|
251 |
return TRUE;
|
252 |
|
253 |
//If we are here, not a valid .jpg file name.
|
254 |
return FALSE;
|
255 |
}
|
256 |
//--------------------------------------------------------------------------------
|
257 |
//As a function of the file name, creates the file name for the thumbnail.
|
258 |
function file_name_to_thumbnail_name($in_filename)
|
259 |
{
|
260 |
$extension_start = strrpos($in_filename, ".");
|
261 |
//Find position of last "." in string. This should precede the
|
262 |
//file extension.
|
263 |
if ($extension_start === FALSE)
|
264 |
{
|
265 |
//"." not found. Should not happen. Filenames were checked in advance.
|
266 |
echo "Fatal internal error at line " . __LINE__ . "\n";
|
267 |
exit(1);
|
268 |
}
|
269 |
|
270 |
$filename_prefix = substr($in_filename, 0, $extension_start);
|
271 |
$filename_extension = substr($in_filename, $extension_start);
|
272 |
|
273 |
$rv = $filename_prefix . CFG_THUMBNAIL_FILENAME_SUFFIX . $filename_extension;
|
274 |
|
275 |
return $rv;
|
276 |
}
|
277 |
//--------------------------------------------------------------------------------
|
278 |
//Writes the index file preamble to the index file. This is very much
|
279 |
//dependent on how the thumbnails will be used. This is currently customized
|
280 |
//for what Dave Ashley does.
|
281 |
function write_index_file_preamble($handle)
|
282 |
{
|
283 |
fwrite($handle, "<" . "?" . "php\n");
|
284 |
fwrite($handle, "require_once(\"style/std/stdwpstyle.inc\");\n");
|
285 |
fwrite($handle, "?" . ">" . "\n");
|
286 |
fwrite($handle, "<" . "?" . "php\n");
|
287 |
fwrite($handle, " $" . "thispage = new StdWpStyle();\n");
|
288 |
fwrite($handle, "\n");
|
289 |
fwrite($handle, " $" . "thispage->static_page_header_title_std(\"TBD\",\n");
|
290 |
fwrite($handle, " \"TBD\",\n");
|
291 |
fwrite($handle, " \"\");\n");
|
292 |
fwrite($handle, " $" . "thispage->static_page_photo_thumbnail_explanation(TRUE, TRUE);\n");
|
293 |
fwrite($handle, "?" . ">" . "\n");
|
294 |
for ($i = 0; $i < CFG_INDEX_PARAGRAPH_INDENT; $i++)
|
295 |
{
|
296 |
fwrite($handle, " ");
|
297 |
}
|
298 |
fwrite($handle, "\n<p align=\"center\">\n");
|
299 |
}
|
300 |
//--------------------------------------------------------------------------------
|
301 |
//Writes the index file postamble to the index file. This is very much
|
302 |
//dependent on how the thumbnails will be used. This is currently customized
|
303 |
//for what Dave Ashley does.
|
304 |
function write_index_file_postamble($handle)
|
305 |
{
|
306 |
for ($i = 0; $i < CFG_INDEX_PARAGRAPH_INDENT; $i++)
|
307 |
{
|
308 |
fwrite($handle, " ");
|
309 |
}
|
310 |
fwrite($handle, "</p>\n\n");
|
311 |
fwrite($handle, "<" . "?" . "php\n");
|
312 |
fwrite($handle, " $" . "thispage->static_page_footer_std();\n");
|
313 |
fwrite($handle, "?" . ">" . "\n");
|
314 |
}
|
315 |
//--------------------------------------------------------------------------------
|
316 |
//Actually creates the thumbnail, and returns some information about what was
|
317 |
//done.
|
318 |
//
|
319 |
//The calls to the ImagMagick library take on the order of 5s per image if there
|
320 |
//is filtering.
|
321 |
function create_thumbnail( $in_filename,
|
322 |
$in_thumbnailname,
|
323 |
& $out_filename_filesize,
|
324 |
& $out_filename_xdim,
|
325 |
& $out_filename_ydim,
|
326 |
& $out_thumbnailname_filesize,
|
327 |
& $out_thumbnailname_xdim,
|
328 |
& $out_thumbnailname_ydim)
|
329 |
{
|
330 |
//Assign output parameters just in case something doesn't get assigned.
|
331 |
$out_filename_filesize = 0;
|
332 |
$out_filename_xdim = 0;
|
333 |
$out_filename_ydim = 0;
|
334 |
$out_thumbnailname_filesize = 0;
|
335 |
$out_thumbnailname_xdim = 0;
|
336 |
$out_thumbnailname_ydim = 0;
|
337 |
|
338 |
//Establish target dimensions. Two cases, depending on which is the longer
|
339 |
//side.
|
340 |
|
341 |
//Construct.
|
342 |
$imagick = new Imagick();
|
343 |
|
344 |
//Load image.
|
345 |
$imagick->readImage($in_filename);
|
346 |
|
347 |
//Get the dimensions of the image we just loaded.
|
348 |
$geo = $imagick->getImageGeometry();
|
349 |
$out_filename_xdim = $geo['width'];
|
350 |
$out_filename_ydim = $geo['height'];
|
351 |
|
352 |
//Calculate target sizes. We rearrange parameters based on which is our
|
353 |
//longest side.
|
354 |
if ($out_filename_xdim >= $out_filename_ydim)
|
355 |
{
|
356 |
//Longer width (x-dimension), or square.
|
357 |
calc_thumbnail_conversion_pars
|
358 |
(
|
359 |
$out_filename_xdim,
|
360 |
$out_filename_ydim,
|
361 |
CFG_THUMBNAIL_DIMENSION_MAX,
|
362 |
$out_thumbnailname_xdim,
|
363 |
$out_thumbnailname_ydim,
|
364 |
$orig_crop_dim_x,
|
365 |
$orig_crop_dim_y,
|
366 |
$orig_crop_start_x,
|
367 |
$orig_crop_start_y
|
368 |
);
|
369 |
}
|
370 |
else
|
371 |
{
|
372 |
//Longer height (y-dimension).
|
373 |
calc_thumbnail_conversion_pars
|
374 |
(
|
375 |
$out_filename_ydim,
|
376 |
$out_filename_xdim,
|
377 |
CFG_THUMBNAIL_DIMENSION_MAX,
|
378 |
$out_thumbnailname_ydim,
|
379 |
$out_thumbnailname_xdim,
|
380 |
$orig_crop_dim_y,
|
381 |
$orig_crop_dim_x,
|
382 |
$orig_crop_start_y,
|
383 |
$orig_crop_start_x
|
384 |
);
|
385 |
}
|
386 |
|
387 |
//For debugging only, might want to know intermediate calculation results.
|
388 |
//echo "xcropdim, ycropdim, xcropstart, ycropstart: "
|
389 |
// .
|
390 |
// $orig_crop_dim_x
|
391 |
// .
|
392 |
// " "
|
393 |
// .
|
394 |
// $orig_crop_dim_y
|
395 |
// .
|
396 |
// " "
|
397 |
// .
|
398 |
// $orig_crop_start_x
|
399 |
// .
|
400 |
// " "
|
401 |
// .
|
402 |
// $orig_crop_start_y
|
403 |
// .
|
404 |
// "\n";
|
405 |
|
406 |
//Crop the original to try to preserve the aspect ratio of the thumbnail
|
407 |
//as precisely as possible.
|
408 |
$imagick->cropImage(
|
409 |
$orig_crop_dim_x,
|
410 |
$orig_crop_dim_y,
|
411 |
$orig_crop_start_x,
|
412 |
$orig_crop_start_y
|
413 |
);
|
414 |
|
415 |
//For debugging only, might want to get a look at the cropped image, to
|
416 |
//be sure nothing unexpected happens on the canvas.
|
417 |
//$imagick->writeImage($in_filename . ".cropped.jpg");
|
418 |
|
419 |
//Resize to thumbnail size.
|
420 |
if (CFG_LANCZOS_FILTER_APPLY)
|
421 |
{
|
422 |
$imagick->resizeImage($out_thumbnailname_xdim,
|
423 |
$out_thumbnailname_ydim,
|
424 |
Imagick::FILTER_LANCZOS,
|
425 |
1);
|
426 |
}
|
427 |
else
|
428 |
{
|
429 |
$imagick->resizeImage($out_thumbnailname_xdim,
|
430 |
$out_thumbnailname_ydim,
|
431 |
0,
|
432 |
1);
|
433 |
}
|
434 |
|
435 |
//Create the border.
|
436 |
$imagick->raiseImage(CFG_THUMBNAIL_BEVELED_BORDER_WIDTH,
|
437 |
CFG_THUMBNAIL_BEVELED_BORDER_WIDTH,
|
438 |
0,
|
439 |
0,
|
440 |
1);
|
441 |
|
442 |
//Set compression to get a smaller thumbnail written, and strip
|
443 |
//header information. stripImage() seems to have the largest effect
|
444 |
//on thumbnail file size, so leaving the thumbnail quality near 100%
|
445 |
//is feasible. The jump in file size between 90% and 95% seemed to be
|
446 |
//fairly large (40% to 50%), so I left it at 90%. My rationale is
|
447 |
//that with the proliferation of mobile devices and cellular data,
|
448 |
//getting the thumbnail as small as possible is more important than
|
449 |
//the thumbnail looking perfect. If the viewer wants a perfect image,
|
450 |
//they can view the full-sized image.
|
451 |
$imagick->setImageCompression(Imagick::COMPRESSION_JPEG);
|
452 |
$imagick->setImageCompressionQuality(90);
|
453 |
$imagick->stripImage();
|
454 |
|
455 |
//Write the thumbnail.
|
456 |
$imagick->writeImage($in_thumbnailname);
|
457 |
|
458 |
//Destroy to prevent memory leak.
|
459 |
$imagick->clear();
|
460 |
$imagick->destroy();
|
461 |
|
462 |
//All of the writing is done. Try to obtain the file sizes.
|
463 |
$fsize = filesize($in_filename);
|
464 |
if ($fsize !== FALSE)
|
465 |
$out_filename_filesize = $fsize;
|
466 |
$fsize = filesize($in_thumbnailname);
|
467 |
if ($fsize !== FALSE)
|
468 |
$out_thumbnailname_filesize = $fsize;
|
469 |
}
|
470 |
//--------------------------------------------------------------------------------
|
471 |
//Writes a line to the index file corresponding to a linked larger picture
|
472 |
//reprsented by a thumbnail.
|
473 |
function write_index_line( $handle,
|
474 |
$filename,
|
475 |
$thumbnailname,
|
476 |
$filename_filesize,
|
477 |
$filename_xdim,
|
478 |
$filename_ydim,
|
479 |
$thumbnailname_filesize,
|
480 |
$thumbnailname_xdim,
|
481 |
$thumbnailname_ydim)
|
482 |
{
|
483 |
//<a href="dscn1258.jpg"><img border="0" src="dscn1258_small.jpg" alt="dscn1258.jpg (4932338 bytes)" width="125" height="93"></a>
|
484 |
//Indent.
|
485 |
for ($i = 0; $i < CFG_INDEX_LINK_INDENT; $i++)
|
486 |
{
|
487 |
fwrite($handle, " ");
|
488 |
}
|
489 |
|
490 |
fwrite($handle, "<a href=\"");
|
491 |
fwrite($handle, $filename);
|
492 |
fwrite($handle, "\"><img border=\"0\" src=\"");
|
493 |
fwrite($handle, $thumbnailname);
|
494 |
fwrite($handle, "\" alt=\"");
|
495 |
fwrite($handle, $filename);
|
496 |
fwrite($handle, " (");
|
497 |
fwrite($handle, $filename_filesize);
|
498 |
fwrite($handle, " bytes)\" ");
|
499 |
fwrite($handle, "title=\"");
|
500 |
fwrite($handle, $filename);
|
501 |
fwrite($handle, " (");
|
502 |
fwrite($handle, $filename_filesize);
|
503 |
fwrite($handle, " bytes)\" width=\"");
|
504 |
fwrite($handle, $thumbnailname_xdim);
|
505 |
fwrite($handle, "\" height=\"");
|
506 |
fwrite($handle, $thumbnailname_ydim);
|
507 |
fwrite($handle, "\"></a>");
|
508 |
fwrite($handle, "\n");
|
509 |
}
|
510 |
//--------------------------------------------------------------------------------
|
511 |
//Write introductory message.
|
512 |
hor_line_thick();
|
513 |
echo "Execution begins.\n";
|
514 |
hor_line_thin();
|
515 |
//-----------------------------------------------------------------------------
|
516 |
//End the script of the index file already exists. Don't want to overwrite
|
517 |
//anything that may be valuable to the user.
|
518 |
if (index_file_or_dir_exists())
|
519 |
{
|
520 |
echo "Index file \"" . index_file_name() . "\" already exists.\n";
|
521 |
echo "Script cannot continue. Exiting.\n";
|
522 |
exit(1);
|
523 |
}
|
524 |
else
|
525 |
{
|
526 |
echo "Index file \"" . index_file_name() . "\" does not exist. " .
|
527 |
"Continuing.\n";
|
528 |
}
|
529 |
hor_line_thin();
|
530 |
//-----------------------------------------------------------------------------
|
531 |
//Get and emit the names of everything in the directory.
|
532 |
$file_list = get_file_names_in_dir();
|
533 |
if ($file_list === FALSE)
|
534 |
{
|
535 |
echo "List of files from PHP function scandir() is empty.\n";
|
536 |
echo "Serious internal error, or nothing to do. Script cannot continue.\n";
|
537 |
exit(1);
|
538 |
}
|
539 |
else
|
540 |
{
|
541 |
echo "Files in working directory (unsorted, unfiltered, "
|
542 |
.
|
543 |
count($file_list)
|
544 |
.
|
545 |
" files):\n";
|
546 |
for ($i = 0; $i < count($file_list); $i++)
|
547 |
echo " " . sprintf("[%5d]", $i) . " " . $file_list[$i] . "\n";
|
548 |
}
|
549 |
hor_line_thin();
|
550 |
//-----------------------------------------------------------------------------
|
551 |
//Figure out which file names are valid for .JPG files and create a
|
552 |
//list containing only those.
|
553 |
$found = FALSE;
|
554 |
for ($i = 0; $i < count($file_list); $i++)
|
555 |
{
|
556 |
if (is_valid_jpg_file_name($file_list[$i]))
|
557 |
{
|
558 |
$found = TRUE;
|
559 |
$staging_list[] = $file_list[$i];
|
560 |
}
|
561 |
}
|
562 |
|
563 |
if (!$found)
|
564 |
{
|
565 |
echo "There were no filenames corresponding to JPEG files.\n";
|
566 |
echo "Script cannot continue.\n";
|
567 |
exit(1);
|
568 |
}
|
569 |
|
570 |
$file_list = $staging_list;
|
571 |
unset($staging_list);
|
572 |
//-----------------------------------------------------------------------------
|
573 |
//Sort the filenames, and provide the user with a list.
|
574 |
sort($file_list, SORT_STRING);
|
575 |
echo "JPEG file to be indexed (filtered, sorted, "
|
576 |
.
|
577 |
count($file_list)
|
578 |
.
|
579 |
" files):\n";
|
580 |
for ($i = 0; $i < count($file_list); $i++)
|
581 |
echo " " . sprintf("[%5d]", $i) . " " . $file_list[$i] . "\n";
|
582 |
hor_line_thin();
|
583 |
//-----------------------------------------------------------------------------
|
584 |
//Create a parallel list that contains the proposed names of files with new
|
585 |
//names that are all lower-case. If the lower-casing option is turned off,
|
586 |
//just create a parallel identical list.
|
587 |
for ($i = 0; $i < count($file_list); $i++)
|
588 |
{
|
589 |
if (CFG_IMAGE_LC_CONVERT)
|
590 |
$file_list_lc[$i] = strtolower($file_list[$i]);
|
591 |
else
|
592 |
$file_list_lc[$i] = $file_list[$i];
|
593 |
}
|
594 |
//-----------------------------------------------------------------------------
|
595 |
//Look for collisions between the two lists, and abort if there are collisions.
|
596 |
//A collision essentially means that at least one file is differentiated from
|
597 |
//at least one other file based only on case. This kind of lazy comparison
|
598 |
//is quadratic, but with 10,000 files it would involve about 10,000,000
|
599 |
//comparisons--not too bad. But for any serious computing, this couldn't be
|
600 |
//done that way it is.
|
601 |
$collisions = 0;
|
602 |
for ($i = 0; $i < count($file_list); $i++)
|
603 |
{
|
604 |
for ($j = 0; $j < count($file_list); $j++)
|
605 |
{
|
606 |
if (($i != $j) && (strcmp($file_list[$i], $file_list_lc[$j]) == 0))
|
607 |
$collisions++;
|
608 |
}
|
609 |
}
|
610 |
|
611 |
if ($collisions != 0)
|
612 |
{
|
613 |
echo "There are filename collisions that have resulted from filenames\n";
|
614 |
echo "differing only in case. The script cannot continue.\n";
|
615 |
exit(1);
|
616 |
}
|
617 |
//-----------------------------------------------------------------------------
|
618 |
//Convert any filenames that need it to lower case.
|
619 |
if (CFG_IMAGE_LC_CONVERT)
|
620 |
{
|
621 |
for ($i = 0; $i < count($file_list); $i++)
|
622 |
{
|
623 |
if (strcmp($file_list[$i], $file_list_lc[$i]) == 0)
|
624 |
{
|
625 |
echo "Skipped rename of \"" . $file_list[$i] .
|
626 |
"\" to lower case.\n";
|
627 |
}
|
628 |
else
|
629 |
{
|
630 |
$result = rename($file_list[$i], $file_list_lc[$i]);
|
631 |
if ($result === TRUE)
|
632 |
{
|
633 |
echo "Renamed \""
|
634 |
.
|
635 |
$file_list[$i]
|
636 |
.
|
637 |
"\" to \""
|
638 |
.
|
639 |
$file_list_lc[$i]
|
640 |
.
|
641 |
"\".\n";
|
642 |
}
|
643 |
else
|
644 |
{
|
645 |
echo "Rename of \""
|
646 |
.
|
647 |
$file_list[$i]
|
648 |
.
|
649 |
"\" to \""
|
650 |
.
|
651 |
$file_list_lc[$i]
|
652 |
.
|
653 |
"\" failed.\n";
|
654 |
echo "The script cannot continue.\n";
|
655 |
exit(1);
|
656 |
}
|
657 |
}
|
658 |
}
|
659 |
|
660 |
hor_line_thin();
|
661 |
}
|
662 |
//-----------------------------------------------------------------------------
|
663 |
//At this point, the lower-case list can be discarded.
|
664 |
//-----------------------------------------------------------------------------
|
665 |
$file_list = $file_list_lc;
|
666 |
unset($file_list_lc);
|
667 |
//-----------------------------------------------------------------------------
|
668 |
//Create a parallel list of thumbnail file names.
|
669 |
for ($i = 0; $i < count($file_list); $i++)
|
670 |
{
|
671 |
$thumbnail_list[$i] = file_name_to_thumbnail_name($file_list[$i]);
|
672 |
//echo $thumbnail_list[$i] . "\n";
|
673 |
}
|
674 |
//-----------------------------------------------------------------------------
|
675 |
//Check for duplicates. A duplicate might occur, for example, if "x.jpg"
|
676 |
//and "x_small.jpg" both exist.
|
677 |
$collisions = 0;
|
678 |
for ($i = 0; $i < count($file_list); $i++)
|
679 |
{
|
680 |
for ($j = 0; $j < count($thumbnail_list); $j++)
|
681 |
{
|
682 |
if (($i != $j) && (strcmp($file_list[$i], $file_list_lc[$j]) == 0))
|
683 |
$collisions++;
|
684 |
}
|
685 |
}
|
686 |
|
687 |
if ($collisions != 0)
|
688 |
{
|
689 |
echo "There are filename collisions that have resulted when trying\n";
|
690 |
echo "to find thumbnail names. Very likely some thumbnails already\n";
|
691 |
echo "exist. The script cannot continue.\n";
|
692 |
exit(1);
|
693 |
}
|
694 |
//-----------------------------------------------------------------------------
|
695 |
//Open up the index file for writing. Abort script if unsuccessful.
|
696 |
$index_handle = fopen(index_file_name(), "w");
|
697 |
if ($index_handle === FALSE)
|
698 |
{
|
699 |
echo "Failed to open \"" . index_file_name() . "\" for writing.\nScript cannot continue.\n";
|
700 |
exit(1);
|
701 |
}
|
702 |
else
|
703 |
{
|
704 |
echo "Successfully opened \"" . index_file_name() . "\" for writing.\n";
|
705 |
hor_line_thin();
|
706 |
}
|
707 |
//-----------------------------------------------------------------------------
|
708 |
//Write the index file preamble to the index file. The preamble is everything
|
709 |
//up until the lines that reference the thumbnails.
|
710 |
write_index_file_preamble($index_handle);
|
711 |
//-----------------------------------------------------------------------------
|
712 |
//For each file, create the thumbnail and write the line in the index file.
|
713 |
for ($i = 0; $i < count($file_list); $i++)
|
714 |
{
|
715 |
echo "Creating thumbnail \"" .
|
716 |
$thumbnail_list[$i] .
|
717 |
"\" from image \"" .
|
718 |
$file_list[$i] .
|
719 |
"\".\n";
|
720 |
create_thumbnail($file_list[$i],
|
721 |
$thumbnail_list[$i],
|
722 |
$filename_filesize,
|
723 |
$filename_xdim,
|
724 |
$filename_ydim,
|
725 |
$thumbnail_filesize,
|
726 |
$thumbnail_xdim,
|
727 |
$thumbnail_ydim);
|
728 |
echo "Conversion complete.\n";
|
729 |
echo " Full-sized image file size/xdim/ydim = " .
|
730 |
$filename_filesize . "/" . $filename_xdim . "/" . $filename_ydim .
|
731 |
",\n";
|
732 |
echo " Thumbnail image filesize/xdim/ydim = " .
|
733 |
$thumbnail_filesize . "/" . $thumbnail_xdim . "/" . $thumbnail_ydim .
|
734 |
",\n";
|
735 |
write_index_line($index_handle,
|
736 |
$file_list[$i],
|
737 |
$thumbnail_list[$i],
|
738 |
$filename_filesize,
|
739 |
$filename_xdim,
|
740 |
$filename_ydim,
|
741 |
$thumbnail_filesize,
|
742 |
$thumbnail_xdim,
|
743 |
$thumbnail_ydim);
|
744 |
hor_line_thin();
|
745 |
}
|
746 |
//-----------------------------------------------------------------------------
|
747 |
//Write the index file postamble to the index file. The postamble is
|
748 |
//everything after the lines that reference the thumbnails.
|
749 |
write_index_file_postamble($index_handle);
|
750 |
//-----------------------------------------------------------------------------
|
751 |
//Close the index file, not checking for error codes.
|
752 |
fclose($index_handle);
|
753 |
//-----------------------------------------------------------------------------
|
754 |
echo "Execution ends.\n";
|
755 |
hor_line_thick();
|
756 |
//--------------------------------------------------------------------------------
|
757 |
//End of File
|
758 |
//--------------------------------------------------------------------------------
|
759 |
?>
|