2

I want to force the user to upload square pictures. I mean that the width and height of the square be equal. In core validators of the yii2 we have image type with this definition:

['primaryImage', 'image', 'extensions' => 'png, jpg',
        'minWidth' => 100, 'maxWidth' => 1000,
        'minHeight' => 100, 'maxHeight' => 1000,
],

now if i want to use custom validator which support client validator what should I do. Please i want to limit the user to upload my forced width and height, just want to force that image be square.

Behzad Hassani
  • 2,129
  • 4
  • 30
  • 51

1 Answers1

2

You can extend ImageValidator:

class ImageSquareValidator extends ImageValidator
{
    /**
     * @inheritdoc
     */
    protected function validateImage($image)
    {
        if (false === ($imageInfo = getimagesize($image->tempName))) {
            return [$this->notImage, ['file' => $image->name]];
        }

        list($width, $height) = $imageInfo;

        if ($width !== $height) {
            return [Yii::t('yii', 'The image "{file}" is not square.'), ['file' => $image->name]];
        }

        return parent::validateImage($image);
    }
}

In client validation you can use this example.

Community
  • 1
  • 1
Onedev_Link
  • 1,981
  • 13
  • 26