用laravel上传照片的最佳,最快的方法是什么

时间:2018-07-02 18:48:23

标签: laravel upload photos

我正在用Laravel上传带有此代码的照片,但是当文件超过1mb时,它的速度非常慢。我要同时上传很多照片,因此,每张照片超过一个或更多(直到有时只有小文件)时,上传速度非常慢。

我的问题是:同时上传照片的更快捷的方法是什么?

public function store(Request $request)
{   
    $inputs = $request->all();
    $data = \Input::except(array('_token'));       

        $rule = array();
        $rule['property_id'] = 'required';
        $rule['photos'] = 'required';

        if(isset($inputs['photos'])){
            $photos = count($inputs['photos']);
            if($photos){
                foreach(range(0, $photos) as $index) {
                    $rule['photos.' . $index] = 'image|mimes:jpeg,bmp,png';
                }
            }
        }
        $validator = \Validator::make($data,$rule);

        if ($validator->fails())
        {
            return [
                'status'=>false,
                'messages'=>$validator->messages(),
            ];
        } 

        $name = $this->property->find($inputs['property_id'])->name;

        foreach ($inputs['photos'] as $photo) { 

            $hardPath =  str_slug($name, '-').'-'.md5(rand(0,99999));            

            if($photo && $inputs['property_id']){  

                $save = [
                    'property_id'=> $inputs['property_id'],
                    'image'      => $hardPath
                ];

                $upload = $this->repository->create($save); 

                if($upload){                

                    \File::delete(public_path() .$this->tmpFilePath.$photo.'-b.jpg');

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

                    $img->fit(640, 425);
                    $img->save($this->tmpFilePath.$hardPath.'-b.jpg');

                    if(!$img){
                        $this->repository->delete($upload->id);
                    }
                }                
            }             

        }
        return [
            'status'=>true,
            'message'=>'Saved with success!',
        ];               
} 

感谢前进!

0 个答案:

没有答案