douji9518 2013-07-02 18:37
浏览 29

PHP脚本突然停止(imagecopyresampled)

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;\">&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条回答 默认 最新

  • duanjue2576 2013-07-02 18:43
    关注

    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.

    评论

报告相同问题?

悬赏问题

  • ¥15 mmocr的训练错误,结果全为0
  • ¥15 python的qt5界面
  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