yii2-crop-image-upload
yii2-crop-image-upload copied to clipboard
image is saving without cropping.
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?
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
Ok thnx a lot. I am checking it again.
Dear Karpoff , so what should I do to save the image, because I am already attaching the CropImageUploadBehavior to the model
you just need to save model please see actionEdit here
Ok, so nice of u. thanks a lot.