genimgs01.php to HTML

index

USE AT OWN RISK

Generated: Tue Jul 31 15:22:00 2007 from genimgs01.php 2007/06/23 14.6 KB bytes.

<?php

/* genimags01.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 ... using the width and height max
 specified, keeping ratio correct
 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
 3. Allow name change in-to-out
 Geoff McLane - 6 February, 2006
 also see - imginfo2.php and mkthumb4.php
 */

// ############################################
$timestart = getmicrotime(); // get time array secs and usecs ...
//$maindir = 'C:/HOMEPAGE/Max5/JennyHaleIllustration/Images/6';
//$maindir = 'C:\Documents and Settings\Geoff McLane.PRO-1\My Documents\Fred\images';
$maindir = 'C:\Documents and Settings\Geoff McLane\My Documents\My Pictures\Nikon\20070622';
$path_out = 'temp3';
$meol = "\r\n";
$quality = 95; // was 75, default IJG
// RE-SIZE each image to this SIZE
$max_width = 464;
$max_height = 459;
// variables
$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;
$dir_arr = array();
$cdir = '';
$arr_list = '';

if ( ! is_dir( $path_out ) ) {
   echo "ERROR: Path out of $path_out does NOT exist ... aborting ... $meol";
   exit(4);
}
$dir_arr[] = $maindir;

foreach ($dir_arr as $dir) {
   ProcessDir($dir);
}
echo "<p>Got ".count($dir_arr)." directories, from the search ...<br>$meol";
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";
print "$arr_list <br>$meol";
$timeend = getmicrotime();
$time = $timeend - $timestart;
echo "Script ran for $time seconds ...<br>$meol";
// ############################################

// ###########################################
// Functions used below
// ###########################################
// only KEEP alpha-numeric values
function get_ascii_only($file)
{
   $chrs = array('0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S' ,'T', 'U', 'V', 'W', 'X', 'Y', 'Z');
   $imx = strlen($file);
   for($i = 0; $i < $imx; $i++) {
   if( ! in_array($file[$i], $chrs) ) {
   $file[$i] = '_';
   }
   }
   return $file;
}

