Closed yinsimlee closed 7 years ago
Hi , Thank for your sharing, i had met some problem, can help me fix ? Thank you!
(view) <?= $form->field($model,'member_profile')->widget(Cropbox::classname(),[ 'attributeCropInfo' => 'crop_info', ] ); ?>
(model) public function afterSave($insert, $changedAttributes) { // open image $image = Image::getImagine()->open($this->image->tempName); $cropInfo = Json::decode($this->cropinfo)[0]; $cropInfo['dWidth'] = (int)$cropInfo['dWidth']; $cropInfo['dHeight'] = (int)$cropInfo['dHeight']; $cropInfo['x'] = $cropInfo['x']; $cropInfo['y'] = $cropInfo['y']; $oldImages = FileHelper::findFiles(Yii::getAlias('@path/to/save/image'), [ '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('@path/to/save/image') . '/thumb_' . $this->id . '.' . $this->image->getExtension();
$image->resize($newSizeThumb) ->crop($cropPointThumb, $cropSizeThumb) ->save($pathThumbImage, ['quality' => 100]); //saving original $this->image->saveAs( Yii::getAlias('@path/to/save/image') . '/' . $this->id . '.' . $this->image->getExtension() );
}
(controller) public function actionView($id) { $model = $this->findModel($id); $this-> layout = 'surveyformlayout'; $oldpic = $model->member_profile; if ($model->load(Yii::$app->request->post())) { $imageName = date("Ymdhis"); $model->file = UploadedFile::getInstance($model,'member_profile'); if(!empty($model->file)) { $model->file->saveAs('../../backend/web/profile/'.$imageName.'.'.$model->file->extension); //save the path in the db column $model->member_profile = 'profile/'.$imageName.'.'.$model->file->extension; } else { $model->member_profile = $oldpic; } $model->updated_at = date("Y-m-d h:i:s"); if($model->save()) { Yii::$app->session->setFlash('success', 'Add Successful'); return $this->redirect(['view','id'=>$model->id]); } else { return $this->render('view', [ 'model' => $model, ]); } } else{ return $this->render('view', [ 'model' => $model, ]); } }
It cannot save the cropped image , but can update new image.
I didn't understood that you have done. It is very strange. =)
Hi , Thank for your sharing, i had met some problem, can help me fix ? Thank you!
(view) <?= $form->field($model,'member_profile')->widget(Cropbox::classname(),[ 'attributeCropInfo' => 'crop_info', ]
); ?>
(model) public function afterSave($insert, $changedAttributes) { // open image $image = Image::getImagine()->open($this->image->tempName); $cropInfo = Json::decode($this->cropinfo)[0]; $cropInfo['dWidth'] = (int)$cropInfo['dWidth']; $cropInfo['dHeight'] = (int)$cropInfo['dHeight']; $cropInfo['x'] = $cropInfo['x']; $cropInfo['y'] = $cropInfo['y']; $oldImages = FileHelper::findFiles(Yii::getAlias('@path/to/save/image'), [ '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('@path/to/save/image') . '/thumb_' . $this->id . '.' . $this->image->getExtension();
}
(controller) public function actionView($id) { $model = $this->findModel($id); $this-> layout = 'surveyformlayout'; $oldpic = $model->member_profile; if ($model->load(Yii::$app->request->post())) { $imageName = date("Ymdhis"); $model->file = UploadedFile::getInstance($model,'member_profile'); if(!empty($model->file)) { $model->file->saveAs('../../backend/web/profile/'.$imageName.'.'.$model->file->extension); //save the path in the db column $model->member_profile = 'profile/'.$imageName.'.'.$model->file->extension; } else { $model->member_profile = $oldpic; } $model->updated_at = date("Y-m-d h:i:s"); if($model->save()) { Yii::$app->session->setFlash('success', 'Add Successful'); return $this->redirect(['view','id'=>$model->id]); } else { return $this->render('view', [ 'model' => $model, ]); }
} else{ return $this->render('view', [ 'model' => $model, ]); }
}
It cannot save the cropped image , but can update new image.