Reputation: 4192
I have a simple validator for image file input which looks like this:
public function rules()
{
return [
['thumbnail', 'image', 'minWidth' => 800],
];
}
But if I try to send a txt file, it does not show an error message, but the ImageValidator
throws me an error:
PHP Notice – yii\base\ErrorException
getimagesize(): Read error!
Why does this happen? Why does it not stop on the image rule and why does it call getimagesize
if it is not an image file? How can I suppress it?
Upvotes: 0
Views: 300
Reputation: 4192
After few times I have this workaround:
['thumbnail', 'image', 'minWidth' => 800, 'when' => function($model) {
$check = @getimagesize($model->thumbnail->tempName);
if( !$check ) $model->addError('thumbnail', 'File is not an image.');
if( !$check['mime'] || !in_array($check['mime'], ['image/jpg', 'image/jpeg']) ) $model->addError('thumbnail', 'File is not jpeg.');
return $check ? true : false;
}],
BUT the correct form is
['thumbnail', 'image',
'extensions' => ['png', 'jpg', 'gif'],
'mimeTypes' => ['image/*'],
'minWidth' => 800],
Upvotes: 1