I'm stuck in a memory hole...
I've written quiite a bit of my very own gallery script (/me proud ...

)
but I've run into a litlle headache:
on my LAMP test server, the script runs fast, but I get a fatal errror on using to much memory from apache.
on my windows easy php test server, the script is so slow that after resizing six images, i get a timeout error.
argh.
so I am hoping that some guru might read this post and and bring me som good adviise..
o yes, the code, I nearly forgot that...
Code: Select all
function image_resize($src,$dest,$maxWidth,$maxHeight,$quality=70) {
if (file_exists($src) && isset($dest)) {
// path info
$destInfo = pathinfo($dest);
// image src size
$srcSize = getImageSize($src);
// image dest size $destSize[0] = width, $destSize[1] = height
$srcRatio = $srcSize[0]/$srcSize[1]; // width/height ratio
$destRatio = $maxWidth/$maxHeight;
if ($destRatio > $srcRatio) {
$destSize[1] = $maxHeight;
$destSize[0] = $maxHeight*$srcRatio;
}
else {
$destSize[0] = $maxWidth;
$destSize[1] = $maxWidth/$srcRatio;
}
// path rectification
if ($destInfo['extension'] == "gif") {
$dest = substr_replace($dest, 'jpg', -3);
}
// true color image, with anti-aliasing
$destImage = imageCreateTrueColor($destSize[0],$destSize[1]);
//imageAntiAlias($destImage,true);
// src image
switch ($srcSize[2]) {
case 1: //GIF
$srcImage = imageCreateFromGif($src);
break;
case 2: //JPEG
$srcImage = imageCreateFromJpeg($src);
break;
case 3: //PNG
$srcImage = imageCreateFromPng($src);
break;
default:
return false;
break;
}
// resampling
imagecopyresampled($destImage, $srcImage, 0, 0, 0, 0,$destSize[0],$destSize[1],$srcSize[0],$srcSize[1]);
// generating image
switch ($srcSize[2]) {
case 1:
case 2:
imageJpeg($destImage,$dest,$quality);
break;
case 3:
imagePng($destImage,$dest);
break;
}
return true;
}
else {
return false;
}
}
function create_thbnail_cache () {
//this function creates a cahce of thumbnails and resized versions of really
//large pictures for easier viewing
//variables from config file
global $table_prefix;
global $gallery_folder;
$query = "SELECT * FROM {$table_prefix}gallery";
$result = mysql_query($query) or die(mysql_error());
//now cycle trhroug all the entries in the gallery database table and create the needed files
while ($row = mysql_fetch_array($result)) {
$sub_dir = $gallery_folder . "/" . $row['dir'];
$thb_cache = $sub_dir . "/thbcache";
$reduced_cache = $sub_dir . "/redcache";
//check if the cache directories exists, if they don't, create them
print ("chceking for folder exist $thb_cache <br>");
if (!is_dir($thb_cache)) {
mkdir($thb_cache);
print ("created folder $thb_cache <br>");
}
print ("chceking for folder exist $reduced_cache <br>");
if (!is_dir($reduced_cache)) {
mkdir($reduced_cache);
print ("created folder $reduced_cache <br>");
}
//now check if a thumbnail picture has been cached from the current image
//if it is not, make one
print ("checking if file exists: $thb_cache/thbc_{$row['filename']}<br>");
if (!file_exists($thb_cache . "/thbc_" . $row['filename'])) {
print ("resizing (thb) {$row['filename']} <br>");
if (!image_resize($sub_dir . "/" . $row['filename'], $thb_cache . "/thbc_" . $row['filename'],
100,100,$quality=70))
{
print ("resize error");
}
}
//now check if a reduced size picture has been cached from the current image
//if it is not, make one
print ("checking if file exists: $reduced_cache/thbc_{$row['filename']}<br>");
if (!file_exists($reduced_cache . "/redc_" . $row['filename'])) {
print ("resizing (red) {$row['filename']} <br>");
if (!image_resize($sub_dir . "/" . $row['filename'], $reduced_cache . "/redc_" . $row['filename'],
800,600,$quality=70))
{
print ("resize error");
}
}
}
}
the create_thbnail_cache () function loops the image_resize function. And some where in there is my horrible mistake...
any feedback recieved with gratitude
-Olegu
oh, and by the way, perhaps this subject should be moved to php code forum now?