Laravel 干预/图像 超过 30 秒的最大执行时间

Laravel Intervention/ Image Maximum execution time of 30 seconds exceeded

我在 Laravel 有一个摄影网站。我使用一个名为 Intervention / Image 的包。在站点管理区域中,管理员可以创建项目,然后将任意数量的图像上传到该项目。问题是,如果图像相当大 4mb 或更大,并且一次上传 10 张或更多,我会收到此错误:

Maximum execution time of 30 seconds exceeded

我觉得脚本或库占用了太多内存,但我不确定哪里出了问题。我知道我可以提高内存和时间限制,但我觉得我不需要这样做。

我还尝试了 100kb 或更小的图像,任何数量超过 20 的图像都会终止进程并且只完成前 20 张图像。我检查了服务器,图像正在正确上传到正确的位置,但它没有写入数据库。

我不确定是上传问题还是查询问题导致的。下面是上传图片的控制器。作为旁注,脚本还为每个图像创建了四种文件大小。

<?php

class UploadController extends BaseController {

    private $sizes = array( 'large' => 2000, 'medium' => 1500, 'small' => 1000, 'thumb' => 300 );

    public function upload( $id ) {

        $movedArray = array();
        $errors = 0;

        $images = Input::file('images');

        foreach( $images as $img ) {

            $oName = $img->getClientOriginalName();
            $oMimeType = $img->getMimeType();
            $oSize = $img->getSize();
            $oExt = $img->getClientOriginalExtension();
            $oTemp = $img->getRealPath();

            // get original file name
            $name = str_replace( array(' '), array('-'), $oName );

            $orig = $oTemp;

            $image_original = Image::make( $img );

            foreach( $this -> sizes as $sizename => $size ) {

                $movedPath = public_path() .'/projects/';
                $movedName = strtotime('now') .'-'. $id .'-'. $sizename .'-'. $name;

                $moved = $image_original -> resize( $size, null, function( $constraint ) {

                    $constraint -> aspectRatio();

                });

                $resizedMoved = $moved -> save( $movedPath . $movedName );

                $movedArray[$sizename] = $movedName;

            }

            // create the record in the database
            $upload = new Upload;

            $upload -> user_id = Auth::id();
            $upload -> project_id = $id;

            $upload -> thumbnail = $movedArray['thumb'];
            $upload -> small = $movedArray['small'];
            $upload -> medium = $movedArray['medium'];
            $upload -> large = $movedArray['large'];

            $upload -> file_type = $oMimeType;
            $upload -> file_size = $oSize;
            $upload -> file_extension = $oExt;

            $upload -> save();

        }

        if( $errors > 0 ) {
            Session::put('alert-class', 'success');
            Session::put('msg', 'All files have been uploaded');
        } else {
            Session::put('alert-class', 'danger');
            Session::put('msg', 'Uh oh, something went wrong please try again');
        }

        return Redirect::back();

    }
}

提高内存和时间限制是我唯一的选择吗?如果是这样,我觉得那是糟糕的编程,因为我已经将其设置为:

ini_set("memory_limit","1000M");
set_time_limit(1000);

我仍然得到同样的错误,任何帮助将不胜感激! 谢谢

改变

max_file_uploads

在你的 php.ini :)

您遇到的错误与内存限制无关,而是与时间限制有关,并且由于对图像进行了多次操作而导致出现此问题,并且自行上传需要一些时间。 请说明您在哪里设置 set_time_limit(1000) 部分。

您还可以提高一些性能,而不是让每个图像的插入查询将数据存储在数组中,并在完成所有上传后将它们全部放在 1 个插入查询中。