lararave干预图像上传慢代码审查

时间:2018-10-22 23:49:28

标签: laravel file-upload image-uploading intervention

我有Upload File Controller方法,在实时服务器上运行缓慢,我不知道为什么?您可以查看我的代码是否结构正确吗?顺序是否正确?如何优化我的代码?

    $id = Auth::user()->id;
    $image=$request->file('file');
    $fileName=time().uniqid(rand()).'.'.$image->getClientOriginalExtension();

    //Resize image here    
    $background = Image::canvas(500, 500);  
    $background->fill('#fff');  

    $lphoto = Image::make($image->getRealpath());
    $lphoto->orientate();
    $lphoto->resize(500, 500, function ($constraint) {
        $constraint->upsize();
        $constraint->aspectRatio();
    });
    $lphoto->encode();

    $sphoto = Image::make($image->getRealpath());
    $sphoto->orientate();
    $sphoto->fit(186,180);
    $sphoto->encode(null, 90);

    $background->insert($lphoto, 'center');
    $background->encode();

    Storage::disk('public')->put( 'images/'.$year.'/'.$month."/".$day."/".$id."/".$fileName, $background);
    Storage::disk('public')->put( 'images/'.$year.'/'.$month."/".$day."/".$id.'/small-'.$fileName, $sphoto);

0 个答案:

没有答案