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 insertImg() { $this->assign("action", __FUNCTION__); if (IS_POST || IS_AJAX) { //上传图片到图片库 $db = M("Img"); $db->create(); $w = I('post.w', 0, 'intval'); $h = I('post.h', 0, 'intval'); if ($w > 0 && $h > 0) { $savepath = I('post.img'); $filepath = legalize_path($_SERVER['DOCUMENT_ROOT'] . $savepath); $savepath = cropImg($filepath, $savepath); $thumb_savepath = createthumb($filepath, $savepath); $db->img = $savepath; $db->thumb_img = $thumb_savepath; } $id = $db->add(); $itemImg = M("ItemImg"); $item_id = I('post.item_id', 0, 'intval'); // $size_id = I('post.size_id',0,'intval'); $view_id = I('post.view_id', 0, 'intval'); if ($id) { //插入图片到物品图片关联库 $data = array("item_id" => $item_id, "img_id" => $id, "size_id" => I('post.size_id', 0, 'intval'), "view_id" => I('post.view_id', 0, 'intval')); $itemImg->add($data); } //获取图片 // $map['size_id'] = $size_id; $map['view_id'] = $view_id; $map['item_id'] = $item_id; $db = D($this->_model); $item = $db->find($item_id); $size = $db->table("bc_item_size")->find($size_id); $view = $db->table("bc_item_view")->find($view_id); $imgs = $this->getImg($item_id, $size_id, $view_id); //模版操作 $this->assign("item", $item); $this->assign("size", $size); $this->assign("view", $view); $this->assign("imgs", $imgs); $this->redirect("insertImg", array("item_id" => $item_id), 1, "插入成功,正在跳转.."); $this->display("viewImg"); //显示图片 } else { $db = D('BestForBaby'); $item_id = I('get.item_id', 0, 'intval'); if ($item_id == 0) { $this->error('商品不存在', '/index.php/home/bestforbaby', 2); die; } else { $item = $db->relation(true)->find($item_id); $viewImgs = $this->getImg($item_id, $item['view'][0]['id']); $this->assign("item", $item); $this->assign("viewImgs", $viewImgs); $this->assign("itemView", $item['view']); $this->assign("itemSize", $item['size']); $this->display(); } } }