Image Extension: Image And Its Behaviors Do Not Have A Method Or Closure Named "resize".

Hi @all,

I tried to use the Image extension of Yii. Installed the extension as described at the official extension site.

My main.php looks like this:





 // autoloading model and component classes

        'import'=>array(

            'application.models.*',

            'application.components.*',

            'application.modules.user.models.*',

            'application.modules.user.components.*',

            'application.modules.rights.*',

            'application.modules.rights.components.*',

            'application.helpers.*',

        ), [...]


'image'=>array(

                    'class'=>'application.extensions.image.CImageComponent',

                    // GD or ImageMagick

                    'driver'=>'GD',

                    // ImageMagick setup path

                    //'params'=>array('/extensions/image/drivers'),

                 ),

and I tried to use the extension in my ImageController.php for resizing an Image submitted by an upload form:

ImageController.php actionCreate() method:


 if(isset($_POST['Image']))

		{

                        $model->attributes=$_POST['Image'];

                        if($model->validate()) {             

                            // upload image file

                            $model->imgName = CUploadedFile::getInstance($model, 'imgName');

                            if($model->imgName == null) {

                                $model->addError('imgName', 'Please upload a picture.');

                            }

                            else {

                                // CVarDumper::dump($model->imgName, 10, true);

                                // create a temporary copy of the Uploaded File object

                                $temp_img = $model->imgName;

                                $imgDir = Yii::app()->params['imgDir'];

                                // @todo: potentially unsafe as imgName could be used for hijacking

                                $imgName_new = Yii::app()->user->getId().'_'.time().'_'.$model->imgName->getName();

                                $model->setAttribute('imgName', $imgName_new);

                                $model->setAttribute('createdBy', Yii::app()->user->getId());

                                $model->setAttribute('createdAt', date('Y-m-d H:i:s'));     

                                if($model->save()) {

                                        $temp_img->saveAs($imgDir.'/'.$imgName_new);

                                        // resizing options

                                        Yii::import('application.extensions.image.Image');

                                       

                                       

                                        $image = new Image($imgDir.'/'.$imgName_new);

                                        CVarDumper::dump($image, 10, true);

                                        $image->resize('400', '300');

                                        $image->render();

                                        // redirect to success page

                                        $this->redirect(array('view','id'=>$model->id));

                                }

                            }

                        }

I also tried the 2nd usage example by using “$image = Yii::app()->image->load(‘pathToImg’); etc.” but I always receive a CException error:

. Even if I try to create an Image object with an invalid img src I got no error message that the image doesn’t exist as I should…

Every other extension works perfectly well.

Best wishes,

Sebastian

Okay I found the problem. I had an own class called “Image” which of course was not providing the resize method by itself. I changed my own created class ‘Image’ to ‘rImage’ and the problem disappeared. Maybe someone will save that lots of time.

Hi, I am learning on how to upload a photo by using Yii Image extension. However, I have problem of uploading a photo. It returns an error of ‘[color="#FF0000"]move_uploaded_file(/admin_1394462451_horse.jpg): failed to open stream: Permission denied[/color]’. Any ideas how to solve it? Thanks in advance.

Here’s my code:

controller




public function actionCreate()

	{

		$model=new Student;

		// Uncomment the following line if AJAX validation is needed

		// $this->performAjaxValidation($model);


		if(isset($_POST['Student']))

		{

		    $model->attributes=$_POST['Student'];

                        if($model->validate()) {             

                            // upload image file

                            $model->foto = CUploadedFile::getInstance($model, 'foto');

                            if($model->foto == null) {

                                $model->addError('foto', 'Please upload a picture.');

                            }

                            else {

                                // CVarDumper::dump($model->imgName, 10, true);

                                // create a temporary copy of the Uploaded File object

                                $temp_img = $model->foto;

                                $imgDir = Yii::app()->params['imgDir'];

                                // @todo: potentially unsafe as imgName could be used for hijacking

                                $imgName_new = Yii::app()->user->getId().'_'.time().'_'.$model->foto->getName();

                                $model->setAttribute('foto', $imgName_new);

                                $model->setAttribute('createdBy', Yii::app()->user->getId());

                                $model->setAttribute('createdAt', date('Y-m-d H:i:s'));     

                                if($model->save()) {

                                        $temp_img->saveAs($imgDir.'/'.$imgName_new);

                                        // resizing options

                                        Yii::import('application.extensions.image.Image');

                                       

                                       

                                        $image = new Image($imgDir.'/'.$imgName_new);

                                        CVarDumper::dump($image, 10, true);

                                        $image->resize('400', '300');

                                        $image->render();

                                        // redirect to success page

                                        $this->redirect(array('view','id'=>$model->id));

                                }

                            }

                        }

        }  

		$this->render('create',array(

			'model'=>$model,

		));

	}



model




public function rules()

	{

		// NOTE: you should only define rules for those attributes that

		// will receive user inputs.

		return array(

			array('username, password, programme, intake, email, contact', 'required'),

			array('contact', 'numerical', 'integerOnly'=>true),

			array('username', 'length', 'max'=>10),

			array('password, programme, intake', 'length', 'max'=>20),

			array('email', 'length', 'max'=>50),

			array('foto', 'file', 'allowEmpty'=>true,'maxSize'=>1024*1024*5, //ukuran file max 

                             'types'=>'jpg,jpeg,png','tooLarge'=>'Ukuran foto tidak lebih dari 5Mb',

                             'wrongType'=>'Jenis file hanya JPEG atau PNG',

                             'on'=>'insert'),

            array('foto', 'file', 'allowEmpty'=>true,'maxSize'=>1024*1024*5, 

                             'types'=>'jpg,jpeg,png','tooLarge'=>'Ukuran foto tidak lebih dari 5Mb',

                             'wrongType'=>'Jenis file hanya JPEG atau PNG',

                             'except'=>'insert'),


			// The following rule is used by search().

			// @todo Please remove those attributes that should not be searched.

			array('id, username, password, programme, intake, email, contact', 'safe', 'on'=>'search'),

		);

	}




_form




....

<?php $form=$this->beginWidget('CActiveForm', array(

	'id'=>'student-form',

	'enableAjaxValidation'=>false,

	'htmlOptions' => array(

        'enctype' => 'multipart/form-data', //tambahan dari _form hasil gii

    ),


)); ?>


	<p class="note">Fields with <span class="required">*</span> are required.</p>


	<?php echo $form->errorSummary($model); ?>

    

	 <div class="row">

                <?php echo $form->labelEx($model,'foto'); ?>

                <?php echo CHtml::activeFileField($model, 'foto'); ?>  <font color='orange'><=.jpg/.png dan pastikan ukuran file max 5Mb</font>

                <?php echo $form->error($model,'foto'); ?>

     </div>

      <?php if($model->isNewRecord!='1'){ ?>

     <div class="row">

     <?php echo CHtml::image($model->foto,"foto",array("width"=>100)); ?>  

     </div>

     <?php } ?>

.....



config/main.php




'import'=>array(

	    'application.extensions.KEmail.KEmail',

		'application.helpers.*',

		'application.models.*',

		'application.components.*',

	),


'components'=>array(

		'user'=>array(

			// enable cookie-based authentication

			'allowAutoLogin'=>true,

			//'class'=>'userGroups.components.WebUserGroups',

		),

	  'image'=>array(

          'class'=>'application.extensions.image.CImageComponent',

            // GD or ImageMagick

            'driver'=>'GD',

            // ImageMagick setup path

            'params'=>array('directory'=>'/opt/local/bin'),

        ),



It works for me perfectly! Great Extension

I use Image extension. and i only want to display image with resized size. i don’t want to save image after resizing it.

In my controller i use following function




 public function actionImageResize()

        {

            Yii::import('application.extensions.image.Image');

            $image = new Image('images/wallpaper-3.jpg');

            //CVarDumper::dump($image, 10, true);

            $image->resize(400, 100);

            $image->render();

            $this->redirect(array('ImageTag'));


        }



but i can’t get any image in my ImageTag page. whats wrong? Can you help me?