PHP脚本突然停止(imagecopyresampled)

时间:2013-07-02 18:37:51

标签: php image memory

我有下面这个脚本在经过一定量的迭代后停止而没有错误。当它使用的图像大约为4MB时,脚本在大约10次迭代后停止。当图像大约为1MB时,脚本在~30次迭代后停止。当图像较小时,脚本可以进行500次迭代。它似乎不是超时,因为它每次都在不同的时间点发生。

当我使用xxamp在我自己的机器上测试时,没有错误,并且脚本完美无缺。

我猜这是某种类型的内存问题?在谈到记忆时,我完全缺乏经验和无能为力。

编辑: 我在imagecopyresampled函数之前和之后放了一个echo。该脚本似乎停止在此功能上。

这是我的循环遍历目录中的每个文件:

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;\">&nbsp;</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;

      }

    }

3 个答案:

答案 0 :(得分:0)

set_time_limit(20);的值提高到更高的超时值...

提供错误消息可能会提供更多见解。

此外,使用imagejpeg后,请使用imagedestroy($image_p)释放内存。

答案 1 :(得分:0)

也许尝试添加一些自己的错误记录。

$filename = $srcDir . '/' . $filer;

echo "Trying {$filename}";

if (is_file($filename)) {

    //do the processing

} else {
    echo "{$filename} was not a file";
}

然后,您至少会看到它尝试的文件以及何时停止工作。看看是否有模式?

答案 2 :(得分:0)

我遇到了这个问题,解决方法是暂时增加内存限制脚本:

ini_set ("memory_limit", "100M");