mkthumb2.php to HTML

index

USE AT OWN RISK

Generated: Tue Jul 31 15:22:10 2007 from mkthumb2.php 2005/11/25 13.2 KB bytes.

<?php

/* mkthumb2.php

 AIM: Process a FOLDER, finding JPEG image files,
 Check if a file contains a thumbnail image
 If YES, extract and write to disk, AND
 try to COPY the jpeg, WITHOUT all the EXIF info, and
 write that to disk also ... check size of new file
 If it contains NO THUMB, then generate one, and write
 that to disk ...
 NOTE: Uses PHP built-in functions and does NOT require
 any tool kit! But does need EXIF enabled in PHP
 1. Set the $maindir, as the INPUT FOLDER
 2. Set the $path_out, as the OUPTUT FOLDER
 Geoff McLane - 25 November, 2005

 */

// ############################################
$timestart = getmicrotime(); // get time array secs and usecs ...
//$maindir = "c:/HOMEPAGE/Max5/images" ; //change this to what ever directory needs scanning
$maindir = "c:/HOMEPAGE/P26/mona" ; //change this to what ever directory needs scanning
// $path_out = 'images';
//$path_out = 'thumbs';
$path_out = 'temp';
$meol = "\r\n";
$quality = 100; // was 75, default IJG
$mydir = opendir($maindir) ;
if( !$mydir ) {
   echo "ERROR: Can not OPEN directory [$maindir] ... aborting ... $meol";
   exit(3);
}

$inp_file = '';
$total_size = 0; // total in file size = bytes processed
$total_th = 0; // total of thumbs written
$total_new = 0; // total of NEW files created
$diff_plus = 0;
$diff_minus = 0;
$with_thumb = 0;
$without_th = 0;
$file_count = 0;
while($fn = readdir($mydir)) //scan through the whole directory
{
   if( !(($fn == '.') || ($fn == '..')) ) {
   $inp_file = $maindir . '/' . $fn;
   if( IsJPEGFile( $inp_file ) ) {
   $file_count++;
   ProcessFile($inp_file, $path_out);
   }
   }
}
closedir($mydir);
echo "Quality set at $quality ...<br>$meol";
echo "Processed $file_count jpg files, total $total_size bytes ...<br>$meol";
echo "Processed $with_thumb with thumbs, and $without_th without thumbs ...<br>$meol";
echo "Total $total_th bytes in thumbs, and total $total_new bytes in new files ... tot =";
echo ($total_th + $total_new) . " bytes<br>$meol";
echo "Positive difference = $diff_plus, and negative difference = $diff_minus ... sum = ";
echo ($diff_plus + $diff_minus) . "<br>$meol";
$timeend = getmicrotime();
$time = $timeend - $timestart;
echo "Script ran for $time seconds ...<br>$meol";
// ############################################

