we can validate its mime type, min height, max height, min width and max width.
Version 1.0 alpha, but works

Attached File(s)
-
EPhotoValidator.php (4.34K)
Number of downloads: 12
Posted 03 May 2009 - 04:53 PM
Posted 10 May 2009 - 03:47 PM
<?php public function rules()
{
return array(
array('image', 'EPhotoValidator', 'maxWidth'=>800, 'maxHeight'=>600,'mimeType'=>array('image/jpeg')),
);
}
?>
<?php echo CHtml::activeFileField($couleur,'image'); ?>
Posted 22 May 2009 - 05:05 AM
/**
* EPhotoValidator class file.
*
* @author emix
* @link http://www.yiiframework.com/
* @copyright Copyright © 2009 emix
* @license
*
* Copyright © 2008 by emix
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification,
* are permitted provided that the following conditions are met:
*
* - Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* - Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
* - Neither the name of emix nor the names of its contributors may
* be used to endorse or promote products derived from this software without
* specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
* GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
* EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
*/
class EPhotoValidator extends CFileValidator {
/**
* @var string Allowed mime type of the image, ie: image/jpeg
*/
public $mimeType;
/**
* @var string Mime type error message
*/
public $mimeTypeError;
/**
* @var int Minimum width of the image required.
*/
public $minWidth;
/**
* @var string Mime type error message
*/
public $minWidthError;
/**
* @var int Maximum width of the image allowed
*/
public $maxWidth;
/**
* @var string Mime type error message
*/
public $maxWidthError;
/**
* @var int Minimum height of the image required.
*/
public $minHeight;
/**
* @var string Mime type error message
*/
public $minHeightError;
/**
* @var int Maximum height of the image allowed.
*/
public $maxHeight;
/**
* @var string Mime type error message
*/
public $maxHeightError;
/**
* Validates the attribute of the object.
* If there is any error, the error message is added to the object.
* @param CModel the object being validated
* @param string the attribute being validated
*/
protected function validateAttribute($object,$attribute)
{
parent::validateAttribute($object, $attribute);
$file=$object->$attribute;
if(!($file instanceof CUploadedFile))
$file=CUploadedFile::getInstance($object,$attribute);
if($this->allowEmpty && ($file===null || $file->getError()==UPLOAD_ERR_NO_FILE))
return;
$info=getimagesize($file->getTempName());
if (isset($this->minWidth)) {
if ($info[0] < $this->minWidth)
{
$message=$this->minWidthError ? $this->minWidthError : Yii::t('yii','Photo should be at least {width}px in width');
$this->addError($object,$attribute,$message,array('{width}'=>$this->minWidth));
}
}
if (isset($this->maxWidth)) {
if ($info[0] > $this->maxWidth)
{
$message=$this->maxWidthError ? $this->maxWidthError : Yii::t('yii','Photo should be at max {width}px in width');
$this->addError($object,$attribute,$message,array('{width}'=>$this->maxWidth));
}
}
if (isset($this->minHeight)) {
if ($info[1] < $this->minHeight)
{
$message=$this->minHeightError ? $this->minHeightError : Yii::t('yii','Photo should be at least {height}px in height');
$this->addError($object,$attribute,$message,array('{height}'=>$this->minHeight));
}
}
if (isset($this->maxHeight)) {
if ($info[1] > $this->maxHeight)
{
$message=$this->maxHeightError ? $this->maxHeightError : Yii::t('yii','Photo should be at max {height}px in height');
$this->addError($object,$attribute,$message,array('{height}'=>$this->maxHeight));
}
}
if (isset($this->mimeType))
{
$mime=is_scalar($this->mimeType) ? array($this->mimeType) : $this->mimeType;
if (!in_array($file->getType(), $mime))
{
$message=$this->mimeTypeError ? $this->mimeTypeError : Yii::t('yii','This mime type of the photo is not allowed');
$this->addError($object,$attribute,$message);
}
}
}
}
Posted 01 July 2009 - 07:27 PM
Posted 08 July 2009 - 03:50 AM
Quote
Posted 14 January 2010 - 11:27 PM
Quote
Quote
Quote
Posted 14 February 2010 - 06:26 AM
Posted 14 February 2010 - 08:43 AM
phiras, on 14 February 2010 - 06:26 AM, said:
Posted 27 February 2010 - 03:03 PM
$message = $this->heighError ? $this->heightError :
$message = $this->heightError ? $this->heightError :
Posted 28 February 2010 - 05:49 AM
TiBe, on 27 February 2010 - 03:03 PM, said:
Posted 15 October 2010 - 07:36 AM
Posted 15 October 2010 - 10:03 AM
juban_, on 08 July 2009 - 03:50 AM, said:
Posted 16 October 2010 - 04:11 AM
Antonio Ramirez, on 15 October 2010 - 10:03 AM, said:
array('image', 'EPhotoValidator', 'types'=>'jpg, gif, png', 'allowEmpty' => false, 'minWidth' => 120, 'minHeight' => 120, 'on' => 'insert'), array('image', 'EPhotoValidator', 'types'=>'jpg, gif, png', 'allowEmpty' => true, 'minWidth' => 120, 'minHeight' => 120),
Posted 16 October 2010 - 11:56 AM
vladm, on 16 October 2010 - 04:11 AM, said:
array('image', 'EPhotoValidator', 'types'=>'jpg, gif, png', 'allowEmpty' => false, 'minWidth' => 120, 'minHeight' => 120, 'on' => 'insert'), array('image', 'EPhotoValidator', 'types'=>'jpg, gif, png', 'allowEmpty' => true, 'minWidth' => 120, 'minHeight' => 120),
Posted 16 October 2010 - 12:22 PM
Posted 16 October 2010 - 03:55 PM
Antonio Ramirez, on 16 October 2010 - 12:22 PM, said:
Posted 16 October 2010 - 07:21 PM
vladm, on 16 October 2010 - 03:55 PM, said:
Posted 10 February 2011 - 08:25 AM