/**
  * Internaly validates the image file object.
  * 
  * @param CActiveRecord $object
  * @param string $attribute
  * @param CUploadedFile $file 
  */
 protected function validateFile($object, $attribute, $file)
 {
     parent::validateFile($object, $attribute, $file);
     if ($object->hasErrors($attribute)) {
         return;
     }
     if (null === $this->maxWidth && null === $this->maxHeight) {
         return;
     }
     $this->maxWidth = (int) $this->maxWidth;
     $this->maxHeight = (int) $this->maxHeight;
     list($width, $height) = getimagesize($file->getTempName());
     if ($this->maxWidth > 0 && $this->maxWidth < $width) {
         $this->addError($object, $attribute, $this->tooWide, array('{value}' => $this->maxWidth));
     }
     if ($this->maxHeight > 0 && $this->maxHeight < $height) {
         $this->addError($object, $attribute, $this->tooTall, array('{value}' => $this->maxHeight));
     }
 }
 /**
  * {@inheritDoc}
  * @see CFileValidator::validateAttribute()
  */
 protected function validateAttribute($object, $attribute)
 {
     parent::validateAttribute($object, $attribute);
     $file = CUploadedFile::getInstance($object, $attribute);
     if ($file === null) {
         return;
     }
     $info = @getimagesize($file->getTempName());
     if ($info === false) {
         $message = $this->wrongType ? $this->wrongType : Yii::t('yii', 'The file "{file}" cannot be uploaded. Only image files are allowed.');
         $this->addError($object, $attribute, $message, array('{file}' => CHtml::encode($file->getName())));
         return;
     }
     if (isset($this->minWidth)) {
         if ($info[0] < $this->minWidth) {
             $message = $this->tooNarrowWidth ? $this->tooNarrowWidth : Yii::t('photo.validator', 'The image "{file}" should be at least {minWidth}px in width.');
             $this->addError($object, $attribute, $message, array('{file}' => CHtml::encode($file->getName()), '{minWidth}' => $this->minWidth));
         }
     }
     if (isset($this->maxWidth)) {
         if ($info[0] > $this->maxWidth) {
             $message = $this->tooWideWidth ? $this->tooWideWidth : Yii::t('photo.validator', 'The image "{file}" should be at max {maxWidth}px in width.');
             $this->addError($object, $attribute, $message, array('{file}' => CHtml::encode($file->getName()), '{maxWidth}' => $this->maxWidth));
         }
     }
     if (isset($this->minHeight)) {
         if ($info[1] < $this->minHeight) {
             $message = $this->tooNarrowHeight ? $this->tooNarrowHeight : Yii::t('photo.validator', 'The image "{file}" should be at least {minHeight}px in height.');
             $this->addError($object, $attribute, $message, array('{file}' => CHtml::encode($file->getName()), '{minHeight}' => $this->minHeight));
         }
     }
     if (isset($this->maxHeight)) {
         if ($info[1] > $this->maxHeight) {
             $message = $this->tooWideHeight ? $this->tooWideHeight : Yii::t('photo.validator', 'The image "{file}" should be at max {maxheight}px in height.');
             $this->addError($object, $attribute, $message, array('{file}' => CHtml::encode($file->getName()), '{maxheight}' => $this->maxHeight));
         }
     }
 }
 /**
  * Internally validates a file object.
  * @param CModel the object being validated
  * @param string the attribute being validated
  * @param CUploadedFile uploaded file passed to check against a set of rules
  */
 public function validateFile($object, $attribute, $file)
 {
     // do parent validation
     parent::validateFile($object, $attribute, $file);
     // prevent next validation if no file, other UPLOAD_ERR_s was checked before
     if (null === $file || $file->getError() == UPLOAD_ERR_NO_FILE) {
         return;
     }
     /**
      * Index 0 and 1 contains respectively the width and the height of the 
      * image. 
      * Index 2 is one of the IMAGETYPE_XXX constants indicating the type of 
      * the image. 
      * Index 3 is a text string with the correct height="yyy" width="xxx" 
      * string. 
      * Key 'mime' is the correspondant MIME type of the image. 
      * Key 'channels' will be 3 for RGB pictures and 4 for CMYK pictures. 
      * Key 'bits' is the number of bits for each color.
      */
     $info = @getimagesize($file->getTempName());
     if (!$info) {
         $message = Yum::t('The file "{file}" is not an image.', array(), 'coreMessages');
         $this->addError($object, $attribute, $message, array('{file}' => $file->getName()));
         return;
     }
     // remove unnecessary values from $info and make it more readable
     $info = array('width' => $info[0], 'height' => $info[1], 'mime' => $info['mime']);
     if ($this->width !== null) {
         if ($info['width'] != $this->width) {
             $message = $this->widthError ? $this->widthError : Yum::t('The image "{file}" width should be "{width}px".', array(), 'coreMessages');
             $this->addError($object, $attribute, $message, array('{file}' => $file->getName(), '{width}' => $this->width));
         }
     }
     if ($this->minWidth !== null) {
         if ($info['width'] < $this->minWidth) {
             $message = $this->minWidthError ? $this->minWidthError : Yum::t('The image "{file}" width should be at least "{width}px".', array(), 'coreMessages');
             $this->addError($object, $attribute, $message, array('{file}' => $file->getName(), '{width}' => $this->minWidth));
         }
     }
     if ($this->maxWidth !== null) {
         if ($info['width'] > $this->maxWidth) {
             $message = $this->maxWidthError ? $this->maxWidthError : Yum::t('The image "{file}" width should be at most "{width}px".', array(), 'coreMessages');
             $this->addError($object, $attribute, $message, array('{file}' => $file->getName(), '{width}' => $this->maxWidth));
         }
     }
     if ($this->height !== null) {
         if ($info['height'] != $this->height) {
             $message = $this->heightError ? $this->heightError : Yum::t('The image "{file}" height should be "{height}px".', array(), 'coreMessages');
             $this->addError($object, $attribute, $message, array('{file}' => $file->getName(), '{height}' => $this->height));
         }
     }
     if ($this->minHeight !== null) {
         if ($info['height'] < $this->minHeight) {
             $message = $this->minHeightError ? $this->minHeightError : Yum::t('The image "{file}" height should be at least "{height}px".', array(), 'coreMessages');
             $this->addError($object, $attribute, $message, array('{file}' => $file->getName(), '{height}' => $this->minHeight));
         }
     }
     if ($this->maxHeight !== null) {
         if ($info['height'] > $this->maxHeight) {
             $message = $this->maxHeightError ? $this->maxHeightError : Yum::t('The image "{file}" height should be at most "{height}px".', array(), 'coreMessages');
             $this->addError($object, $attribute, $message, array('{file}' => $file->getName(), '{height}' => $this->maxHeight));
         }
     }
     if ($this->smallSideSize !== null) {
         if (min($info['height'], $info['width']) == $this->smallSideSize) {
             $message = $this->smallSideSizeError ? $this->smallSideSizeError : Yum::t('The image "{file}" small side should be "{side}px".', array(), 'coreMessages');
             $this->addError($object, $attribute, $message, array('{file}' => $file->getName(), '{side}' => $this->smallSideSize));
         }
     }
     if ($this->smallSideMinSize !== null) {
         if (min($info['height'], $info['width']) < $this->smallSideMinSize) {
             $message = $this->smallSideMinSizeError ? $this->smallSideMinSizeError : Yum::t('The image "{file}" small side should be at least "{side}px".', array(), 'coreMessages');
             $this->addError($object, $attribute, $message, array('{file}' => $file->getName(), '{side}' => $this->smallSideMinSize));
         }
     }
     if ($this->smallSideMaxSize !== null) {
         if (min($info['height'], $info['width']) > $this->smallSideMaxSize) {
             $message = $this->smallSideMaxSizeError ? $this->smallSideMaxSizeError : Yum::t('The image "{file}" small side should be at most "{side}px".', array(), 'coreMessages');
             $this->addError($object, $attribute, $message, array('{file}' => $file->getName(), '{side}' => $this->smallSideMaxSize));
         }
     }
     if ($this->bigSideSize !== null) {
         if (max($info['height'], $info['width']) == $this->bigSideSize) {
             $message = $this->bigSideSizeError ? $this->bigSideSizeError : Yum::t('The image "{file}" big side should be "{side}px".', array(), 'coreMessages');
             $this->addError($object, $attribute, $message, array('{file}' => $file->getName(), '{side}' => $this->bigSideSize));
         }
     }
     if ($this->bigSideMinSize !== null) {
         if (max($info['height'], $info['width']) < $this->bigSideMinSize) {
             $message = $this->bigSideMinSizeError ? $this->bigSideMinSizeError : Yum::t('The image "{file}" big side should be at least "{side}px".', array(), 'coreMessages');
             $this->addError($object, $attribute, $message, array('{file}' => $file->getName(), '{side}' => $this->bigSideMinSize));
         }
     }
     if ($this->bigSideMaxSize !== null) {
         if (max($info['height'], $info['width']) > $this->bigSideMaxSize) {
             $message = $this->bigSideMaxSizeError ? $this->bigSideMaxSizeError : Yum::t('The image "{file}" big side should be at most "{side}px".', array(), 'coreMessages');
             $this->addError($object, $attribute, $message, array('{file}' => $file->getName(), '{side}' => $this->bigSideMaxSize));
         }
     }
     if ($this->mimeType !== null) {
         $mimeTypes = is_scalar($this->mimeType) ? array($this->mimeType) : $this->mimeType;
         if (!in_array($info['mime'], $mimeTypes)) {
             $message = $this->mimeTypeError ? $this->mimeTypeError : Yum::t('The image "{file}" mime type "{mime}" is not allowed.', array(), 'coreMessages');
             $this->addError($object, $attribute, $message, array('{file}' => $file->getName(), '{mime}' => $info['mime']));
         }
     }
 }
