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

File does not exist. #29

Closed yinsimlee closed 7 years ago

yinsimlee commented 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.

bupy7 commented 7 years ago

I didn't understood that you have done. It is very strange. =)