// ###########################################
// Functions used below
// ###########################################
// ProcessFile( string input_file, string output path )
function ProcessFile( $in_file, $out_path ) {
   global $meol;
   global $total_size; // total in file size = bytes processed
   global $total_th; // total of thumbs written
   global $total_new; // total of NEW files created
   global $diff_plus, $diff_minus, $with_thumb, $without_th;
   global $quality;
   if ( !file_exists( $in_file ) ) {
   echo "ERROR: Can NOT locate file '$in_file' ... aborting ...$meol";
   exit(1);
   }
   $th_size = 0;
   $new_size = 0;
   $file_size = filesize( $in_file );
   // separate path and filename
   $parts = split("/", $in_file);
   $pc = count($parts);
   if( $pc > 1 ) {
   $file_name = $parts[$pc-1]; // last part is FILE NAME
   $patha = array_slice($parts,0,$pc-1); // get array of parts, excluding last ...
   $file_path = implode( "/", $patha);
   } else {
   $file_name = $parts[$pc-1]; // get FILE NAME
   $file_path = ".";
   }
   // echo "Got path of '$file_path', and file name of '$file_name' ...size = $file_size bytes<br>$meol";
   // separate file name and extention
   $parts = split("\.", $file_name);
   $pc = count($parts);
   if( $pc > 1 ) {
   $file_ext = $parts[$pc-1]; // last part is FILE NAME
   $filea = array_slice($parts,0,$pc-1);
   $file_tit = implode( ".", $filea );
   } else {
   $file_tit = $file_name;
   $file_ext = "";
   }
   // got ORIGINAL file size, name, and extension
   echo "Got file title '$file_tit', extension '$file_ext' ... size = $file_size bytes<br>$meol";
   if (strtolower( $file_ext ) != 'jpg') {
   echo "ERROR: NOT a JPG file ... aborting ...<br>$meol";
   exit(2);
   }

   $src_img = imagecreatefromjpeg("$in_file");
   // $src_img = LoadJpeg("$in_file");
   if ( !$src_img ) {
   echo "ERROR: FAILED to load JPEG image ... aborting ...<br>$meol";
   exit(3);
   }
   // get it's height and width
   $imgSx = imagesx($src_img);
   $imgSy = imagesy($src_img);
   if( ($imgSx == 0) || ($imgSy == 0) ) {
   echo "ERROR: FAILED to get image size! ... aborting ...<br>$meol";
   exit(4);
   }
   echo "Loaded '$in_file', of size $imgSx X $imgSy ...<br>$meol";
   $thumb_name = $out_path . '/' . $file_tit . '-t.' . $file_ext;
   $new_name = $out_path . '/' . $file_tit . '-n.' . $file_ext;
   // see if there is ALREADY a thumbnail image embedded ...
   $thumb_data = exif_thumbnail($in_file, $in_width, $in_height, $in_type);
   if ( $thumb_data ) {
   $with_thumb++;
   echo "Got thumbnail image $in_width X $in_height ($in_type) ...<br>$meol";
   $fp = fopen("$thumb_name","wb");
   fputs($fp,$thumb_data);
   fclose($fp);
   $th_size = filesize($thumb_name);
   print " Embedded Thumbnail IMAGE written to disk, as '$thumb_name' ... size = $th_size<br>$meol";
   print "<img src='$thumb_name'><br>$meol";
   $new_jpeg = imagecreatetruecolor($imgSx, $imgSy); /* Create a black image */
   if ( !$new_jpeg ) {
   echo "WARNING: FAILED to create new image ... <br>$meol";
   } else {
   if( imagecopy($new_jpeg,$src_img,0,0,0,0,$imgSx,$imgSy) ) {
   if( imagejpeg( $new_jpeg, $new_name, $quality ) ) {
   $new_size = filesize($new_name);
   $diff_size = $file_size - ($th_size + $new_size);
   echo "New written to $new_name - New size = $new_size bytes - diff = $diff_size<br>$meol";
   print "<table><tr>$meol";
   print "<td><img src='$in_file'></td>$meol";
   print "<td><img src='$new_name'></td>$meol";
   print "</tr></table>$meol";
   } else {
   echo "WARNING: Write New image to $new_name FAILED!...<br>$meol";
   }
   } else {
   echo "WARNING: FAILED to COPY image ...<br>$emeol";
   }
   imagedestroy( $new_jpeg );
   }
   } else {
   $without_th++;
   echo "Note: No thumbnail image embedded in this JPEG file ...<br>$meol";
   $ratio = $imgSx/$imgSy;
   if($ratio > 1) {
   $new_imgSx = 150;
   $new_imgSy = round(150 / $ratio);
   } else {
   $new_imgSx = round(150 * $ratio);
   $new_imgSy = 150;
   }
   print " Ratio is $ratio ... New sizes $new_imgSx x $new_imgSy ...<br>$meol";
   $dst_img = imagecreatetruecolor($new_imgSx,$new_imgSy);
   if ($dst_img) {
   /* create the scaled instance */
   if ( ImageCopyResampled($dst_img,$src_img,0,0,0,0,$new_imgSx,$new_imgSy,$imgSx,$imgSy) ) {
   /* write to disk */
   if ( imageJpeg($dst_img,"$thumb_name") ) {
   $th_size = filesize($thumb_name);
   print " Thumbnail IMAGE written to disk, as '$thumb_name' ... size = $th_size<br>$meol";
   print "<img src='$thumb_name'><br>$meol";
   print "Copying $in_file to $new_name ...<br>$meol";
   if ( copy( $in_file, $new_name ) ) {
   // succeeded in making a COPY
   $new_size = filesize($new_name);
   $diff_size = $file_size - ($th_size + $new_size);
   echo "New written to $new_name - New size = $new_size bytes - diff = $diff_size<br>$meol";
   print "<table><tr>$meol";
   print "<td><img src='$in_file'></td>$meol";
   print "<td><img src='$new_name'></td>$meol";
   print "</tr></table>$meol";
   } else {
   echo "WARNING: Copy of $in_file FAILED!<br>$meol";
   }
   } else {
   echo "WARNING: imagejpeg(...) FAILED ... <br>$meol";
   }
   } else {
   echo "WARNING: ImageCopyResampled FAILED ...<br>$meol";
   }
   } else {
   echo "WARNING: FAILED to create new thumb image ...<br>$meol";
   }
   }
   $total_size += $file_size; // total in file size = bytes processed
   $total_th += $th_size; // total of thumbs written
   $total_new += $new_size; // total of NEW files created
   if( $diff_size > 0 ) {
   $diff_plus += $diff_size;
   } else {
   $diff_minus += $diff_size;
   }
}

// unused function
//function LoadJpeg($imgname) 
//{
// $im = @imagecreatefromjpeg($imgname); /* Attempt to open */
// if (!$im) { /* See if it failed */
// $im = imagecreatetruecolor(150, 30); /* Create a black image */
// $bgc = imagecolorallocate($im, 255, 255, 255);
// $tc = imagecolorallocate($im, 0, 0, 0);
// imagefilledrectangle($im, 0, 0, 150, 30, $bgc);
// /* Output an errmsg */
// imagestring($im, 1, 5, 5, "Error loading $imgname", $tc);
// }
// return $im;
//}

