如何在上传之前检查图像尺寸 在 Laravel 4.


how to check image dimensions before upload in laravel 4

我是 laravel 4 的新手。

我想上传最大尺寸为 800x600 的照片。

请帮助指导我!

你可以

简单地使用 getImageSize((,因为

list($width, $height) = getimagesize(Input::file('file'));

这样检查验证

if ($validator->passes() && correct_size(Input::file('file'))) {
    // do something
}

创建一个这样的验证函数,无论您在其中选择哪种模型

public function correct_size($photo) {
    $maxHeight = 100;
    $maxWidth = 100;
    list($width, $height) = getimagesize($photo);
    return ( ($width <= $maxWidth) && ($height <= $maxHeight) );
}

您可以决定是否要静态使用它。 但是,如果您想在没有依赖项的情况下使用相同的验证检查其他上传,我的建议不是静态定义它。

在表单验证中检查如下。

'avatar' => 'dimensions:min_width=100,min_height=200'