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

时间:2018-07-31 14:10:15

标签: laravel validation image-uploading laravel-validation

我的Laravel应用上有这个上传控制器。 它允许上传大小小于2Mb的图像。问题是我有几个jpg文件,压缩后的大小为900KB,打开后的大小为69MB,尺寸为6016×4016px,分辨率为300dpi。

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

<?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 ]);

    }

1 个答案:

答案 0 :(得分:4)

使用dimensions validation rule

public function store(Request $request)
{
    $request->validate([
        'image' => 'dimensions:min_width=100,min_height=200',
    ]);
}