Exemplo n.º 1
0
 public function avatar()
 {
     $uid = $this->request->get('uid');
     $size = $this->request->get('s');
     if (!is_numeric($uid)) {
         Typecho_Common::error(404);
         exit;
     }
     $path = __TYPECHO_ROOT_DIR__ . Widget_Common::getAvatarPath($uid);
     $path .= $uid . '.jpg';
     if (!is_file($path)) {
         $path = __TYPECHO_ROOT_DIR__ . '/usr/avatar/default.jpg';
     }
     require_once __TYPECHO_ROOT_DIR__ . '/var/Util/Image.php';
     $image = new Image();
     $image->open($path);
     $type = $image->type();
     if (is_numeric($size)) {
         $image->thumb($size, $size);
     }
     header('Content-Type:image/' . $type . ';');
     //输出图像
     if ('jpeg' == $type || 'jpg' == $type) {
         // 采用jpeg方式输出
         imagejpeg($image->showImg());
     } elseif ('gif' == $type) {
         imagegif($image->showImg());
     } else {
         $fun = 'image' . $type;
         $fun($image->showImg());
     }
 }
Exemplo n.º 2
0
 /**
  * 上传头像
  * @param array $file
  * @return boolean|multitype:string unknown number Ambigous <string, unknown> Ambigous <Ambigous, string, mixed>
  */
 private function uploadAvatar($file)
 {
     if (empty($file['name'])) {
         return false;
     }
     $ext = $this->getSafeName($file['name']);
     if (!Widget_Upload::checkFileType(strtolower($ext)) || Typecho_Common::isAppEngine()) {
         return false;
     }
     $options = Typecho_Widget::widget('Widget_Options');
     $path = Widget_Common::getAvatarPath($this->user->uid);
     $realPath = Typecho_Common::url($path, defined('__TYPECHO_UPLOAD_ROOT_DIR__') ? __TYPECHO_UPLOAD_ROOT_DIR__ : __TYPECHO_ROOT_DIR__);
     //创建上传目录
     if (!is_dir($realPath)) {
         if (!$this->makeAvatarDir($realPath)) {
             return false;
         }
     }
     //获取文件名
     $fileName = $this->user->uid . '.tmp.' . $ext;
     $realPath = $realPath . '/' . $fileName;
     if (isset($file['tmp_name'])) {
         //移动上传文件
         if (!@move_uploaded_file($file['tmp_name'], $realPath)) {
             return false;
         }
     } else {
         if (isset($file['bytes'])) {
             //直接写入文件
             if (!file_put_contents($realPath, $file['bytes'])) {
                 return false;
             }
         } else {
             return false;
         }
     }
     if (!isset($file['size'])) {
         $file['size'] = filesize($realPath);
     }
     //返回相对存储路径
     return array('name' => $file['name'], 'path' => $path, 'file' => $path . $fileName, 'size' => $file['size'], 'type' => $ext, 'mime' => Typecho_Common::mimeContentType($realPath));
 }