Switch branches/tags
Nothing to show
Find file
Fetching contributors…
Cannot retrieve contributors at this time
62 lines (51 sloc) 2.28 KB


Image upload widget for YII 1.x framework.


  • preview of currrent image
  • possibility to delete current image without uploading new image
  • checks whether submitted image is valid
  • checks whether submitted image is in specified dimensions
  • provides state variable which indicates whether image left unchanged or new image is uploaded


Add widget to active form

		'absolute_path' => '/images/full/', // absolute path where images are stored
		'preview_width' => '320', // optional attribute - default 160
        'preview_height' => '200' // optional attribute - default 120

Add widget using form builder

	'absolute_path' => '/images/full/'

Add validator with basic configuration


It only checks wheter submitted image is valid. Validation will not be trigered if value is empty, it can be done using required validator.

Add validator with additional configuration

	  'maxWidth' => 640,
	  'minWidth' => 200,
	  'maxHeight' => 480,
	  'minHeight' => 120

Any of additional attributes can be omitted

Handle form submit and save file to disk

$state = Yii::app()->request->getPost(DstImageField::getStateHiddenFieldName('image_full'));

if($state == DstImageField::STATE_CHOSEN) {
	$image_full = CUploadedFile::getInstanceByName(DstImageField::getFileFieldName('image_full'));
	$image_full->saveAs('images/content-location/' . $image_full, false);

	$image = Yii::app()->image->load('images/content-location/' . $image_full);
	$image->resize(640, 480)->quality(50);

Possible states for DstImageField are:

  • STATE_CHOSEN - in this case new image has been submitted and storage can be handled with code above
  • STATE_EMPTY - image has not been submitted so if field is not required empty value will be set to model
  • STATE_UNCHANGED - image has not been submitted but model already contains image which will be shown in preview.