【问题标题】:PHP Script stopping abruptly (imagecopyresampled)PHP 脚本突然停止 (imagecopyresampled)
【发布时间】:2013-06-30 06:35:41
【问题描述】:

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

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

我猜这是某种类型的内存问题?在记忆方面,我完全没有经验和一无所知。

编辑: 我在 imagecopyresampled 函数之前和之后放置了一个回声。脚本似乎停止在这个函数上。

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

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;

      }

    }

【问题讨论】:

  • 我可能是错的,但我不确定false !== ($file = readdir($handle)) 是正确的语法。 != 是“不相等”的标准。如果你想发短信,如果它“完全不相等”,我猜你想要!(($file = readdir($handle)) === false)

标签: php image memory


【解决方案1】:

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

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

另外,在使用imagejpeg 之后,使用imagedestroy($image_p) 来释放内存。

【讨论】:

  • 没有错误信息,这两种我都试过了,我的循环仍然突然结束。
  • 好吧,既然您没有检查您要处理的文件类型,那么您很可能会遇到非 JPEG 文件...尝试启用 error_reporting 来查看。
  • 错误报告已启用,我已将非 JPEG 文件放入其中,但确实出现错误。
  • 好吧......那是你的问题......你应该在is_file之后检查它是否是一张图片。
  • 对不起,我的意思是我在里面放了非 jpeg 来测试是否有错误。目前没有错误,因为那里只有 jpeg。如果我继续运行脚本,最终所有图像都会被转换。
【解决方案2】:

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

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

echo "Trying {$filename}";

if (is_file($filename)) {

    //do the processing

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

然后您至少会看到它尝试了哪些文件以及它何时停止工作。看看有没有规律?

【讨论】:

  • 问题是,如果我继续手动重新启动脚本,所有图像都会得到处理。它不会在特定图像上停止,而是在 X 个图像后停止。 (X 根据图像大小而变化)
【解决方案3】:

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多