Q:

Backpack CRUD: How do I validate dimensions of an image uploaded with elFinder?

I'm having trouble validating image dimensions in Backpack for laravel.
In my Backpack CRUD, I am adding a field for a photo upload using elFinder like so:
$addFields = [
    [
        'name' => 'photo',
        'label' => 'Header Photo (1500 x 1000 EXACTLY)',
        'type' => 'browse'
    ]
]

In my request I am using the following rule to validate the size of the image:
public function rules()
{

    return [
        'photo' => 'dimensions:width=1500,height=1000'
    ];
}

No matter what dimension attributes I try the validation fails. I have tried things like max_width way above the dimensions of the test case file.
My theory is that the validator is looking for an actual 'file' field to be passed in the post, but elFinder is passing the path of a file that was uploaded.
Any ideas on how to handle image dimension validation in Laravel using the path?

A:

I resolved this by adding a custom validator method and PHP's native getimagesize function to validate using the path supplied by the rule.
Create a custom validation type
In app/Providers/AppServiceProvider.php:
Include the Validator Class:
use Illuminate\Support\Facades\Validator;
Inside of the boot() function in AppServiceProvider.php add:
    Validator::extend('header_image', function($attribute, $value, $parameters, $validator) {

        $path = public_path();
        $photo = "$path/$value";
        list($width,$height) = getimagesize($photo);
        // $parameters[0] = width
        // $parameters[1] = height
        if ($width != $parameters[0] || $height != $parameters[1]) {
            return false;
        }else{
            return true;
        }

    });

Now I can use header_image as a validation type and pass in the width/height contraints as parameters:
'photo' => 'header_image:1500,1000',

