Yii 1.1: eimagevalidator

Validates uploaded image types, min & max size, width and height
13 followers

Validates the uploaded image with types (jpg, gif, png...etc), min & max size (in kilo bytes, width and height. This extension can be used for different purposes depending upon your requirements.

Requirements

Tested with Yii 1.1.10 and 1.1.12. may work on other versions

Usage

Extract the file under protected/components

Example usage with types:

public function rules()
{
    return array(
        'photo', 'EImageValidator', 'types' => "gif, jpg, png",
    );
}

Example usage with types and error message:

public function rules()
{
    return array(
        'photo', 'EImageValidator', 'types' => "gif, jpg, png", 'typesError' => 'Types error message',
    );
}

Example usage with min & max size (in kilo bytes):

public function rules()
{
    return array(
        'photo', 'EImageValidator', 'min_size' => 10, 'max_size' => 50,
    );
}

Example usage with min & max size (in kilo bytes) and error message:

public function rules()
{
    return array(
        'photo', 'EImageValidator', 'min_size' => 10, 'max_size' => 50, 'sizeError' => 'Size error message',
    );
}

Example usage with width and height:

public function rules()
{
    return array(
        'photo', 'EImageValidator', 'width' => 600, 'height' => 400,
    );
}

Example usage with width, height and allowEmplty:

public function rules()
{
    return array(
        'photo', 'EImageValidator', 'width' => 600, 'height' => 400, 'allowEmpty' => 'true',
    );
}

Example usage with width, height and error message:

public function rules()
{
    return array(
        'photo', 'EImageValidator', 'width' => 600, 'height' => 400, 'dimensionError' => 'Image dimension error',
    );
}

Resources

Total 13 comments

#18676 report it
turi at 2014/12/12 03:39am
Added min & Max Size feature

I have removed exact size feature & added feature for setting min & max size for the uploaded file. Hope this be useful to you guys.

Regards, Ibrar Turi

#18669 report it
moginn at 2014/12/09 11:48am
exact size and exact dimensions

I agree with ejaz karim. There should be an option to specify the maximum and minimum values for the size and dimensions. It is pointless to require exact values.

#18022 report it
darkheir at 2014/08/27 04:11am
If one of the dimension is good the image will be valid

I checked the code and to check the image size the following is applied:

if ($imageWidth != $this->width && $imageHeight != $this->height) {
    //error
}

So if one of the dimmension is valid the validator will validate the image

#17728 report it
ejaz karim at 2014/07/17 11:13am
How to set MaxSize?

Whenever I try to limit size to 400kb it says "The file "forms.html" is either small or large. Its size should be 400 kilo bytes." What I really want to do is to allow max size limit to 400kb not exact 400kb. Is there any way to do this?

#15921 report it
turi at 2013/12/29 11:49pm
@UASergey

It does work with local...

This extension works either on the local or live server. It just provides you with image validation which is provided by this validation.

For image uploading you can check this article... which might help you; how to upload images/files in yii...

Then you can use this extension to apply validation if you want any... :)

#15915 report it
UASergey at 2013/12/29 07:17am
does it work with local files?

I think it does not work.

//begin of the model
public $image_path = false;
 
//rules of the model
array('image_path', 'EImageValidator', 'types' => "gif, jpg, png")
 
//use
$property = new Property();
$property->image_path = '/home/billy/1.png';
$property->save(); 
 
It shows me "Image Path cannot be blank"
#15311 report it
Angus123 at 2013/10/27 12:19pm
Tank you! Brilliant work!

Thank you for this extension :) I'ts very important for me now :)

#15290 report it
Aman Rohiman at 2013/10/24 02:05am
Very useful

Thanks Turi, its simple, but very useful

#13687 report it
turi at 2013/06/17 08:49am
Updates

I have added some extra features to this extension. Feel free to use it :)

#13666 report it
rajesh chaurasia at 2013/06/15 03:20am
hi

thanks for this extension is there is any way to set the size of file in kb inplace of width and height.

#13629 report it
turi at 2013/06/12 10:57am
@rajesh chaurasia

One way is to use the image type would be like the follwing:

public function rules()
{
    return array(
        array('photo', 'file', 'types' => 'jpg, gif, png'),
        array('photo', 'EImageValidator', 'width' => 600, 'height' => 400),
    );
}

the other way would be to add MIME type option to this extension... which i will try to add soon.

#13627 report it
shiv at 2013/06/12 08:17am
Excellent Job!

I was planning this extension but you have done it so well. Thanks

#13605 report it
sefburhan at 2013/06/11 03:54am
excellent!!

this is awsome!!

Leave a comment

Please to leave your comment.

Create extension