public function ajaxAction() { // 获取dir参数 $dir = Request::getGET('dir', 'image'); if (!array_key_exists($dir, self::$ALLOWED)) { echo json_encode(array('error' => 1, 'message' => '参数错误!')); return; } // 获取上传的文件 $field = 'imgFile'; $fileSize = Upload::getFilesize($field); $tmpName = Upload::getTmpName($field); $fileExt = Upload::getFileExt($field); if (empty($fileSize)) { echo json_encode(array('error' => 1, 'message' => '请上传文件!')); return; } // 校验格式 if (!in_array($fileExt, self::$ALLOWED[$dir])) { echo json_encode(array('error' => 1, 'message' => '文件格式不支持,无法上传!')); return; } // 保存 $cdnKey = Cdn::uploadLocalFile($tmpName, $this->loginUserInfo['id'], $fileExt); $url = Cdn::getUrl($cdnKey); echo json_encode(array('error' => 0, 'url' => $url)); }
public static function getHeadUrl($cdnKey, $sex) { if (empty($cdnKey)) { return $sex == UcUserModelVars::SEX_FEMALE ? '//sta.hqoj.net/image/www/oj/head/default-girl.jpg' : '//sta.hqoj.net/image/www/oj/head/default-boy.jpg'; } return Cdn::getUrl($cdnKey); }
/** * Get the configuration * @return array */ protected function getListOf() { if (Cdn::isOnline() && $this->offline) { return []; } /** @var ConfigLoader $loader */ $loader = \Yii::createObject($this->config['configLoaderClass']); $loader->online($this->getPath()); return $loader->asArray(); }
public function ajaxUploadAction() { $field = 'file'; $fileExt = Upload::getFileExt($field); $fileSize = Upload::getFilesize($field); $tmpFile = Upload::getTmpName($field); if (!in_array($fileExt, array('png', 'jpeg', 'jpg', 'gif', 'bmp'))) { $this->echoJson(1, '只允许jpg,jpeg,bmp,png,gif格式的图片!'); } if ($fileSize > 102400) { $this->echoJson(1, '图片大小不能超过100KB!'); } // 保存头像 $cdnKey = Cdn::uploadLocalFile($tmpFile, $this->loginUserInfo['id'], $fileExt); UserCommonInterface::save(array('id' => $this->loginUserInfo['id'], 'head_img' => $cdnKey)); // 删除原先的头像 Cdn::delete($this->loginUserInfo['head_img']); $this->echoJson(0, 'Success!', array('src' => OjCommonHelper::getHeadUrl($cdnKey, $this->loginUserInfo['sex']))); }