bupy7 / yii2-widget-cropbox

This widget allows crop image before upload to server and send informations about crop in JSON format.
BSD 3-Clause "New" or "Revised" License
91 stars 33 forks source link

Undefined index: dHeight #8

Closed jepp112 closed 9 years ago

jepp112 commented 9 years ago

On the afterSave() method I get error saying Undefined index: dHeight..My JQuery version is 2.1.4. Please Help.

bupy7 commented 9 years ago

Give me code, please.

jepp112 commented 9 years ago

View

            <?= $form->field($model,'image')->widget(Cropbox::className(), [
                'attributeCropInfo' => 'crop_info',
             ]);
             ?>

Model

public function afterSave($insert, $changedAttributes) {

        // open image
        $image = Image::getImagine()->open($this->image->tempName);

        // rendering information about crop of ONE option 
        $cropInfo = Json::decode($this->crop_info)[0];
        $cropInfo['dWidth'] = (int)$cropInfo['dWidth']; //new width image
        $cropInfo['dHeight'] = (int)$cropInfo['dHeight']; //new height image
        $cropInfo['x'] = $cropInfo['x']; //begin position of frame crop by X
        $cropInfo['y'] = $cropInfo['y']; //begin position of frame crop by Y
        // Properties bolow we don't use in this example
//        $cropInfo['ratio'] = $cropInfo['ratio'] == 0 ? 1.0 : (float) $cropInfo['ratio']; //ratio image. 
//        $cropInfo['width'] = (int) $cropInfo['width']; //width of cropped image
//        $cropInfo['height'] = (int) $cropInfo['height']; //height of cropped image
//        $cropInfo['sWidth'] = (int) $cropInfo['sWidth']; //width of source image
//        $cropInfo['sHeight'] = (int) $cropInfo['sHeight']; //height of source image
        //delete old images
        $oldImages = FileHelper::findFiles(Yii::getAlias(Yii::$app->basePath . '/web/uploads'), [
                    'only' => [
                        $this->id . '.*',
                        'thumb_' . $this->id . '.*',
                    ],
        ]);
        for ($i = 0; $i != count($oldImages); $i++) {
            @unlink($oldImages[$i]);
        }

        //saving thumbnail
        $newSizeThumb = new Box($cropInfo['dWidth'], $cropInfo['dHeight']);
        $cropSizeThumb = new Box(200, 200); //frame size of crop
        $cropPointThumb = new Point($cropInfo['x'], $cropInfo['y']);
        $pathThumbImage = Yii::getAlias(Yii::$app->basePath . '/web/uploads')
                . '/thumb_'
                . $this->id
                . '.'
                . $this->image->getExtension();

        $image->resize($newSizeThumb)
                ->crop($cropPointThumb, $cropSizeThumb)
                ->save($pathThumbImage, ['quality' => 100]);

        //saving original
        $this->image->saveAs(
                Yii::getAlias(Yii::$app->basePath . '/web/uploads')
                . '/'
                . $this->id
                . '.'
                . $this->image->getExtension()
        );
    }

Controller

public function actionCreate() {
        $model = new Coaches();
        $signup = new SignupForm();

        if ($model->load(Yii::$app->request->post())) {

            $model->hasuser = 0;
            $model->image = \yii\web\UploadedFile::getInstance($model, 'image');

            if ($model->save()) {
                return $this->redirect(['index']);
            }
            print_r($model->getErrors());
            return $this->render('create', [
                        'model' => $model,
                        'signup' => $signup,
            ]);
        } else {
            return $this->render('create', [
                        'model' => $model,
                        'signup' => $signup,
            ]);
        }
    }

error

bupy7 commented 9 years ago

@xhepa112 attach, please, output of var_dump($this->crop_info) before save.

bupy7 commented 9 years ago

@xhepa112 which version of extension you use?

jepp112 commented 9 years ago

var_dump(this->crop_info) is returning NULL , I Installed extensin by "bupy7/yii2-widget-cropbox": "*",

in my exetensions.php

'bupy7/yii2-widget-cropbox' => 
  array (
    'name' => 'bupy7/yii2-widget-cropbox',
    'version' => '4.0.0.0',
    'alias' => 
    array (
      '@bupy7/cropbox' => $vendorDir . '/bupy7/yii2-widget-cropbox',
    ),
  ),
bupy7 commented 9 years ago

@xhepa112 attach content rules() method of model.

bupy7 commented 9 years ago

@xhepa112 you defined property public $crop_info; at model?

jepp112 commented 9 years ago

It was my bad not adding rules(). Thank you very much for your time :)

bupy7 commented 9 years ago

@xhepa112 don't worry. please =)