function crop($w, $h, $x, $y, $url) { $rootPath = C('FACE_PATH'); //图片地址 $bigFacePath = $rootPath . session('auth')['id'] . '_200*200.jpg'; $smallFacePath = $rootPath . session('auth')['id'] . '_50*50.jpg'; //裁剪图片 $imgHandler = new Image(); $imgHandler->open($url); $imgHandler->crop($w, $h, $x, $y); //保存大图 $imgHandler->thumb(200, 200)->save($bigFacePath); //保存小图 $imgHandler->thumb(50, 50)->save($smallFacePath); $pathArr = array(small => $smallFacePath, big => $bigFacePath); return $pathArr; }
/** *上传userInfo到数据库 */ public function userInfo() { $upload = new \Think\Upload(); $upload->maxSize = 1000000; $upload->exts = array('jpg', 'png', 'jpeg'); $upload->rootPath = './Public/Bzone/images/head-pic/'; $upload->savePath = ''; $upload->replace = 'true'; $upload->saveName = 'time'; $info = $upload->upload(); if (!$info) { $this->error($upload->getError()); } else { foreach ($info as $file) { $image = new Image(); $image->open('./Public/Bzone/images/head-pic/' . $file['savepath'] . $file['savename']); $width = $image->width(); $height = $image->height(); if ($width <= 130 || $height <= 130) { $image->crop(130, 130)->save('./Public/Bzone/images/head-pic/' . $file['savepath'] . $file['savename']); } else { $image->thumb(130, 130)->save('./Public/Bzone/images/head-pic/' . $file['savepath'] . $file['savename']); } $data['info_pic'] = $file['savepath'] . $file['savename']; $data['info_selfintroduce'] = I('selfInfo'); if (M('userinfo')->where(array('user_id' => session('userId')))->save($data)) { redirect(U('Bzone/showFabu', '', '')); } else { $this->error('上传失败'); } } } }
public function img() { //实例化图像处理类,默认为GD库 $image = new Image(); //加载一张预处理的图片 $image->open('./Public/images/1.jpg'); //上面两句,可以用一句话包含 //$image = new Image(Image::IMAGE_GD, './Public/images/1.jpg'); //获取图片信息 $arr['width'] = $image->width(); $arr['height'] = $image->height(); $arr['type'] = $image->type(); $arr['mime'] = $image->mime(); $arr['size'] = $image->size(); //裁剪图片,高400,宽400 $image->crop(400, 400)->save('./Public/images/1.jpg'); $image->open('./Public/images/2.jpg'); $image->thumb(300, 300, Image::IMAGE_THUMB_CENTER)->save('./Public/images/2.jpg'); //在图片右下角添加水印并生成 $image->open('./Public/images/3.jpg'); $image->water('./Public/images/logo.png')->save('./Public/images/3.jpg'); echo '<pre>' . print_r($arr, 1) . '</pre>'; }
function cropImg($filepath, $webpath) { $image = new \Think\Image(); $filepath = legalize_path($filepath); var_dump($filepath); $image->open($filepath); $savepath = $filepath; $x = I("post.x1", 0, 'intval'); $y = I("post.y1", 0, 'intval'); $w = I("post.w", 0, 'intval'); $h = I("post.h", 0, 'intval'); if ($w > 0 && $h > 0) { $image->crop($w, $h, $x, $y, $w, $h)->save($savepath); } echo "<img src='{$webpath}' />"; $savepath = $webpath; //dump($webpath); //dump($thumb_savepath); return $savepath; }
public function crop() { $path = './Public/images/demo.jpg'; $image = new Image(Image::IMAGE_GD, $path); $image->crop(200, 200)->save('./Public/images/demo-crop-200x200.jpg'); }
public function changePhoto() { if (IS_POST) { $id = is_login(); $basepath = substr($_POST['basepath'], 1); $image = new Image(); $image->open($basepath); $image->crop($_POST['w'], $_POST['h'], $_POST['x'], $_POST['y'], 200, 200); $last = strrpos($basepath, '/') + 1; $ext = substr($basepath, strrpos($basepath, '.')); $path = substr($basepath, 0, $last); $filename = $path . 'crop_' . $id . $ext; $image->save($filename, null, 100, false); $filename = '/' . $filename . '?t=' . NOW_TIME; $user = array('id' => is_login(), 'photo' => $_POST['photo'], 'photo_url' => $filename); M('Users')->save($user); memberupdate($id, $user['photo_url']); $ret = array('status' => 1, 'info' => '头像更改成功!', 'photo_url' => $filename, 'photo' => $_POST['photo']); $this->ajaxReturn($ret); $this->success('头像更改成功!'); } else { $this->display('changePhoto'); } }