yii2-crop-image-upload
yii2-crop-image-upload copied to clipboard
help me
When I use widget I face a following issue:
- It shows on view(interface) is ok
- When I submit data, ‘photo’ field return NULL
- My action to submit named ‘actionPost’.
- I do not understand about code lines ‘on’=>[‘insert’,’update’] inside ‘rules()’ function and ‘scenarios’=>[‘insert’,’update’] inside ‘behaviors()’ function is to do what ?
- And I do not also understand how to get return data with ‘photo’, ‘crop_field’, ‘cropper_field’ from actionPost() to check validation ? Please help me this . Thanks.
Hi hphuoclam
‘on’=>[‘insert’,’update’] means that this validation rule will work only for 'insert' and 'update' scenarios ‘scenarios’=>[‘insert’,’update’] means the same for CropImageUploadBehavior so to make uploading files work you should properly set behavior name in your post action for example
public function actionPost()
{
$model = new MyCropModel();
$model->scenario = 'insert';
if ($model->load(Yii::$app->request->post())) {
$model->save();
}
}
not sure that I've understand correctly what do you mean in last question all validation and image proceed will be done automatically and after that $model->photo will contain image filename (it will be original image name if cropped_field is set or cropped image name if not) crop_field is optional (as well as cropped_field). it is needed if you want to store original and cropped image separately
feel free to ask any questions you have
hi, why when I submit it always returns Null photo?
please check https://github.com/karpoff/yii2-test-examples I've added an example how you can work with crop widget
I have the same problem, when form is submited, NULL value is returned in photo field. The fields crop_field and cropped_field are ok, but the attribute value is null, this attribute should save the name of the image? or should save the path to image?
Congrats for the extension..it's really god!
I have solved the problem. I don't using 'scenarios', and in the beforeValidate function of UploadBehavior checks the array of 'scenarios', i have deleted this if to adjust to my Model.
Now all is ok! Thanks!
maestre19, I suggest you not to change extension files It's better to add 'scenarios' => ['default'] to behavior properties in Model file I've added default scenario as default value so if you just remove scenario property it will work
glad to see that my extension is usefull :)
Need help !!!
- When I update image, cropped_field not update in DB
- require photo on update (How to skip it)
- JPG after crop is low quality.
iyoba, have you checked https://github.com/karpoff/yii2-test-examples ? can you share code where you use this behavior somewhere (model and controller where you update it)?
Actually I haven't checked image quality. I believe that it should be the same as for original image. I'll check it
Yes, I check example already, here in my model
/** * @inheritdoc */ public function rules() { return [ [['name', 'logo'], 'required'], [['name'], 'string', 'max' => 50], [['name'], 'unique'], [['logo_crop', 'logo_cropped'], 'string', 'max' => 100], ['logo', 'file', 'extensions' => 'jpg, jpeg, png', 'on' => ['insert', 'update']], ]; }
/**
* @inheritdoc
*/
function behaviors()
{
return [
[
'class' => CropImageUploadBehavior::className(),
'attribute' => 'logo',
'scenarios' => ['insert', 'update'],
'path' => '@frontend/web/upload/brands',
'url' => '@frontend/upload/brands',
'ratio' => 1,
'crop_field' => 'logo_crop',
'cropped_field' => 'logo_cropped',
],
];
}
In DB
- When insert new data field logo => 5507daea81761.png field logo_cropped => 5507daea81761_5507daea81761.png
- Then update data field logo => 5507db603ccbf.jpg field logo_cropped => 5507daea81761_5507daea81761.png
For image quality I suggested to see CropImageUploadBehavior.php line 144 when save Image, should have option for imagine library.
please also share contoller code you use for saving data
Here in my controller
/** * Creates a new Brand model. * If creation is successful, the browser will be redirected to the 'view' page. * @return mixed */ public function actionCreate() { $model = new Brand(); $model->scenario = 'insert';
if ($model->load(Yii::$app->request->post()) && $model->save()) {
return $this->redirect(['view', 'id' => $model->id]);
} else {
return $this->render('create', [
'model' => $model,
]);
}
}
/**
* Updates an existing Brand model.
* If update is successful, the browser will be redirected to the 'view' page.
* @param integer $id
* @return mixed
*/
public function actionUpdate($id)
{
$model = $this->findModel($id);
$model->scenario = 'update';
if ($model->load(Yii::$app->request->post()) && $model->save()) {
return $this->redirect(['view', 'id' => $model->id]);
} else {
return $this->render('update', [
'model' => $model,
]);
}
}
iyoba, this should be fixed now for setting image quality use save_options behavior attribute (for separate crop or for all)
http://imagine.readthedocs.org/en/latest/usage/introduction.html#save-images here is docs how it can configured
Oh.., Thanks a lot :)
hi how i can manage a null pic? really i don't want to make mandatory my attribute, but if i skip it, i get an error :1. in /vendor/imagine/imagine/lib/Imagine/Gd/Imagine.php at line 98
Hi I can't catch this error but I've added some additional checks to prevent it could you check it?