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',
);
}
excellent!!
this is awsome!!
Excellent Job!
I was planning this extension but you have done it so well. Thanks
@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.
hi
thanks for this extension is there is any way to set the size of file in kb inplace of width and height.
Updates
I have added some extra features to this extension. Feel free to use it :)
Very useful
Thanks Turi, its simple, but very useful
Tank you! Brilliant work!
Thank you for this extension :) I'ts very important for me now :)
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"
@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... :)
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?
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
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.
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
Higher dimension
Why it does not validate higher dimesnions images, is it problem in extension or is it a problem in uploading?
@Usman Iqbal
Dear Usman, please can you provide more detail about your problem. For example, type of image and dimensions of the image, also this extension configuration that you are using.
Regrads,
Ibrar Turi
If you have any questions, please ask in the forum instead.
Signup or Login in order to comment.