// ProcessFile( string input_file, string output_path, string out_name )
function ProcessFile( $in_file, $out_path, $out_file ) {
   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;
   global $max_width, $max_height;
   global $arr_list;
   if ( !file_exists( $in_file ) ) {
   echo "ERROR: Can NOT locate file '$in_file' ... aborting ...$meol";
   exit(1);
   }
   echo "<p>Got IN file $in_file, OUT-PATH $out_path ...<br>$meol";
   $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 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);
   $parts = split("\.", $out_file);
   $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 = "";
   }
   $file_tit = get_ascii_only($file_tit);
   // 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");
   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;
   $n_name = $file_tit . '-n.' . $file_ext;
   $new_name = $out_path . '/' . $n_name;
   $ratio = $imgSx/$imgSy;
   $new_ix = $imgSx;
   $new_iy = $imgSy;
   echo " Size 1 $new_ix X $new_iy";
   if ( ($imgSx > $max_width)||($imgSy > $max_height) ) {
   $new_ix = $max_width;
   $new_iy = $max_height;
   if($imgSx > $max_width) {
   if($ratio > 1) {
   $new_ix = $max_width;
   $new_iy = round($max_width / $ratio);
   } else {
   $new_ix = round($max_width * $ratio);
   $new_iy = $max_width;
   }
   echo " Size 2 $new_ix X $new_iy";
   }
   if($new_iy > $max_height) {
   if($ratio > 1) {
   $new_ix = round($max_height) / $ration;
   $new_iy = $max_height;
   } else {
   $new_ix = round($max_height * $ratio);
   $new_iy = $max_height;
   }
   echo " Size 3 $new_ix X $new_iy";
   }
   }
   echo "<br>$meol";
   print " Ratio is $ratio ... New sizes $new_ix x $new_iy ... ";
   print " ". ($new_ix / $new_iy) . "<br>$meol";
   // see if there is ALREADY a thumbnail image embedded ...
   $thumb_data = exif_thumbnail($in_file, $in_width, $in_height, $in_type);
   $th_size = 0;
   if ( $thumb_data ) { // PROCESS THUMB DATA
   // separate the FILE into two - thumb and full
   $with_thumb++;
   echo "Got thumbnail image $in_width X $in_height ($in_type) ...<br>$meol";
   } else {
   $without_th++;
   echo "Note: No thumbnail image embedded in this JPEG file ...<br>$meol";
   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_ix,$new_iy);
   if( !$dst_img ) {
   echo "ERROR: Failed to create new image ...<br>$meol";
   return;
   }
   if(($new_ix != $imgSx) || ($new_iy != $imgSy)) {
   // new size(s)
   if( !ImageCopyResampled($dst_img,$src_img,0,0,0,0,
   $new_ix,$new_iy,$imgSx,$imgSy) ) {
   echo "ERROR: Failed to copy sample ...<br>$meol";
   return;
   }
   } else {
   // same size
   if( !imagecopy($dst_img,$src_img,0,0,0,0,$new_ix,$new_iy) ) {
   echo "ERROR: Failed to copy ...<br>$meol";
   return;
   }
   }
   if( imagejpeg( $dst_img, $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='$new_name'></td>$meol";
   print "<td><img src='$in_file'></td>$meol";
   print "</tr></table>$meol";
   $arr_list .= '"'.$n_name.'", "'. $new_ix . 'x' . $new_iy . '"' . $meol;
   } else {
   echo "WARNING: Failed to write new JPEG 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;
   }

/* ==================================
   // see if there is ALREADY a thumbnail image embedded ...
   $thumb_data = exif_thumbnail($in_file, $in_width, $in_height, $in_type);
   if ( $thumb_data ) { // PROCESS THUMB DATA
   // separate the FILE into two - thumb and full
   $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 using quality $quality ...<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";
   if ( ($imgSx > $max_width)||($imgSy > $max_height) ) {
   $new_ix = $max_width;
   $new_iy = $max_height;
   if($imgSx > $max_width) {
   if($ratio > 1) {
   $new_ix = $max_width;
   $new_iy = round($max_width / $ratio);
   } else {
   $new_ix = round($max_width * $ratio);
   $new_iy = $max_width;
   }
   }
   if($new_iy > $max_height) {
   if($ratio > 1) {
   $new_ix = round($max_height) / $ration;
   $new_iy = $max_height;
   } else {
   $new_ix = round($max_height * $ratio);
   $new_iy = $max_height;
   }
   }
   print " Ratio is $ratio ... New sizes $new_ix x $new_iy ...<br>$meol";
   $dst_img2 = imagecreatetruecolor($new_ix,$new_iy);
   if ($dst_img2) {
   if ( ImageCopyResampled($dst_img2,$src_img,0,0,0,0,$new_ix,$new_iy,$imgSx,$imgSy) ) {
   if ( imageJpeg($dst_img2,$new_name) ) {
   $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='$new_name'></td>$meol";
   print "<td><img src='$in_file'></td>$meol";
   print "</tr></table>$meol";
   } else {
   echo "WARNING: 1.Resizing of $in_file FAILED!<br>$meol";
   }
   } else {
   echo "WARNING: 2.Resizing of $in_file FAILED!<br>$meol";
   }
   } else {
   echo "WARNING: 3.Resizing of $in_file FAILED!<br>$meol";
   }
   } else {
   // NO RE-SIZING
   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='$new_name'></td>$meol";
   print "<td><img src='$in_file'></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;
   }
   ============ */
}

function ProcessDir($theDir)
{
   global $meol;
   global $path_out, $file_count;
   global $dir_arr;
   $mydir = opendir($theDir) ;
   if( !$mydir ) {
   echo "ERROR: Can not OPEN directory [$theDir] ... aborting ... $meol" ;
   exit(3) ;
   }

   // while($fn = readdir($mydir)) //scan through the whole directory
   while(false !== ($fn = readdir($mydir))) //scan through the whole directory
   {
   if( !(($fn == '.') || ($fn == '..')) ) {
   $inp_file = $theDir . '/' . $fn;
   if( is_dir( $inp_file ) ) {
   $dir_arr[] = $inp_file;
   ProcessDir($inp_file);
   } else if( IsJPEGFile( $inp_file ) ) {
   $out_file = strtolower($fn);
   $file_count++;
   //echo "<p>From in name $fn, got out name ";
   //echo get_ascii_only($out_file);
   //echo "...<br>$meol";
   ProcessFile($inp_file, $path_out, $out_file);
   }
   }
   }

   closedir($mydir);
}


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 getmicrotime() {
 $temparray=split(" ",microtime());
 $returntime=$temparray[0]+$temparray[1];
 return $returntime;
} 

?>

index

Valid HTML 4.01 Transitional