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


Laravel Intervention/ Image Maximum execution time of 30 seconds exceeded

我在拉拉维尔有一个摄影网站。我使用一个名为干预/图像的包。在站点管理区域中,管理员可以创建项目,然后将任意数量的图像上传到该项目。问题是,如果图像的大小为 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);

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

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

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

更改

max_file_uploads

在你的 PHP 中.ini :)