mkthumb.php to HTML

index

USE AT OWN RISK

Generated: Tue Jul 31 15:22:09 2007 from mkthumb.php 2005/11/07 7.8 KB bytes.

<?php

/* mkthumb.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
 Geoff McLane - 7 November, 2005
*/

$timestart = getmicrotime(); // get time array secs and usecs ...

$meol = "\r\n";
$inp_file = "images/oz_aug61.jpg";
//$inp_file = 'test.jpg';
$maindir = "c:/HOMEPAGE/Max5/images" ; //change this to what ever directory needs scanning
$mydir = opendir($maindir) ;
if( !$mydir ) {
   echo "ERROR: Can not OPEN directory [$maindir] ... aborting ... $meol";
   exit(3);
}
// $path_out = 'images';
//$path_out = 'thumbs';
$path_out = 'temp';
//$inp_file = "/homepage/russ8/images/page00/DSC00001.JPG";
while($fn = readdir($mydir)) //scan through the whole directory
{
   if( !(($fn == '.') || ($fn == '..')) ) {
   $inp_file = $maindir . '/' . $fn;
   if( IsJPEGFile( $inp_file ) ) {
   ProcessFile($inp_file, $path_out);
   }
   }
}

$timeend = getmicrotime();
$time = $timeend - $timestart;
echo "Script ran for $time seconds ...<br>$meol";

// ProcessFile( string input_file, string output path )
function ProcessFile( $in_file, $out_path ) {
   global $meol;
   if ( !file_exists( $in_file ) ) {
   echo "ERROR: Can NOT locate file '$in_file' ... aborting ...$meol";
   exit(1);
   }

   $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";
   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;
   // see if there is ALREADY a thumbnail image embedded ...
   $thumb_data = exif_thumbnail($in_file, $in_width, $in_height, $in_type);
   if ( $thumb_data ) {
   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) ) {
   $new_name = $out_path . '/' . $file_tit . '-n.' . $file_ext;
   if( imagejpeg( $new_jpeg, $new_name, 75 ) ) {
   $new_size = filesize($new_name);
   echo "New image written to $new_name ... New size = $new_size bytes ...<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 {
   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") ) {
   $new_size = filesize($thumb_name);
   print " Thumbnail IMAGE written to disk, as '$thumb_name' ... size = $new_size<br>$meol";
   print "<img src='$thumb_name'><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";
   }
   }
}

// 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 ) {
   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;
} 


?>

index

Valid HTML 4.01 Transitional