1
votes

Iam new in laravel .im trying to validate dimensions of image .i want dimensions minimum(width=100,height=50).iam using validation code in controller.php is here 'galimg'=>'required|max:200kb|DimensionMin(300,300)|Mimes:jpeg,jpg,gif,png ,pneg'
but DimensionMin(300,300) is not work....i think custom validation rule is possible ..but i dont know how to use it ?and where ? this is my controller.php code

public function getgallery()
  {
    $validate=Validator::make(Input::all(),array(
    'galname'=>'required|max:20',
     'galimg'=>'required|max:400kb|Dimensionmin(300,300)|Mimes:jpeg,jpg,gif,png
                     ,pneg'));
    if($validate->fails())
     {  return Redirect::route('getgallery')
        ->withErrors($validate)->withInput(); }
    else
    { $max_image = 3;
    if(ForumGallery::all()->count() < $max_image)
     {  $file=Input::file('galimg');
        $filename=$file->getClientOriginalName();
     $file->move('uploads',$filename);
     ForumGallery::create(['galname'=>Input::get('galname'),
           'galimg'=>$filename]);
     return Redirect::route('addgallery'); 
      }
    else
     {return Redirect::route('gallery')
      ->with('success','Max Image Upload Reached!');
     } }}
3

3 Answers

0
votes

you can use this code

function getImgSize(imgSrc) { var newImg = new Image();

newImg.onload = function() {
var height = newImg.height;
var width = newImg.width;
alert ('The image size is '+width+'*'+height);
}

newImg.src = imgSrc; // this must be done AFTER setting onload

}`

Here you get Image height and width compare it with your required dimention.

refer http://tiku.io/questions/4060613/how-to-validate-image-size-and-dimensions-before-saving-image-in-database

0
votes
$v = Validator::make($data, array(
    'email' => 'required|email',
    'games' => 'required|numeric',
));

Assume you have some other fields to validate as well. Put them in $v. Now you need to add custom validation rules.

$v->sometimes('galimg', 'required', function($input)
{
   //psudo code, here validate your image
   if($input.length >= 300) return false;
   return true;
});

Put image-related manipulation into the function.

Hope this helps.

$validate=Validator::make(Input::all(),array(
'galname'=>'required|max:20',
 'galimg'=>'required|max:400kb|Mimes:jpeg,jpg,gif,png
                 ,pneg'));

$validate->sometimes('galimg', 'required', function($input)
{
   //psudo code, here validate your image
   return imagesx($input) > 300 && imagesy($input) > 300;
});
0
votes

You can use this awesome library for detecting your image dimension here

afte done installing you can use it in your controller like this one :

$validate=Validator::make(Input::all(),array(
  'galname'=>'required|max:20',
  'galimg'=>'required|mimes:jpeg,jpg,gif,png,pneg|image_size:1200,800'));

the rules should be 1200 wide and 800 tall or width = 1200 and height = 800

Note : the dimension is in pixels hope it helps.