/** * action UploadFile */ public function actionUploadFile() { $avatar = new Avatar(); $avatar->upload('avatar'); exit; $file = UploadedFile::getInstanceByName('myfile'); /* Image::thumbnail($file->tempName, 120, 120) ->save(Yii::getAlias('/tmp/test.jpg'), ['quality' => 80]); */ Image::crop($file->tempName, 100, 100, [100, 100])->save(Yii::getAlias('/tmp/test2.jpg'), ['quality' => 80]); echo 'good'; exit; /* $oss = new Oss(); $file = '/tmp/1455812025.jpg'; $obj = date('Ym/') . 'test.jpg'; $result = $oss->upload_by_file('cyj-i-test', $obj, $file); var_dump($result); */ }
/** * 图片上传处理 */ public function uploadFile($name, $options, $userId) { $mAvatar = new Avatar(); $file = UploadedFile::getInstanceByName($name); $extName = $file->getExtension(); $allowExtNames = ['jpg', 'jpeg', 'png', 'gif']; if (!in_array($extName, $allowExtNames)) { return 1; // 类型不符 } if ($file->size / 1024 > 4096) { return 2; //图片过大 } $avatar = $mAvatar->upload('avatarfile', $options); if (!empty($avatar)) { //存储图片 $mPicture = new UserImage(); $datas = ['uid' => $userId, 'url' => $avatar, 'type' => UserImage::USER_AVATAR, 'status' => UserImage::STATUS_USABLE]; $mPicture->store($datas); return $avatar; } else { //将失败的信息记录到日志 } }
/** * 修改个人信息 */ public function actionModify() { $this->layout = false; if (!$this->isLogin) { $this->redirect(Url::toRoute('user/login')); return false; } $mUser = new UserBaseInfo(); //获取登录用户信息 //$user = $mUser::getLoginUser(); $params = Yii::$app->request->post(); $params = array_map('trim', $params); $mAvatar = new Avatar(); $ocutpos = json_decode($params['ocutpos'], true); $options = ['pointer' => [$ocutpos['x'], $ocutpos['y']], 'size' => $ocutpos['w']]; try { $avatar = $mAvatar->upload('avatarfile', $options); if (!empty($avatar)) { //存储图片 $mPicture = new UserImage(); $datas = ['uid' => $params['id'], 'url' => $avatar, 'type' => UserImage::USER_AVATAR, 'status' => UserImage::STATUS_USABLE]; $picture = $mPicture->store($datas); } else { //将失败的信息记录到日志 FileLogger::getInstance(date('Ymd') . '_uc_profile.log')->writeOne($params['oavatar'], Logger::LEVEL_ERROR, 'image'); } } catch (\Exception $e) { // throw new \Exception('上传图片失败,请重试!!!'); } $session = Yii::$app->session; $cookies = Yii::$app->getResponse()->cookies; //session更新 $tmp = $mUser->getUserInfoCache(); if ($avatar) { $data['avatar'] = $avatar; if ($tmp) { $tmp['avatar'] = $avatar; } $cookie = new Cookie(['name' => 'avatar', 'value' => $avatar]); $cookies->add($cookie); } $tmp['name'] = $params['username']; $session[UserBaseInfo::SESSION_KEY_USER] = $tmp; //cookie更新 $cookie = new Cookie(['name' => 'avatar', 'value' => $avatar]); $cookie = new Cookie(['name' => 'username', 'value' => $params['username']]); $cookies->add($cookie); //4-20个字符,可全部由字母组成,或数字、字母、“_”、“-”任意两种以上组合 $preg = '/^((?!^\\d+$)(?!^\\-+$)(?!^\\_+$)[\\x{4e00}-\\x{9fa5}a-zA-Z0-9\\_\\-]{2,20})$/u'; $res = preg_match($preg, $params['username']); if (!$res) { echo "<script>alert('4-20个字符,可全部由字母组成,或数字、字母、“_”、“-”任意两种以上组合');history.go(-1);</script>"; return false; } $data['id'] = $params['id']; if (!empty($params['username'])) { $data['username'] = strip_tags($params['username']); } else { echo "<script>alert('昵称不能为空');history.go(-1);</script>"; return false; } $relname = preg_match($preg, $params['realname']); if (!$relname) { echo "<script>alert('4-20个字符,可全部由字母组成,或数字、字母、“_”、“-”任意两种以上组合');history.go(-1);</script>"; return false; } if (!empty($params['realname'])) { $data['realname'] = strip_tags($params['realname']); } else { echo "<script>alert('昵称不能为空');history.go(-1);</script>"; return false; } $data['gender'] = intval($params['gender']); $genderArr = [UserBaseInfo::GENDER_UNKNOWN, UserBaseInfo::GENDER_MALE, UserBaseInfo::GENDER_FEMALE]; if (!in_array($data['gender'], $genderArr)) { echo "<script>alert('性别非法修改');history.go(-1);</script>"; return false; } if (!is_numeric($params['year'])) { echo "<script>alert('年份非法修改');history.go(-1);</script>"; return false; } $month = str_pad(intval($params['month']), 2, "0", STR_PAD_LEFT); $day = str_pad(intval($params['day']), 2, "0", STR_PAD_LEFT); if (checkdate($month, $day, $params['year'])) { if ($params['year'] <= date('Y', time()) && $params['year'] >= 1950) { $data['birthday'] = $params['year'] . '-' . $month . '-' . $day; } else { echo "<script>alert('日期不在规定时间之内');history.go(-1);</script>"; return false; } } else { echo "<script>alert('非法日期');history.go(-1);</script>"; return false; } $params['province'] = intval($params['province']); $params['city'] = intval($params['city']); if (!$params['province']) { echo "<script>alert('请选择省');history.go(-1);</script>"; return false; } if (!$params['city']) { echo "<script>alert('请选择市');history.go(-1);</script>"; return false; } $params['province'] = intval($params['province']); $params['city'] = intval($params['city']); $region = District::getById($params['city'], true); if (!$region) { echo "<script>alert('城市信息不存在');history.go(-1);</script>"; return false; } $data['city'] = $params['city']; //用户行业配置 $industry = Yii::$app->util->loadConfig('user-industry', '/platform_i/config/'); $data['industry'] = $params['industry']; if (!isset($industry[$data['industry']])) { echo "<script>alert('行业信息不存在');history.go(-1);</script>"; return false; } $comStrLength = mb_strlen($params['company'], 'utf8'); $posStrLength = mb_strlen($params['position'], 'utf8'); if ($comStrLength < 0 || $comStrLength > 50) { echo "<script>alert('公司名称填写仅限于50个字以内');history.go(-1);</script>"; return false; } if ($posStrLength < 0 || $posStrLength > 30) { echo "<script>alert('职位填写仅限于30个字以内');history.go(-1);</script>"; return false; } $data['company'] = strip_tags($params['company']); $data['position'] = strip_tags($params['position']); $data['gender'] = $params['gender']; $this->syncProfile($data); //根据id获取用户修改前的信息 $oriUser = $mUser->getUserById($tmp['id'], UserBaseInfo::USER_NORMAL_STATUS, true); array_pop($oriUser); if ($mUser->updateUserById($data)) { //将成功信息记录到日志 $this->profileLog($oriUser, $data); echo "<script type='text/javascript'>window.location.href='" . Url::toRoute('user/profile') . "'</script>"; } else { //将失败的信息记录到日志 FileLogger::getInstance(date('Ymd') . '_uc_profile.log')->writeOne($params, Logger::LEVEL_ERROR, 'profile'); echo "<script type='text/javascript'>alert('修改失败');history.go(-1);</script>"; return false; } }