public function upload() { $x1 = I("x1"); $y1 = I("y1"); $upload = new \Think\Upload(); $upload->maxSize = 3145728; // 设置附件上传大小 $upload->exts = array('jpg', 'gif', 'png', 'jpeg'); // 设置附件上传类型 $upload->rootPath = 'Public/'; $upload->savePath = 'Uploads/'; $info = $upload->uploadOne($_FILES['member_pic']); if (!$info) { // 上传错误提示错误信息 $this->error($upload->getError()); } else { // 上传成功 获取上传文件信息 $member_pic = $info['savepath'] . $info['savename']; } $member_p = "./Public/" . $info['savepath'] . $info['savename']; $image = new \Think\Image(); $image->open($member_p); $image->crop(128, 128, $x1, $y1)->save($member_p); $memberdetail = M("Memberdetail"); if ($memberdetail->create()) { $memberdetail->member_pic = $member_pic; $memberdetail->save(); $_SESSION['member_pic'] = $member_pic; $this->success("头像修改成功!", U("avatar")); } else { $this->error("头像修改失败!", U("avatar")); } }
function upload_one_thumb($param, $w = 680, $h = 250) { $upload = new \Think\Upload(); $upload->maxSize = 2097152; //字节 1KB=1024字节 默认为2M $upload->exts = array('jpg', 'gif', 'png', 'jpeg'); $upload->savePath = $param['savePath']; //保存路径 相对路径 $upload->subName = $param['subName']; //子目录 $upload->saveName = $param['saveName']; //保存名称 $upload->saveExt = $param['saveExt']; //保存后缀 $upload->replace = true; //存在同名的文件 覆盖 $info = $upload->uploadOne($param['files']); if (!$info) { return 'error'; } else { $img_src = './Uploads/' . $info['savepath'] . $info['savename']; $image = new \Think\Image(); $image->open($img_src); //将图片裁剪为680X250并保存为 $new_img_src = './Uploads/' . $info['savepath'] . 'thumb_' . $info['savename']; $image->crop($w, $h)->save($new_img_src); return $info['savepath'] . $info['savename']; //return $info['savepath'].'thumb_'.$info['savename']; } }
/** * 图片裁剪 * @param int $src_x 原图裁剪点的x坐标 * @param int $src_y 原图裁剪点的y坐标 * @param int $width 裁剪宽度 * @param int $height 裁剪高度 * @param int $save_width 保存宽度 * @param int $save_height 保存高度 * @param int $attachment_id 裁剪附件ID * @return array 裁剪后图片 * @author 李志亮 <*****@*****.**> */ public function crop(int $src_x, int $src_y, int $width, int $height, int $save_width, $save_height, int $attachment_id) { if (empty($width) || empty($height) || empty($attachment_id)) { $this->errorCode = 10001; $this->errorMessage = '参数不合法'; return false; } $attachment_model = new AttachmentModel(); $attach_origin = $attachment = $attachment_model->find($attachment_id); if (empty($attachment) || !file_exists(UPLOAD_PATH . $attachment['path'])) { $this->errorCode = 10010; $this->errorMessage = '附件不存在'; return false; } try { $image_size = getimagesize(UPLOAD_PATH . $attachment['path']); } catch (Exception $e) { $this->errorCode = 10040; $this->errorMessage = '原图信息获取失败!'; return false; } // 计算缩放比 $ratio = $image_size[0] / C('IMAGE_CROP_WIDTH'); $src_x = intval($src_x * $ratio); $src_y = intval($src_y * $ratio); $width = intval($width * $ratio); $height = intval($height * $ratio); try { // 图片裁剪处理 $image = new \Think\Image(1, UPLOAD_PATH . $attachment['path']); $name = uniqid() . '.' . $attachment['ext']; $des_path = str_replace($attachment['name'], $name, UPLOAD_PATH . $attachment['path']); $image->crop($width, $height, $src_x, $src_y)->save($des_path); $attachment['url'] = str_replace($attachment['name'], $name, $attachment['url']); $attachment['path'] = str_replace($attachment['name'], $name, $attachment['path']); $attachment['name'] = $name; // 缩略图处理 $thumb_obj = new \Think\Image(1, UPLOAD_PATH . $attachment['path']); $thumb_name = uniqid() . '.' . $attachment['ext']; $thumb_des_path = str_replace($attachment['name'], $thumb_name, UPLOAD_PATH . $attachment['path']); $thumb_obj->thumb($save_width, $save_height, 2)->save(str_replace($attachment['name'], $thumb_name, UPLOAD_PATH . $attachment['path'])); $thumb['url'] = str_replace($attachment['name'], $thumb_name, $attachment['url']); $thumb['path'] = str_replace($attachment['name'], $thumb_name, $attachment['path']); $thumb['name'] = $thumb_obj; return array('origin' => $attach_origin, 'crop' => $attachment, 'thumb' => $thumb); } catch (Exception $e) { $this->errorCode = 10012; $this->errorMessage = '图片裁剪失败'; return false; } }
public function crop($file, $x, $y, $width, $height) { $image = new \Think\Image(); $image->open('.' . $file); //将图片裁剪为$widthx$height并保存为$tmp_file $tmp_file = date('Y_m_d_h_i_s') . '.' . $image->type(); $local_file = './Uploads/tmp/' . $tmp_file; $image->crop($width, $height, $x, $y)->save($local_file); if (!is_file($local_file)) { $this->error('生成裁剪文件失败'); } $res = local_upload($local_file, 'Picture'); if ($res['status']) { $this->success('裁剪成功', '', $res); } else { $this->error($res['error']); } }
/** * 头像保存 * */ public function save() { $imgX = I('post.imgX'); $imgY = I('post.imgY'); $imgW = I('post.imgW'); $imgH = I('post.imgH'); $path = I('post.path'); if ($imgX == '' || $imgY == '' || $imgW == '' || $imgH == '' || $path == '') { $this->ajaxReturn("数据为空"); } $image = new \Think\Image(); $file = './Public' . $path; //trace($file); $image->open($file); $status = $image->crop($imgW, $imgH, $imgX, $imgY)->save($file); if ($status) { $this->ajaxReturn(TRUE); } else { $msg = "保存失败"; $this->ajaxReturn($msg); } }
public function updateFace() { // var_dump($_POST); $model = D('user'); // var_dump($_SESSION); $face = $_POST['name']; $face = trim($face, '"'); $arr = explode('/', $face); $path = $arr['5']; $name = $arr['6']; // echo $face; // var_dump($_POST['name']); $open = 'Public/Uploads/User/' . $path . '/' . $name; // echo $open; $cut_save = 'Public/Uploads/User/' . $path . '/cut_' . $name; $small_save = 'Public/Uploads/User/' . $path . '/small_' . $name; $model->image = '/Uploads/User/' . $path . '/cut_' . $name; $image = new \Think\Image(); $image->open($open); $image->thumb(110, 110, \Think\Image::IMAGE_THUMB_FILLED)->save($small_save); $image->open($small_save); $image->crop(100, 100, \Think\Image::IMAGE_THUMB_CENTER)->save($cut_save); if ($model->where("id={$this->userId}")->save($data)) { echo 'true'; } else { echo 'false'; } }
public function logoCrop() { if (I('get.p')) { $x1 = intval(I('post.x1', 0)); $y1 = intval(I('post.y1', 0)); $w = intval(I('post.w', 100)); $h = intval(I('post.h', 100)); $src = session('?thumb') ? session('thumb') : null; $image = new \Think\Image(); $image->open($src); $filename = explode('.', $src); $filename = '.' . $filename[1] . '_thumb.png'; $thumbName = $filename; $result = $image->crop($w, $h, $x1, $y1)->save($thumbName); if ($result) { $filename = explode('.', session('crop')); $filename = $filename[0] . '_thumb.png'; $Form = new Model(); $success = $Form->execute('update project_info set project_logo="%s" where project_id="%s"', $filename, I('get.p')); session('crop', null); session('thumb', null); echo 200; } } }
public function profileCrop() { $x1 = intval(I('post.x1', 0)); $y1 = intval(I('post.y1', 0)); $w = intval(I('post.w', 100)); $h = intval(I('post.h', 100)); if (session('?type') && session('?id')) { // 上传成功 $src = session('?thumb') ? session('thumb') : null; $image = new \Think\Image(); $image->open($src); $filename = explode('.', $src); $filename = '.' . $filename[1] . '_thumb.png'; $thumbName = $filename; $result = $image->crop($w, $h, $x1, $y1)->save($thumbName); if ($result) { $Form = new Model(); $filename = explode('.', session('crop')); $filename = $filename[0] . '_thumb.png'; if ($_SESSION['type'] == "1") { $success = $Form->execute('update investor_personal set portrait="%s" where user_id="%s"', $filename, $_SESSION['id']); } else { $success = $Form->execute('update entrepreneur_personal set portrait="%s" where user_id="%s"', $filename, $_SESSION['id']); } session('crop', null); session('thumb', null); session('portrait', $filename); echo 200; } } else { echo 401; } }
public function modifyAvater() { if (!IS_POST) { $this->error('非法请求', U('Index/index')); } // 获取用户设置的头像坐标 $coordsW = I('post.coords_w'); $coordsH = I('post.coords_h'); $coordsX = I('post.coords_x'); $coordsY = I('post.coords_y'); // 获取图片缩放后宽高 $imagesW = intval(I('post.images_w')); $imagesH = intval(I('post.images_h')); // 设置上传参数 $rootPath = './Uploads/Avater/'; $upload = new \Think\Upload(); // 实例化上传类 $upload->maxSize = 2097152; // 设置附件上传大小 $upload->exts = array('jpg', 'gif', 'png', 'jpeg'); // 设置附件上传类型 $upload->rootPath = $rootPath; // 设置附件上传根目录 $upload->autoSub = false; // 是否自动创建子目录 $info = $upload->upload(); // 上传错误提示错误信息 if (!$info) { $this->ajaxReturn(get_ajax_res(0, '头像上传失败:' . $upload->getError())); } // 获取头像图片信息 $avaterName = $info['avater']['savename']; $avaterPath = $rootPath . $avaterName; // 进行图像裁剪 $imageObj = new \Think\Image(); $res = $imageObj->open($avaterPath); // 设置临时图像路径和新头像路径 $imageType = $imageObj->type(); $tempAvater = $rootPath . 'avater_temp.' . $imageType; $newAvaterName = $avaterName . '_thubm.' . $imageType; $newAvater = $rootPath . $newAvaterName; // 先生成临时头像缩略图 $imageObj->thumb($imagesW, $imagesH)->save($tempAvater); $imageObj->open($tempAvater); // 后进行头像裁剪 $res = $imageObj->crop($coordsW, $coordsH, $coordsX, $coordsY)->save($newAvater); if ($res) { // 成功后删除原图片和临时图片 unlink($avaterPath); unlink($tempAvater); $userDB = M('user'); $map = array('id' => session('UID')); $data = array('avater_path' => $newAvaterName); $res = $userDB->where($map)->setField($data); if ($res) { session('avaterPath', $newAvaterName); } $this->ajaxReturn(get_ajax_res(1, '头像上传成功')); } else { $this->ajaxReturn(get_ajax_res(0, '头像上传失败,请重试')); } }
public function cut() { if (!($uid = is_login())) { $this->error("您还没有登陆,请先登陆", U("/"), 2); } $id = $_POST["couponid"]; $coupon = M("usercoupon"); if ($coupon->where("uid='{$uid}' and couponid='{$id}'")->select()) { $data["msg"] = "已领取过"; $data["status"] = "0"; $this->ajaxreturn($data); } else { $data["uid"] = $uid; $data["couponid"] = $id; $data["time"] = NOW_TIME; $data["status"] = "1"; $data["info"] = "未使用"; $coupon->add($data); $data["msg"] = "已成功领取,请刷新查看"; $this->ajaxreturn($data); } $uid = I("get.id"); $cut = M("ucenter_member")->where("id='{$uid}'")->select(); $this->assign('cut', $cut); $images = new \Think\Image(); if ($_POST['pic']) { //$src=C('DOMAIN').$_POST["pic"]; $src = $_POST['pic']; $images->open($src); $name = time() . $src; $x = $_POST["x"]; $y = $_POST["y"]; $w = $_POST["w"]; $h = $_POST["h"]; $s = $images->crop(400, 400, 100, 30)->save('./' . $name); echo $s; } $this->meta_title = '修改图像'; $this->display(); }
public function cropImgEnd() { $image = new \Think\Image(); $image_url = $_POST['img_url']; $view = $_POST['view']; $image->open('./Uploads/' . $image_url); $image->crop($_POST['cropw'], $_POST['croph'], $_POST['cropx'], $_POST['cropy'])->save('./Uploads/' . $image_url); $this->success('裁切成功!', __ROOT__ . '/admin.php/Content/' . $view); }
public function cut() { $uid = I("get.id"); $cut = M("member")->where("uid='{$uid}'")->select(); $this->assign('cut', $cut); $images = new \Think\Image(); if ($_POST['pic']) { //$src=C('DOMAIN').$_POST["pic"]; $src = $_POST['pic']; $images->open($src); $name = time() . $src; $x = $_POST["x"]; $y = $_POST["y"]; $w = $_POST["w"]; $h = $_POST["h"]; $s = $images->crop(400, 400, 100, 30)->save('./' . $name); echo $s; } $this->meta_title = '修改图像'; $this->display(); }
/** * 裁剪图片 * @author jry <*****@*****.**> */ public function crop($data = null) { $image = new \Think\Image(); $image->open($data['src']); $type = $image->type(); if ($image) { $file = './Runtime/Temp/crop' . \Org\Util\String::randString(12, 1) . '.' . $type; $url = U(MODULE_MARK . "/Upload/upload", null, true, true); // 图片缩放计算 $sw = $sh = 1; if ($data['vw']) { $sw = $image->width() / $data['vw']; } if ($data['vh']) { $sh = $image->height() / $data['vh']; } // 裁剪并保存 $image->crop($data['w'] * $sw, $data['h'] * $sh, $data['x'] * $sh, $data['y'] * $sh)->save($file); $result = $this->curlUploadFile($url, $file); return json_decode($result, true); } }
/** * 其他信息编辑 */ public function head() { if (IS_POST) { $img_dir = str_ireplace(C('IMG_URL'), C('IMG_ROOT'), $_POST['filePath']); $img_dir = preg_replace("/\\?.*/is", '', $img_dir); $image = new \Think\Image(1, $img_dir); $b_img = str_ireplace('logo', 'b_logo', $img_dir); $image->crop(200, 200, $_POST['x'], $_POST['y'])->save($b_img); $m_img = str_ireplace('logo', 'm_logo', $img_dir); $image->thumb(120, 120)->save($m_img); $s_img = str_ireplace('logo', 's_logo', $img_dir); $image->thumb(48, 48)->save($s_img); if (file_exists($s_img)) { $this->success('上传成功'); } else { $this->error('上传失败'); } exit; } else { $img_dir = C('IMG_ROOT') . 'avatar/' . get_dir($_GET['id']) . '/m_logo.jpg'; if (file_exists($img_dir)) { $img = C('IMG_URL') . 'avatar/' . get_dir($_GET['id']) . '/m_logo.jpg'; } $this->assign('img', $img); } $this->display(); }
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("头像更新失败!"); } } }
M('zhaopian')->where(array('id' => $item['id']))->save(array('create_time' => $time)); $path = $root_path . "/uploads/" . $item['pic_url']; echo $path . "\r\n"; if (file_exists($path) && is_file($path)) { if (!file_exists($path . '_thumb.jpg')) { $img = new \Think\Image(2); $img->open($path); $width = $img->width(); $height = $img->height(); $x = $y = 0; if ($width > $height) { $x = floor(($width - $height) / 2); $width = $height; } elseif ($height > $width) { $y = floor(($height - $width) / 2); $height = $width; } $img->crop($width, $height, $x, $y, 300, 300)->save($path . '_thumb.jpg', 'jpg'); } if (!file_exists($path . '_thumb2.jpg')) { $img = new \Think\Image(2); $img->open($path); $img->thumb(500, 1000)->save($path . '_thumb1.jpg', 'jpg'); if (file_exists($path . '_thumb1.jpg')) { $img = new \Think\Image(2); $img->open($path . '_thumb1.jpg')->gaussianBlurImage(40, 36)->save($path . '_thumb2.jpg', 'jpg'); } } M('zhaopian')->where(array('id' => $item['id']))->save(array('is_create' => 1)); } } while (true);
public function jcorp() { $res = array('code' => 2, 'msg' => ''); if (IS_AJAX) { $post = I('post.'); $pic = __ROOT__ . '.' . $post['url']; $image = new \Think\Image(); $image->open($pic); $image->crop($post['w'], $post['h'], $post['x'], $post['y'])->save($pic); $image->thumb(100, 100, \Think\Image::IMAGE_THUMB_FILLED)->save($pic); $res['code'] = 1; $res['data'] = __ROOT__ . substr($pic, 1); echo json_encode($res); die; } else { $res['msg'] = '访问出错'; die(json_encode($res)); } }
public function Cropavatar() { $img = I("img"); $x = I("x1"); $y = I("y1"); $w = I("width"); $h = I("height"); echo "." . $img; if (file_exists("." . $img)) { $image = new \Think\Image(); $image->open("." . $img); $image->crop($w, $h, $x, $y)->save('./Uploads/avatar_img/' . UID . '.jpg'); $p['avatar'] = './Uploads/avatar_img/' . UID . '.jpg'; D('Member')->where("uid='" . UID . "'")->save($p); $this->redirect('avatar', array("msg", "保存成功")); } }
public function cropimg() { if (IS_AJAX) { $img = I('post.img'); $img = str_replace(__ROOT__ . '/', '', $img); $image = new \Think\Image(); $image->open($img); $pathinfo = pathinfo($img); $savepath = $pathinfo['dirname'] . '/' . $pathinfo['filename'] . '_' . I('post.tw') . '_' . I('post.th') . '.' . $pathinfo['extension']; $b = $image->crop(I('post.w'), I('post.h'), I('post.x'), I('post.y'), I('post.tw'), I('post.th'))->save($savepath); $data = array(); if ($b) { $data = array('error' => '', 'pic' => __ROOT__ . '/' . $savepath, 'picpath' => $savepath); } else { $data = array('error' => 'error'); } echo json_encode($data); } }
/** * 头像截图上传 * @date: 2016-01-05 上午03:25:58 * @author: zhouqg * @param: $path:文件保存路径; * @return: */ function upload_photo($attr, $path = 'Images') { $upload = new \Think\Upload(); // 实例化上传类 $upload->maxSize = $size; // 设置附件上传大小 $upload->exts = explode(',', C('UPLOAD_PICEXTS')); //图片上传类型 $upload->rootPath = './Uploads/'; // 设置附件上传根目录 $upload->savePath = '/' . $path . '/'; // 网站主栏目图片 设置附件上传(子)目录 缩略图 $upload->autoSub = true; $upload->subName = array('date', 'Ymd'); $upload->saveName = array('uniqid', ''); //设置上传文件规则 $info = $upload->upload(); if (!$info) { // 上传错误提示错误信息 $rtn['success'] = '0'; //表示失败 $rtn['error'] = $upload->getError(); } else { // 上传成功 foreach ($info as $file) { $image = __ROOT__ . '/' . 'Uploads' . $file['savepath'] . $file['savename']; $imgurl = './' . 'Uploads' . $file['savepath'] . $file['savename']; //读取截图用相对路径 } $rtn['success'] = '1'; //表示成功 $rtn['url'] = $image; //文件的路径 /** * 截图上传 */ $img = new \Think\Image(); $img->open($imgurl); $img->crop($attr['dataWidth'], $attr['dataHeight'], $attr['dataX'], $attr['dataY'])->save($imgurl); } return $rtn; }
public function helpImage($upload) { //如果使用了多语言功能的话(假设,我们在当前语言包里面定义了'lang_var'=>'标题必须!'),就可以这样定义模型的自动验证 //array('title','require','{%lang_var}',1), //'FILE_FORMAT ' => '文件格式: {$format},文件大小:{$size}', //{:L('FILE_FORMAT ',array('format' => 'jpeg,png,gif,jpg','maximum' => '2MB'))} // 采用时间戳命名 $upload->saveName = 'time'; // 采用GUID序列命名 $upload->saveName = 'com_create_guid'; // 采用自定义函数命名 $upload->saveName = 'myfun'; // 开启子目录保存 并以日期(格式为Ymd)为子目录 $upload->autoSub = true; $upload->subName = array('date', 'Ymd'); $image = new \Think\Image(); $image->open('./1.jpg'); $width = $image->width(); // 返回图片的宽度 $height = $image->height(); // 返回图片的高度 $type = $image->type(); // 返回图片的类型 $mime = $image->mime(); // 返回图片的mime类型 $size = $image->size(); // 返回图片的尺寸数组 0 图片宽度 1 图片高度 //裁剪图片 $image = new \Think\Image(); $image->open('./1.jpg'); //将图片裁剪为400x400并保存为corp.jpg $image->crop(400, 400)->save('./crop.jpg'); //使用thumb方法生成缩略图 $image = new \Think\Image(); $image->open('./1.jpg'); // 按照原图的比例生成一个最大为150*150的缩略图并保存为thumb.jpg $image->thumb(150, 150)->save('./thumb.jpg'); //居中裁剪 $image = new \Think\Image(); $image->open('./1.jpg'); // 生成一个居中裁剪为150*150的缩略图并保存为thumb.jpg $image->thumb(150, 150, \Think\Image::IMAGE_THUMB_CENTER)->save('./thumb.jpg'); //添加图片水印 $image = new \Think\Image(); $image->open('./1.jpg'); //将图片裁剪为440x440并保存为corp.jpg $image->crop(440, 440)->save('./crop.jpg'); // 给裁剪后的图片添加图片水印(水印文件位于./logo.png),位置为右下角,保存为water.gif $image->water('./logo.png')->save("water.gif"); // 给原图添加水印并保存为water_o.gif(需要重新打开原图) $image->open('./1.jpg')->water('./logo.png')->save("water_o.gif"); //给图片添加文字水印 $image = new \Think\Image(); // 在图片右下角添加水印文字 T hinkPHP 并保存为new.jpg $image->open('./1.jpg')->text('T hinkPHP', './1.ttf', 20, '#000000', \Think\Image::IMAGE_WATER_SOUTHEAST)->save("new.jpg"); }