头像上传预览,最好可以剪裁的,谁有相关代码呀? [ 2.0 版本 ]
我想做一个用户头像上传预览的功能代码
视图:
<?php $form = ActiveForm::begin(['enableAjaxValidation' => false,'action' => ['user/regist'],'options' => ['enctype' => 'multipart/form-data']]); ?>
<?= $form->field($model, 'username')->textInput(['class'=>'menk form-control menk-vtcl']) ?>
<?= $form->field($model, 'password')->passwordInput(['class'=>'menk form-control menk-vtcl']) ?>
<?= $form->field($model, 'password_compare')->passwordInput(['class'=>'menk form-control menk-vtcl']) ?>
<?= $form->field($model, 'email')->textInput(['class'=>'menk form-control menk-vtcl']) ?>
<?= $form->field($model, 'sex')->radioList(['男'=>'ᠡᠷᠡᠭᠲᠡᠢ','女'=>'ᠡᠮᠡᠭᠲᠡᠢ']) ?>
<?= $form->field($model, 'num')->textInput(['class'=>'menk form-control menk-vtcl'])?>
<?= $form->field($model, 'img')->fileInput(['class'=>'menk form-control menk-vtcl']) ?>
<?= Html::submitButton('ᠳᠡᠪᠰᠢᠭᠦᠯᠬᠦ',['class'=>'menk btn btn-primary-outline menk-vtcl']) ?>
<?= Html::resetButton('ᠲᠠᠬᠢᠨ ᠪᠢᠴᠢᠬᠦ',['class'=>'menk btn btn-primary-outline menk-vtcl']) ?>
<?php $form=ActiveForm::end();?>
模型中:
public function rules()
{
return [
[['username','img'],'required','message'=>'ᠬᠣᠭᠣᠰᠡᠨ ᠪᠠᠶᠢᠵᠦ ᠪᠣᠯᠬᠣᠷ ᠦᠭᠡᠢ'],
['username','filter','filter'=>'trim'],
['username','unique','targetClass'=>'\app\models\user\User','message'=>' ᠡᠨᠡ ᠨᠡᠷᠡ ᠲᠠᠩᠰᠠᠯᠵᠦ ᠦᠩᠭᠡᠷᠰᠡᠨ᠂ ᠦᠪᠡᠷ ᠨᠡᠷᠡ ᠠᠪᠠᠭᠠᠷᠠᠢ︕'],
//密码比较
[['password','password_compare'],'required','message'=>' ᠨᠣᠮᠧᠷ ᠬᠣᠭᠣᠰᠡᠨ ᠪᠠᠶᠢᠵᠦ ᠪᠣᠯᠬᠤ ᠦᠭᠡᠢ'],
[['password'],'match','pattern'=>'/^[A-Za-z0-9]{4,99}$/','message'=>' ᠨᠣᠮᠧᠷ ᠠᠩᠭᠢᠯᠢ ᠶᠡᠬᠡ ᠪᠠᠭᠠ᠂ ᠲᠣᠭᠠ᠂ 4 ᠪᠣᠯᠥᠨ99 ᠣᠷᠣᠨ ᠲᠠᠢ ᠲᠣᠭᠠ'],
['password_compare','compare','compareAttribute'=>'password','message'=>' ᠬᠣᠶᠠᠷ ᠨᠣᠮᠧᠷ ᠡᠷᠬᠡᠪᠴᠢ ᠨᠢᠭᠡᠲᠦᠯᠭᠡ ᠲᠠᠢ ᠪᠠᠶᠢᠬᠤ ᠬᠡᠷᠡᠭᠲᠡᠢ'],
['email', 'email','message'=> 'ᠬᠠᠶᠠᠭ ᠲᠠᠭᠠᠷᠰᠡᠨ ᠦᠭᠡᠢ︕'],
[['sex'],'required','message'=> 'ᠬᠣᠭᠣᠭᠰᠠᠨ ᠪᠠᠶᠢᠵᠦ ᠦᠭᠡᠢ︕'],
[['num'],'match','pattern'=>'/^13(\d{9})$|^15(\d{9})$|^189(\d{8})$/','message'=> 'ᠭᠠᠷ ᠣᠲᠠᠰᠣ ᠶᠢᠨ ᠵᠦᠪ ᠪᠢᠴᠢᠭᠡᠷᠡᠢ'],
[['img'], 'file'],
];
}
public function signup()
{
if ($this->validate()) {
echo '1212';
exit();
$user = new User();
$user->username = $this->username;
$user->setPassword($this->password);
$user->email=$this->email;
$user->num=$this->num;
$user->sex=$this->sex;
$user->create_time=gmdate('Y-m-d H:m:s',time()+8*3600);
//$user->img=saveAs('uploads/' . $this->img->baseName . '.' . $this->img->extension);
//$user->img=$this->img;
if ($user->save()) {
$roles=new U_R();
$save=$roles->save_roles($user->id);
if(!empty($save)){
return $user;
}else{
echo '用户没有保存等级';
exit();
}
}
}
return null;
}
控制器:
public function actionRegist(){
$model = $this->RegistForm();
$model->load($_POST);
if (Yii::$app->request->isAjax) {
Yii::$app->response->format = \yii\web\Response::FORMAT_JSON;
return \yii\bootstrap\ActiveForm::validate($model);
}
if (Yii::$app->request->post()) {
$model->img = UploadedFile::getInstances($model, 'img');
//$ext = getExtension($model->img);
//$imageName = time().rand(100,999).'.'.$ext;
//$image->saveAs('uploads/' . $model->img->baseName . '.' . $model->img->extension); //设置图片的存储位置
//$model->img = 'http://www.xxx.cn/uploads/'.$imageName;//设置图片的url,可以用_SERVER["HTTP_HOST"]+图片名代替
var_dump($model->img) ;//string(10) "ad_sys.png" NULL
exit();
if($user = $model->signup()){
//$user->img=$model->img->saveAs('uploads/' . $model->img->baseName . '.' . $model->img->extension);
if(Yii::$app->getUser()->login($user)){
return $this->redirect(['user/index']);
}
}
}
return $this->render('regist', ['model' => $model,]);
}
就一直保存不上,测试了很多种,很多遍,谁有更好的方法或者代码可以分享一下呀??
共 1 个回答
SaSa
注册时间:2016-03-28
最后登录:2017-10-15
在线时长:11小时23分
最后登录:2017-10-15
在线时长:11小时23分
- 粉丝9
- 金钱55
- 威望0
- 积分165