Ad

Laravel And Intervention - How To Disallow Upload Of Big Images

I have this upload controller on my Laravel app. It allows to upload image of <2Mb of size. The problem is that I have several jpg files that are 900KB when compressed and 69MB once opened, with a dimension of 6016×4016px and a resolution of 300dpi.

How can I detect dimensions and resolution and stop the upload before?

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

    }
Ad

Answer

Use dimensions validation rule:

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