yii2-crop-image-upload icon indicating copy to clipboard operation
yii2-crop-image-upload copied to clipboard

image is saving without cropping.

Open asifrafeeq opened this issue 7 years ago • 5 comments

I m using this extension in frontend. the code is working for only uploading the images, but the cropping is not working. the directory for images is frontend/web/profile_images this is my code Model

use karpoff\icrop\CropImageUploadBehavior;
   function behaviors()
    {
        return [
            [
                'class' => CropImageUploadBehavior::className(),
                'attribute' => 'image',
                'scenarios' => ['insert', 'update'],
                'path' => '@webroot/profile_images',
                'url' => '@web/profile_images',
                'ratio' => 1,
                'crop_field' => 'photo_crop',
                'cropped_field' => 'photo_cropped',
            ],
        ];
    }

View


use karpoff\icrop\CropImageUpload; 
<?= $form->field($model, 'image')->widget(CropImageUpload::className()) ?>

Controller

public function actionUpdateprofile($id){
        $this->layout="courses_layout";
                $model = Profile::find()->where(['user_id'=>$id])->one();
                
                $uploadedimage = $model->image;
                
                if(!empty($uploadedimage)){
                $uploadedimage_path = $model->image_path;
                }
            
            if ($model->load(Yii::$app->request->post())) {
         $image = UploadedFile::getInstance($model, 'image');
                if(!empty($image)) 
                {
                    
                    
                 $no=rand(0,1000);
               
                $image = UploadedFile::getInstance($model, 'image');
             
                $model->image= $no.$image->name;
                
                    $image->saveAs(\Yii::$app->basePath.'/web/profile_images/'.$model->image);
                    $model->image_path = 'profile_images/'.$model->image;
                } 
                
                else 
                
                {
                $model->image = $uploadedimage;
                 $model->image_path = $uploadedimage_path;
                
                }

                 $model->save(false);
        
                return $this->redirect(['profile','id'=>$id]);
            } else {
                return $this->render('profile_update', [
                    'model' => $model,
                ]);
            }
        }

please guide me where is the error?

asifrafeeq avatar Dec 05 '17 09:12 asifrafeeq

Hi, asifrafeeq this product is out of support now. moreover I haven't wrote php code for a couple of years :) at first sight I can say that you don't need to save image manually. it is done automatically by CropImageUploadBehavior if it is assigned to model

karpoff avatar Dec 05 '17 19:12 karpoff

Ok thnx a lot. I am checking it again.

asifrafeeq avatar Dec 06 '17 05:12 asifrafeeq

Dear Karpoff , so what should I do to save the image, because I am already attaching the CropImageUploadBehavior to the model

asifrafeeq avatar Dec 06 '17 06:12 asifrafeeq

you just need to save model please see actionEdit here

karpoff avatar Dec 06 '17 08:12 karpoff

Ok, so nice of u. thanks a lot.

asifrafeeq avatar Dec 06 '17 10:12 asifrafeeq