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_thumbnai l($infile,$outf ile,$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($infi le)) {
return FALSE;
}
$functions = array(
'image/png' ='ImageCreateFr omPng',
'image/jpeg' ='ImageCreateFr omJpeg',
);
if (function_exist s('ImageCreateF romGif')) { $functions['image/gif']
= 'ImageCreateFro mGif'; }
$size = getimagesize($i nfile);
if (!$function = $functions[$size['mime']]) {
return FALSE;
}
if (!$source_img = @$function($inf ile)) {
return FALSE;
}
$save_function = "image" .
strtolower(subs tr(strrchr($siz e['mime'],'/'),1));
// Scale dimensions
list($neww,$new h) = scale_dimension s($size[0],$size[1],$maxw,$maxh,
$stretch);
// Create new image
$new_img = imagecreatetrue color($neww,$ne wh);
// Copy and resize image
imagecopyresize d($new_img,$sou rce_img,0,0,0,0 ,$neww,$newh,$s ize[0],
$size[1]);
// Save output file
if ($save_function == 'imagejpeg') {
if (!$save_functio n($new_img,$out file,60)) {
return FALSE;
}
} else {
if (!$save_functio n($new_img,$out file)) {
//trigger_error(" Unable to save output
image",E_USER_W ARNING);
return FALSE;
}
}
// Cleanup
imagedestroy($s ource_img);
imagedestroy($n ew_img);
return TRUE;
}
}
// Scales dimensions
function scale_dimension s($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,$ne wh);
}
}
?>
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_thumbnai l($infile,$outf ile,$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($infi le)) {
return FALSE;
}
$functions = array(
'image/png' ='ImageCreateFr omPng',
'image/jpeg' ='ImageCreateFr omJpeg',
);
if (function_exist s('ImageCreateF romGif')) { $functions['image/gif']
= 'ImageCreateFro mGif'; }
$size = getimagesize($i nfile);
if (!$function = $functions[$size['mime']]) {
return FALSE;
}
if (!$source_img = @$function($inf ile)) {
return FALSE;
}
$save_function = "image" .
strtolower(subs tr(strrchr($siz e['mime'],'/'),1));
// Scale dimensions
list($neww,$new h) = scale_dimension s($size[0],$size[1],$maxw,$maxh,
$stretch);
// Create new image
$new_img = imagecreatetrue color($neww,$ne wh);
// Copy and resize image
imagecopyresize d($new_img,$sou rce_img,0,0,0,0 ,$neww,$newh,$s ize[0],
$size[1]);
// Save output file
if ($save_function == 'imagejpeg') {
if (!$save_functio n($new_img,$out file,60)) {
return FALSE;
}
} else {
if (!$save_functio n($new_img,$out file)) {
//trigger_error(" Unable to save output
image",E_USER_W ARNING);
return FALSE;
}
}
// Cleanup
imagedestroy($s ource_img);
imagedestroy($n ew_img);
return TRUE;
}
}
// Scales dimensions
function scale_dimension s($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,$ne wh);
}
}
?>
Comment