// unused - output directly to BROWSER
// note: this does NOT appear to work in a local
// command line use ...
//function OutIData($newimage) {
//ob_start(); // start a new output buffer
// imagejpeg( $newimage, "", 75 );
// $ImageData = ob_get_contents();
// $ImageDataLength = ob_get_length();
//ob_end_clean(); // stop this output buffer
//header("Content-type: image/jpeg") ;
//header("Content-Length: ".$ImageDataLength);
//echo $ImageData;
// }
function IsJPEGFile( $in_file ) {
   if ( strtolower( substr($in_file,-4,4) ) == '.jpg' ) {
   return 1;;
   } else if ( strtolower( substr($in_file,-5,5) ) == '.jpeg' ) {
   return 1;
   }
   return 0;
}

function IsJPEGFile2( $in_file ) {
   global $meol;
   if ( !file_exists( $in_file ) ) {
   // echo "ERROR: Can NOT locate file '$in_file' ... aborting ...$meol";
   return 0;
   }
   $file_size = filesize( $in_file );
   // separate path and filename
   $parts = split("/", $in_file);
   $pc = count($parts);
   if( $pc > 1 ) {
   $file_name = $parts[$pc-1]; // last part is FILE NAME
   $patha = array_slice($parts,0,$pc-1); // get array of parts, excluding last ...
   $file_path = implode( "/", $patha);
   } else {
   $file_name = $parts[$pc-1]; // get FILE NAME
   $file_path = ".";
   }
   // echo "Got path of '$file_path', and file name of '$file_name' ...size = $file_size bytes<br>$meol";

   // separate file name and extention
   $parts = split("\.", $file_name);
   $pc = count($parts);
   if( $pc > 1 ) {
   $file_ext = $parts[$pc-1]; // last part is FILE NAME
   $filea = array_slice($parts,0,$pc-1);
   $file_tit = implode( ".", $filea );
   } else {
   $file_tit = $file_name;
   $file_ext = "";
   }

   // echo "Got file title '$file_tit', extension '$file_ext' ... <br>$meol";
   if (strtolower( $file_ext ) != 'jpg') {
   // echo "ERROR: NOT a JPG file ... aborting ...<br>$meol";
   return 0;
   }
   return 1;
}

function getmicrotime() {
 $temparray=split(" ",microtime());
 $returntime=$temparray[0]+$temparray[1];
 return $returntime;
} 

/* Experimenting with the QUALITY setting
 ======================================

With $quality = 75;
Processed 89 jpg files, total 3884136 bytes ...
Processed 46 with thumbs, and 43 without thumbs ...
Total 415628 bytes in thumbs, and total 2020616 bytes in new files ... tot =2436244 bytes
Positive difference = 1619222, and negative difference = -171330 ... sum = 1447892

This represents a substantial GAIN ... there is an overall REDUCTION in disk size of 1.4MB!
But it is clear the new image does NOT look as good ... REJECTED!

With $quality = 80;
Processed 89 jpg files, total 3884136 bytes ...
Processed 46 with thumbs, and 43 without thumbs ...
Total 415628 bytes in thumbs, and total 2125703 bytes in new files ... tot =2541331 bytes
Positive difference = 1514135, and negative difference = -171330 ... sum = 1342805

With $quality = 85;
Processed 89 jpg files, total 3884136 bytes ...
Processed 46 with thumbs, and 43 without thumbs ...
Total 415628 bytes in thumbs, and total 2264905 bytes in new files ... tot =2680533 bytes
Positive difference = 1374933, and negative difference = -171330 ... sum = 1203603

Quality set at 90 ...
Processed 89 jpg files, total 3884136 bytes ...
Processed 46 with thumbs, and 43 without thumbs ...
Total 415628 bytes in thumbs, and total 2519606 bytes in new files ... tot =2935234 bytes
Positive difference = 1120232, and negative difference = -171330 ... sum = 948902

Quality set at 95 ...
Processed 89 jpg files, total 3884136 bytes ...
Processed 46 with thumbs, and 43 without thumbs ...
Total 415628 bytes in thumbs, and total 3127299 bytes in new files ... tot =3542927 bytes
Positive difference = 519948, and negative difference = -178739 ... sum = 341209

This has a MINIMAL GAIN of 340KB of disk space, but even here it is possible
to see a tiny difference in the new image ... BUT it is HARD to tell the DIFFERENCE!
They are essentially the SAME ... if I was going to uploaded PAIRED images,
then this would be the quality level I would choose ...

Quality set at 100 ...
Processed 89 jpg files, total 3884136 bytes ...
Processed 46 with thumbs, and 43 without thumbs ...
Total 415628 bytes in thumbs, and total 4631281 bytes in new files ... tot =5046909 bytes
Positive difference = 0, and negative difference = -1162773 ... sum = -1162773

This represents a LOSS of disk space ... each of the 'new' files is LARGER,
thus a loss of 1.1MB of disk space, and even at this 100% quality level,
the new jpeg still has very slightly LESS clarity ... almost
imperceptable though ...

Decision: Especially since I have already uploaded ALL the images to the site,
then I will ONLY add the thumbnails to the site, and leave each of the original
images in place ... the choice for FUTURE images is still in the balance ...
The Quality of 95 seems about the BEST ...

*/


?>

index

Valid HTML 4.01 Transitional