Page 1 of 1
Maximum execution time exceeded
Posted: Mon May 08, 2006 11:44 am
by thinICE
Hallo, i made a class in PHP 4 with 2 function, one function calls the second one to create a thumbnail of a big image.
This is done for every image in a directory, so if there's more than 30 images the script goes out of time on the server. (default is 30 sec)
I resolved this using a set_time_limit(30) at start of the function that create thumbnails, but set_time_limit does not work if php is configured in safe mode.
Any suggestion to resolve this problem??
Thanks in advance.
Posted: Mon May 08, 2006 12:44 pm
by Christopher
Maybe process the images one at a time by using redirects to the same page or Ajax calls?
Posted: Mon May 08, 2006 12:45 pm
by Roja
1. Only do thumbnails for pictures that don't have them
2. Only do ten (or similarly small number) at a time
3. Setup a crontab to run that script every few minutes to ensure you never end up with more than ten without thumbnails (or very rarely)
Alternatively, post the code, and we can try to change it to run faster.
Posted: Mon May 08, 2006 1:01 pm
by infolock
if it's for a photo gallery, i'd reduce the number of images being shown, and maybe have a "Next Image List" link that goes to the next set of 10 images. That would be proper sequence since you then have the luxury of adding more images laster on.
also, how you are resizing the image? Are you using image magic or gd? Either way it should be fast. I'd also look at the format of the image you are using. hopefully you are using gif/jpg/png
last, if you want to change the execution speed to a higher limit, you can set these in the pho.ini and apache.conf files. however, doing so is bad practice because even though you are helping on this one small issue, you will in turn start having bad coding practices by writing code that doesn't meet the desired execution speed. No web-based script should take longer than 30 seconds. If it does, I'd recommend writing a console app that produces the output and then just generate it from a static flat file when needed.
Last, as Roja said, post your code. you could be using loops and crap where you really dont need to, or in turn you may be doing other incorrect coding practices that is resulting in timeouts.
Either way hope that helps out a little.
Posted: Tue May 09, 2006 6:43 am
by thinICE
I'm a flash developer so i'm still not so good with php coding practices.
In any case I need to use classes to have a good connection with flash.
The code creates thumbnails only if they don't exist yet or if they are old. But at first access obviously no thumbnails exist so is very common to have more than 30 thumbs to create. Nothing strange from my point of view, this is a work that must be dono in a way or another, so i don't understand time limitations.
Thanks for all the answers.
Code: Select all
class MGallery
{
var $cacheThumbnails = true;
var $thumbnailHeight;
var $thumbnailWidth;
function MGallery(){}
// Read all files and dir in a specified dir and create a multidimensional array
// with n element corresponding with files and dir, and every element have type, name and
// size subarray (formed by 4 element)
// N.B. $dir must end with a /
function getFilesInfos($dir, $thumbSize, $autoThumb)
{
$files = array();
$handle = opendir($dir);
$i = 0;
$dirs = array();
$images = array();
// set thumb size
$this->thumbnailWidth = $thumbSize['width'];
$this->thumbnailHeight = $thumbSize['height'];
// check thumbsDir
if ($this->cacheThumbnails)
{
if (file_exists($dir."/".$this->thumbDir))
{
if (!is_dir($dir."/".$this->thumbDir))
{
$this->cacheThumbnails = false;
}
}
else
{
if (!mkdir($dir."/".$this->thumbDir,0755))
{
$this->cacheThumbnails = false;
}
}
}
// return files array
while($file = readdir($handle))
{
if ($file != "." && $file != ".." && $file != ".mthumbs")
{
if (filetype($dir.$file) == "dir")
{
$dirs[$i] = $file;
}
else
{
$images[$i] = $file;
// check thumbFile
if($autoThumb)
{
$thumbFile = $dir.$this->thumbDir."/".$file;
if (file_exists($thumbFile))
{
if (filemtime($thumbFile)<=filemtime($dir.$file))
{
$this->createThumb($file, $dir);
}
}
else
{
$this->createThumb($file, $dir);
}
}
}
}
$i++;
}
sort($dirs);
sort($images);
$dirsBak = array();
$imagesBak = array();
for($i=0; $i < count($dirs); $i++)
{
$dirsBak[$i]["name"] = $dirs[$i];
$dirsBak[$i]["type"] = filetype($dir.$dirs[$i]);
}
for($i=0; $i < count($images); $i++)
{
$imagesBak[$i]["name"] = $images[$i];
$imagesBak[$i]["type"] = filetype($dir.$images[$i]);
$imagesBak[$i]["size"] = getimagesize("{$dir}/{$imagesBak[$i]["name"]}");
}
closedir($handle);
$files = array_merge($dirsBak,$imagesBak);
return $files;
}
function createThumb($fileP, $dirP)
{
set_time_limit(30);
$file = $fileP;
$dir = $dirP;
$thumbFile = $dir.$this->thumbDir."/".$file;
$extension = strtolower(substr(strrchr($file, "."), 1));
switch ($extension)
{
case "gif": $src = imagecreatefromgif($dir.$file); break;
case "jpg": // fall through
case "jpeg": $src = imagecreatefromjpeg($dir.$file); break;
case "png": $src = imagecreatefrompng($dir.$file); break;
default: return;
}
$srcWidth = imagesx($src);
$srcHeight = imagesy($src);
$srcAspectRatio = $srcWidth / $srcHeight;
if ($srcWidth > $srcHeight)
{
$thumbWidth = $this->thumbnailWidth;
$thumbHeight = $thumbWidth / $srcAspectRatio;
}
else
{
$thumbHeight = $this->thumbnailHeight;
$thumbWidth = $thumbHeight * $srcAspectRatio;
}
if (function_exists('imagecreatetruecolor'))
{
$thumb = imagecreatetruecolor($thumbWidth, $thumbHeight);
}
else
{
$thumb = imagecreate($thumbWidth, $thumbHeight);
}
imagecopyresampled($thumb, $src, 0, 0, 0, 0, $thumbWidth, $thumbHeight, $srcWidth, $srcHeight);
imagedestroy($src);
imagejpeg($thumb, $thumbFile);
imagedestroy($thumb);
chmod($thumbFile, 0755);
}
}
Posted: Thu May 11, 2006 5:22 am
by thinICE
up.