|  | Posted by flamer die.spam@hotmail.com on 01/23/08 08:07 
Hi all, I have a new website im setting up, its a large image archive,I have it setup to open a folder, go through and for each file in that
 folder check a /tn sub-folder for a matching name, if the name doesnt
 exist, it means a new image has been added and that a thumbnail should
 be made, when thumbnails are made they are stored rather than deleted
 (like most gallery software does) this check happens every time a
 folder is opened. the problem is performance, this site will have
 around 25k images in each folder, on my testing with 800 images in a
 folder the scirpt times out 3 times before all the thumbs are made.
 the server is fairly decent but it shoots up to 100% cpu usage when
 the script is executed.. this is the script i have for making the
 thumbs: (i should mentioned for the folders where all thumbs already
 exist its very fast so the issue is in this script somewhere)
 
 * i tried turning the quality down from 60 to 6, it still took 40
 seconds to generate 137 images totalling 87mb for the fullsize and
 165kb for 137 thumbs
 
 <?php
 
 function create_thumbnail($infile,$outfile,$maxw,$maxh,$stretch) {
 
 if (!preg_match('%\\A/webdir/galleries/[\\wa-z]/[\\wa-z0-9]*/[\\wa-
 z0-9]*\\.jpg\\z%i', $infile))
 {
 
 die("died");
 // if we keep file names sane, then nothing should ever happen here
 }
 
 else {
 
 
 clearstatcache();
 if (!is_file($infile)) {
 
 return FALSE;
 }
 
 
 $functions = array(
 'image/png' => 'ImageCreateFromPng',
 'image/jpeg' => 'ImageCreateFromJpeg',
 );
 
 
 if (function_exists('ImageCreateFromGif')) { $functions['image/gif']
 = 'ImageCreateFromGif'; }
 
 $size = getimagesize($infile);
 
 
 if (!$function = $functions[$size['mime']]) {
 
 return FALSE;
 }
 
 
 if (!$source_img = @$function($infile)) {
 
 return FALSE;
 }
 
 $save_function = "image" .
 strtolower(substr(strrchr($size['mime'],'/'),1));
 
 // Scale dimensions
 list($neww,$newh) = scale_dimensions($size[0],$size[1],$maxw,$maxh,
 $stretch);
 
 // Create new image
 $new_img = imagecreatetruecolor($neww,$newh);
 
 // Copy and resize image
 imagecopyresized($new_img,$source_img,0,0,0,0,$neww,$newh,$size[0],
 $size[1]);
 
 // Save output file
 if ($save_function == 'imagejpeg') {
 
 if (!$save_function($new_img,$outfile,60)) {
 
 return FALSE;
 }
 } else {
 if (!$save_function($new_img,$outfile)) {
 //trigger_error("Unable to save output
 image",E_USER_WARNING);
 return FALSE;
 }
 }
 
 // Cleanup
 imagedestroy($source_img);
 imagedestroy($new_img);
 
 return TRUE;
 }
 }
 
 // Scales dimensions
 function scale_dimensions($w,$h,$maxw,$maxh,$stretch) {
 if ((!$stretch) && (($w < $maxw) || (!$maxw)) &&
 (($h < $maxh) || (!$maxh))) return array($w,$h);
 
 // Scale Height
 if ((!$maxw) || (($h > $w) && ($maxh)) ) {
 $newh = $maxh;
 $neww = floor($w * $newh /$h);
 }
 // Scale width
 elseif ((!$maxh) || (($w >= $h) && ($maxw))) {
 $neww = $maxw;
 $newh = floor($h * $neww / $w);
 } else
 // Scale neither
 return array($w,$h);
 
 return array($neww,$newh);
 }
 
 
 
 }
 
 
 
 
 
 ?>
 [Back to original message] |