/** * 手机端照片上传 * @param array $contentData 二进制数据 * @param array $infoData * @param string $type 上传类型 * @param string $flag 上传标识(1日记,2广播,3联系人,4头像,5群组封面) */ public function upload($contentData, $infoData, $type, $flag) { $user_id = $this->uid; if ($type = 'wp') { //获取相册ID $album_id = $this->db->getOne("wp_album", "album_id", "user_id = {$user_id} AND album_flag = {$flag}"); if (!$album_id) { $data = array("album_name" => '广播相册', "user_id" => $user_id, "create_dt" => time(), "update_dt" => time(), "album_flag" => $flag); $album_id = $this->db->insertData("wp_album", $data); } $file_md5 = md5($contentData); //上传到FS include 'application/include/FastDFS.php'; $this->fs = FastDFS::factory('group1'); //FS存储地址 $img_fs_name = $this->fs->upByBuff($contentData, 'jpg'); //写TTServer $value = array("pic_fs_path" => $img_fs_name, "exif" => "", "height" => $infoData['height'], "width" => $infoData['width']); $this->TT = new TTServer(); $this->TT->set($file_md5, $value); //插入wp表数据 $pic_data_array['album_id'] = $album_id; $pic_data_array['user_id'] = $user_id; $pic_data_array['create_time'] = time(); $pic_data_array['update_time'] = time(); $pic_data_array['upload_ip'] = $_SERVER['REMOTE_ADDR']; $pic_data_array['upload_file_name'] = 'sj.jpg'; $pic_data_array['file_md5'] = $file_md5; $pic_data_array['file_size'] = $infoData['size']; $pic_data_array['file_type'] = $infoData['type']; $pic_data_array['pic_width'] = $infoData['width']; $pic_data_array['pic_height'] = $infoData['height']; $pic_id = $this->db->insertData("wp_pic", $pic_data_array); if ($pic_id) { $fid = $pic_id + 100000; if ($flag == 2) { //广播返回图片地址不同 return array("data" => Kohana::config('album.recordThumb') . 'imgs/' . $pic_id . '_160.jpg', "fid" => $fid, "md5" => $file_md5); } else { return array("data" => Kohana::config('album.thumb') . 'imgs/' . $pic_id . '_160.jpg', "fid" => $fid, "md5" => $file_md5); } } else { return null; } } }
public function getAvatar($user_id, $size = 'middle') { $user_id = $user_id ? $user_id : $this->uid; if ($size == 'small') { $type = 48; } else { $type = 120; } //获取用户头像照相册ID $result = $this->db->getRow("album_user_album", "album_id", "album_default = '2' AND user_id={$user_id}"); $album_id = $result['album_id']; if (!$album_id) { return null; } //获取图片md5 $file_md5 = $this->db->getOne("album_pic", "file_md5", "album_id={$album_id} AND is_avatar = '1' ORDER BY pic_id DESC"); if ($file_md5) { //获取FS地址 $this->TT = new TTServer(); $pic_fs = $this->TT->get($file_md5); if ($pic_fs['pic_fs_path']) { //获取缩略图的FS路径 include 'application/include/FastDFS.php'; $this->fs = FastDFS::factory('group1'); $fsfile_name_array = explode('.', $pic_fs['pic_fs_path']); $thumb_path = $fsfile_name_array[0] . '_' . $type . '.' . $fsfile_name_array[1]; //获取文件数据流 $thumb = $this->fs->downToBuff($thumb_path); $file_size = strlen($thumb); if ($file_size) { return $thumb; } else { return null; } } } else { //没有设置头像,返回默认头像 return null; } }
public function uploadFileEnd($temp_id, $flag) { $uid = $this->uid; $data = $this->db->getRow("album_upload_temp", "file_title, file_md5, file_size, file_offset, file_path, file_type", "temp_id={$temp_id} AND user_id={$uid}"); if (!$data) { return array("code" => 403, "msg" => "uploadID不存在或无权限"); } //if($data['file_size'] != $data['file_offset']) { // return array("code" => 403, "msg" => "文件大小不匹配."); //} //$tempfsize = 0; //if( file_exists(ALBUM_TEMP.$data['file_path']) ) { // $tempfsize = filesize(ALBUM_TEMP.$data['file_path']); //} //if(!$tempfsize || $tempfsize != $data['file_size']) { // return array("code" => 403, "msg" => "文件大小不匹配!"); //} //移动临时文件到文件目录 if (!is_dir(ALBUM_IMAGE . $uid)) { mkdir(ALBUM_IMAGE . $uid, 0777); } //临时文件移动到user文件夹下 $tempfname = substr($data['file_path'], 0, -4); rename(ALBUM_TEMP . $data['file_path'], ALBUM_IMAGE . $tempfname); //移动文件成功 if (file_exists(ALBUM_IMAGE . $tempfname)) { $file_url = ALBUM_IMAGE . $tempfname; //获取图片的EXIF信息 $exif = Exif::get_exif_info($file_url); $img_exif = $exif['exif_info']; $degree = $exif['degree']; //获取图片的高度、宽度 $img = getimagesize($file_url); $width = $img[0]; $height = $img[1]; $file_format = $img[2]; if (!$img || !$width || !$height || $file_format > 6) { return false; } if ($file_format == 2 && $degree) { //旋转图片 $rotateDeg = $degree * 90; Image::rotate_by_degree($file_url, $rotateDeg); if ($degree == 1 || $degree == 3) { $t = $width; $width = $height; $height = $t; } $img_exif['degree'] = 0; $degree = 0; } $flag = $flag ? $flag : 2; //广播标识 $album_id = $this->db->getOne("wp_album", "album_id", "user_id = {$uid} AND album_flag={$flag}"); if (!$album_id) { //创建广播相册 $album_data = array('album_name' => '广播相册', 'user_id' => $uid, 'create_dt' => time(), 'update_dt' => time(), 'album_flag' => $flag); $album_id = $this->db->insertData("wp_album", $album_data); } $pic_info = array('album_id' => $album_id, 'user_id' => $uid, 'create_time' => time(), 'update_time' => time(), 'upload_ip' => $_SERVER['REMOTE_ADDR'], 'file_md5' => $data['file_md5'], 'file_size' => $data['file_size'], 'file_type' => $data['file_type'], 'pic_width' => $width, 'pic_height' => $height, 'degree' => intval($degree), 'appid' => 1); //上传到FS include 'application/include/FastDFS.php'; $this->fs = FastDFS::factory('group1'); //FS存储地址 $img_fs_name = $this->fs->upByFileName($file_url); //写TTServer $value = array("pic_fs_path" => $img_fs_name, "exif" => serialize($img_exif), "width" => $width, "height" => $height); $this->TT = new TTServer(); $this->TT->set($data['file_md5'], $value); $pic_id = $this->db->insertData("wp_pic", $pic_info); $this->db->query("UPDATE wp_album SET pic_num = pic_num+1 WHERE album_id={$album_id}"); //删除临时表数据 $this->db->DeleteData("album_upload_temp", array("temp_id" => $temp_id)); //生成160 $images = Image::createThumb_by_type($file_url, 160); $thumb_fs_name = substr($img_fs_name, 0, -4); $meta = array('remote_filename' => $thumb_fs_name . '_160.jpg'); $this->fs->upByFileName($images, $meta); if (file_exists($images)) { unlink($images); } return array("pid" => $pic_id, "url" => Kohana::config('album.recordThumb') . 'imgs/' . $pic_id . '_160.jpg', "uploaded" => true); } else { //移动文件失败 return null; } }