function avatar_update() { if (!empty($_SESSION['avatar'])) { $targ_w = intval($_POST['w']); $targ_h = intval($_POST['h']); $x = $_POST['x']; $y = $_POST['y']; $jpeg_quality = 90; $avatar = $_SESSION['avatar']; $avatar_dir = C("UPLOADPATH") . "avatar/"; if (sp_is_sae()) { //TODO 其它存储类型暂不考虑 $src = C("TMPL_PARSE_STRING.__UPLOAD__") . "avatar/{$avatar}"; } else { $src = $avatar_dir . $avatar; } $avatar_path = $avatar_dir . $avatar; if (sp_is_sae()) { //TODO 其它存储类型暂不考虑 $img_data = sp_file_read($avatar_path); $img = new \SaeImage(); $size = $img->getImageAttr(); $lx = $x / $size[0]; $rx = $x / $size[0] + $targ_w / $size[0]; $ty = $y / $size[1]; $by = $y / $size[1] + $targ_h / $size[1]; $img->crop($lx, $rx, $ty, $by); $img_content = $img->exec('png'); sp_file_write($avatar_dir . $avatar, $img_content); } else { $image = new \Think\Image(); $image->open($src); $image->crop($targ_w, $targ_h, $x, $y); $image->save($src); } $userid = sp_get_current_userid(); $result = $this->users_model->where(array("id" => $userid))->save(array("avatar" => $avatar)); $_SESSION['user']['avatar'] = $avatar; if ($result) { $this->success("头像更新成功!"); } else { $this->error("头像更新失败!"); } } }
/** * 编辑 */ public function yhedit_post() { $id = $_REQUEST['id']; $sort = $_POST['sort']; $bank_name = $_POST['bank_name']; $state = $_POST['state']; if ($sort == '') { $this->error("排序不能为空!"); } if ($bank_name == '') { $this->error("银行名称不能为空!"); } $config = array('FILE_UPLOAD_TYPE' => sp_is_sae() ? "Sae" : 'Local', 'rootPath' => './' . C("UPLOADPATH"), 'savePath' => './file/', 'maxSize' => 2097152, 'saveName' => array('uniqid', ''), 'exts' => array('jpg', 'png', 'jpeg'), 'autoSub' => false); $upload = new \Think\Upload($config); // $info = $upload->upload($_FILES); $data = array("sort" => $sort, "bank_name" => $bank_name, "bank_img" => 'file/' . $info['bank_img']['savename'], "state" => strtr($state, ",", ",")); $model = M("yinhang"); if ($model->where("id='{$id}'")->save($data)) { $this->success("修改成功!", U('xitong/yinhang')); } else { $this->error("修改失败!"); } }
function sp_file_read($file) { if (sp_is_sae()) { $s = new SaeStorage(); $arr = explode('/', ltrim($file, './')); $domain = array_shift($arr); $save_path = implode('/', $arr); return $s->read($domain, $save_path); } else { file_get_contents($file); } }
private function _get_remote_image() { $source = array(); if (isset($_POST['source'])) { $source = $_POST['source']; } else { $source = $_GET['source']; } $item = array("state" => "", "url" => "", "size" => "", "title" => "", "original" => "", "source" => ""); $date = date("Ymd"); //远程抓取图片配置 $config = array("savePath" => './' . C("UPLOADPATH") . "ueditor/{$date}/", "allowFiles" => array(".gif", ".png", ".jpg", ".jpeg", ".bmp"), "maxSize" => 3000); $list = array(); foreach ($source as $imgUrl) { $return_img = $item; $return_img['source'] = $imgUrl; $imgUrl = htmlspecialchars($imgUrl); $imgUrl = str_replace("&", "&", $imgUrl); //http开头验证 if (strpos($imgUrl, "http") !== 0) { $return_img['state'] = $this->stateMap['ERROR_HTTP_LINK']; array_push($list, $return_img); continue; } //获取请求头 if (sp_is_sae()) { //SAE下无效 $heads = get_headers($imgUrl); //死链检测 if (!(stristr($heads[0], "200") && stristr($heads[0], "OK"))) { $return_img['state'] = $this->stateMap['ERROR_DEAD_LINK']; array_push($list, $return_img); continue; } } //格式验证(扩展名验证和Content-Type验证) $fileType = strtolower(strrchr($imgUrl, '.')); if (!in_array($fileType, $config['allowFiles']) || stristr($heads['Content-Type'], "image")) { $return_img['state'] = $this->stateMap['ERROR_HTTP_CONTENTTYPE']; array_push($list, $return_img); continue; } //打开输出缓冲区并获取远程图片 ob_start(); $context = stream_context_create(array('http' => array('follow_location' => false))); //请确保php.ini中的fopen wrappers已经激活 readfile($imgUrl, false, $context); $img = ob_get_contents(); ob_end_clean(); //大小验证 $uriSize = strlen($img); //得到图片大小 $allowSize = 1024 * $config['maxSize']; if ($uriSize > $allowSize) { $return_img['state'] = $this->stateMap['ERROR_SIZE_EXCEED']; array_push($list, $return_img); continue; } //创建保存位置 $savePath = $config['savePath']; if (!file_exists($savePath)) { mkdir("{$savePath}", 0777); } $file = uniqid() . strrchr($imgUrl, '.'); //写入文件 $tmpName = $savePath . $file; $file = C("TMPL_PARSE_STRING.__UPLOAD__") . "ueditor/{$date}/" . $file; if (strpos($file, "https") === 0 || strpos($file, "http") === 0) { } else { //local $host = (is_ssl() ? 'https' : 'http') . "://" . $_SERVER['HTTP_HOST']; $file = $host . $file; } if (sp_file_write($tmpName, $img)) { $return_img['state'] = 'SUCCESS'; $return_img['url'] = $file; array_push($list, $return_img); } else { $return_img['state'] = $this->stateMap['ERROR_WRITE_CONTENT']; array_push($list, $return_img); } } return json_encode(array('state' => count($list) ? 'SUCCESS' : 'ERROR', 'list' => $list)); }