Beispiel #4
0
 /**
  * (non-PHPdoc)
  * @see CFileValidator::validateFile()
  */
 protected function validateFile($object, $attribute, $file)
 {
     parent::validateFile($object, $attribute, $file);
     $size = getimagesize($file->getTempName());
     if ($size === false) {
         $message = $this->notImage ? $this->notImage : Yii::t('yii', 'This is not a picture');
         $this->addError($object, $attribute, $message, array('{mime}' => is_array($this->mime) ? implode(', ', $this->mime) : $this->mime));
         return;
     }
     if (!$this->checkMime($attribute, $file)) {
         $message = $this->wrongMime ? $this->wrongMime : Yii::t('yii', 'This mime type of the photo is not allowed, mime types: {mime}');
         $this->addError($object, $attribute, $message, array('{mime}' => is_array($this->mime) ? implode(', ', $this->mime) : $this->mime));
     }
     if ($this->minWidth !== null && $size[0] < $this->minWidth) {
         $message = $this->tooSmallWidth ? $this->tooSmallWidth : Yii::t('yii', 'Photo should be at least {width}px in width');
         $this->addError($object, $attribute, $message, array('{width}' => $this->minWidth));
     }
     if ($this->maxWidth !== null && $size[0] > $this->maxWidth) {
         $message = $this->tooLargWidth ? $this->tooLargWidth : Yii::t('yii', 'Photo should be at max {width}px in width');
         $this->addError($object, $attribute, $message, array('{width}' => $this->maxWidth));
     }
     if ($this->minHeight !== null && $size[1] < $this->minHeight) {
         $message = $this->tooSmallHeight ? $this->tooSmallHeight : Yii::t('yii', 'Photo should be at least {height}px in height');
         $this->addError($object, $attribute, $message, array('{height}' => $this->minHeight));
     }
     if ($this->maxHeight !== null && $size[1] > $this->maxHeight) {
         $message = $this->tooLargHeight ? $this->tooLargHeight : Yii::t('yii', 'Photo should be at max {height}px in height');
         $this->addError($object, $attribute, $message, array('{height}' => $this->maxHeight));
     }
 }
Beispiel #5
0
 /**
  * Checks whether the uploaded note size less than 500 KB and its type is allowed.
  * @param  string $attribute
  * @param  array $params
  */
 public function checkNote($attribute, $params)
 {
     if (empty($this->raw_file_text)) {
         $validator = new CFileValidator();
         $validator->attributes = array('file');
         $validator->maxSize = self::MAX_FILE_SIZE;
         $validator->message = '{attribute} tidak boleh kosong.';
         $validator->tooLarge = '{attribute} maksimum ' . Yii::app()->format->size(self::MAX_FILE_SIZE) . '.';
         $validator->wrongType = '{attribute} hanya boleh bertipe PDF atau JPEG';
         $allowedTypes = Note::getAllowedTypes();
         foreach ($allowedTypes as $info) {
             $validator->types[] = $info['extension'];
         }
         $validator->validate($this);
     }
 }
 /**
  * @dataProvider providerSizeToBytes
  *
  * @param string $sizeString
  * @param integer $assertion
  */
 public function testSizeToBytes($sizeString, $assertion)
 {
     $fileValidator = new CFileValidator();
     $this->assertEquals($assertion, $fileValidator->sizeToBytes($sizeString));
 }