Laravel 和干预 - 如何禁止上传大图像

Posted

技术标签:

【中文标题】Laravel 和干预 - 如何禁止上传大图像【英文标题】:Laravel and Intervention - How to disallow upload of big images 【发布时间】:2019-01-07 23:16:08 【问题描述】:

我的 Laravel 应用上有这个上传控制器。 它允许上传

如何检测尺寸和分辨率并停止上传?

<?php namespace App\Http\Controllers;

use App\Http\Requests;
use App\Http\Controllers\Controller;

use Illuminate\Http\Request;

use App;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Input;
use Illuminate\Support\Facades\Response;
use Illuminate\Support\Facades\File;


class UploadController extends Controller 


public function store()
    

        $uploadedimage = array();

        $type      = Input::get('type');
        $table     = Input::get('table');
        $folder    = Input::get('folder');
        $extkey    = Input::get('extkey');
        $record    = Input::get('record');
        $tipo      = Input::get('tipo');
        $adduser   = Input::get('adduser');

        foreach (Input::file('image') as $image) 

            $oldname = $image->getClientOriginalName();
            $vowels = array("#", "@", "+", "à", "è", "é", "ì", "ò", "ù", ";", ",", "?", "*", '$', "!", "(", ")", "/", '"', "'", "%", "£", "<", ">", "&");
            $newname = str_replace($vowels, "", $oldname);

            $imagename = time()."-".$newname;
            $uploadflag = $image->move('repository/'.$folder, $imagename);

            if($uploadflag)
                $uploadedimage[] = $imagename;
            

        

        foreach ($uploadedimage as $value) 

            if($type == 1) 
                $tab = explode("|", $extkey);

                if($adduser == 1)
                DB::table($table)->insert(array(
                        $tab[1].'_id'      => $tab[0],
                        'active'           => 1,
                        'ordine'           => 100,
                        $record            => $value,
                        'user_id'          => Auth::id()
                    ));
                 else 
                    DB::table($table)->insert(array(
                        $tab[1].'_id'      => $tab[0],
                        'active'           => 1,
                        'ordine'           => 100,
                        $record            => $value
                    ));
                
             else 
                DB::table($table)->where('id', $extkey)->update(array($record => $value));
            

        

        return Response::json(['success' => 'true', 'images' => $uploadedimage ]);

    

【问题讨论】:

@MarcinMagdziarz Intervention 一个图像处理库。 image.intervention.io 【参考方案1】:

使用dimensions validation rule:

public function store(Request $request)

    $request->validate([
        'image' => 'dimensions:min_width=100,min_height=200',
    ]);

【讨论】:

还有size 验证规则。 好的,这通过将上传无效,有没有快速提醒用户的方法?

以上是关于Laravel 和干预 - 如何禁止上传大图像的主要内容,如果未能解决你的问题,请参考以下文章

使用图像干预的多图像上传。我尝试上传多张图片但无法上传,因为我不知道如何,任何帮助将不胜感激

干预图像 - 如何上传 MIME 类型的图像:application/octet-stream

上传大图像时网络连接中止后恢复或重新启动ajax请求?

在上传大图像时,蛋糕php中的内存分配问题

如何在 CloudKit 中存储大图像?

imagettfbbox():使用 Laravel 干预/图像进行文本布局时出现问题