I have this script below that stops without an error after a certain amount of iterations. When the images it is using are ~4MB, the script stops after about 10 iterations. When the images are around 1MB, the script stops after ~30 iterations. When the images are smaller, the script can go for 500 iterations. It doesn't seem to be a timeout, as it happens at a different time count each time.
When I test this on my own machine using xxamp, there are no errors, and the script finishes perfectly.
I am guessing this is some type of memory problem? I am completely inexperienced and clueless when it comes to memory.
EDIT: I put an echo before the imagecopyresampled function and after. The script seems to stop on this function.
Here is my loop that goes through each file in the directory:
while (false !== ($filer = readdir($handle))) {
if (is_file($srcDir . '/' . $filer)) {
set_time_limit(20);
$counter++;
$total = $x;
$percent = intval($counter/$total * 100)."%";
// Javascript for updating the progress bar and information
echo '<script language="javascript">
document.getElementById("progress").innerHTML="<div style=\"width:'.$percent.';background-color:#333;\"> </div>";
document.getElementById("information").innerHTML="'.$counter.' images processed.";
</script>';
//location/filename variable
$filename = $srcDir . '/' . $filer;
// This is for the buffer achieve the minimum size in order to flush data
echo str_repeat(' ',1024*64);
// Send output to browser immediately
flush();
//get the extension of the image
$path_parts = pathinfo("$filename");
$ext = strtolower ($path_parts['extension']);
$width = 100;
$height = 100;
// Get new dimensions
list($width_orig, $height_orig) = getimagesize($filename);
$ratio_orig = $width_orig/$height_orig;
if ($width/$height > $ratio_orig) {
$width = $height*$ratio_orig;
} else {
$height = $width/$ratio_orig;
}
// Resample
$image_p = imagecreatetruecolor($width, $height);
$image = imagecreatefromjpeg($filename);
imagecopyresampled($image_p, $image, 0, 0, 0, 0, $width, $height, $width_orig, $height_orig);
// Output the small image
imagejpeg($image_p, "$destDir/$filer", 100);
//Move the big image
rename($srcDir . '/' . $filer, $destDirbig . '/' . $filer);
echo $counter;
}
}
Raise the value of set_time_limit(20);
to a higher timeout value...
Providing the error message may give more insight.
Also, after using imagejpeg
, use imagedestroy($image_p)
to free up memory.
Maybe try adding some of your own error logging.
$filename = $srcDir . '/' . $filer;
echo "Trying {$filename}";
if (is_file($filename)) {
//do the processing
} else {
echo "{$filename} was not a file";
}
Then you will at least see which files it tries and when it stops working. See if there is a pattern?
I had that problem, the solution is to temporarily increase the memory limit script:
ini_set ("memory_limit", "100M");