/**
  * 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));
     }
 }
 /**
  * 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']));
         }
     }
 }
Example #3
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));
     }
 }