/** * 上传一张图片 * * @param $f3 */ public function Upload($f3) { // 权限检查 $this->requirePrivilege('manage_goods_edit_edit_post'); // 参数验证 $errorMessage = ''; $validator = new Validator($f3->get('POST')); $goods_id = $validator->required('商品ID不能为空')->digits()->min(1)->validate('goods_id'); if (!$this->validate($validator)) { $errorMessage = "param goods_id does not exist"; goto out_fail; } // 上传路径 $dataPathRoot = $f3->get('sysConfig[data_path_root]'); if (empty($dataPathRoot)) { $dataPathRoot = $f3->get('BASE') . '/data'; } // 上传路径对应的 URL 前缀 $dataUrlPrefix = $f3->get('sysConfig[data_url_prefix]'); if (empty($dataUrlPrefix)) { $dataUrlPrefix = $f3->get('BASE') . '/data'; } // 我们的文件上传操作全部采用 KindEditor 来做 $kindEditor = new \KindEditor\KindEditor(); $fileInfo = $kindEditor->doAction($dataPathRoot, $dataUrlPrefix, 'upload'); // 文件上传之后调用后续处理,生成缩略图 // 上传文件相对 dataPathRoot 的文件名 $imageOriginalFileRelativeName = $fileInfo['relativeName']; $pathInfoArray = pathinfo($imageOriginalFileRelativeName); //生成头图 $imageFileRelativeName = $pathInfoArray['dirname'] . '/' . $pathInfoArray['filename'] . '_' . $f3->get('sysConfig[image_width]') . 'x' . $f3->get('sysConfig[image_height]') . '.jpg'; StorageImageHelper::resizeImage($dataPathRoot, $imageOriginalFileRelativeName, $imageFileRelativeName, $f3->get('sysConfig[image_width]'), $f3->get('sysConfig[image_height]')); //生成缩略图 $imageThumbFileRelativeName = $pathInfoArray['dirname'] . '/' . $pathInfoArray['filename'] . '_' . $f3->get('sysConfig[image_thumb_width]') . 'x' . $f3->get('sysConfig[image_thumb_height]') . '.jpg'; StorageImageHelper::resizeImage($dataPathRoot, $imageOriginalFileRelativeName, $imageThumbFileRelativeName, $f3->get('sysConfig[image_thumb_width]'), $f3->get('sysConfig[image_thumb_height]')); //保存 goods_gallery 记录 $goodsGalleryService = new GoodsGalleryService(); // ID 为0,返回一个新建的 dataMapper $goodsGallery = $goodsGalleryService->_loadById('goods_gallery', 'img_id=?', 0); $goodsGallery->goods_id = $goods_id; $goodsGallery->img_desc = $fileInfo['originalName']; $goodsGallery->img_original = $imageOriginalFileRelativeName; $goodsGallery->img_url = $imageFileRelativeName; $goodsGallery->thumb_url = $imageThumbFileRelativeName; $goodsGallery->save(); //清除缓存,确保商品显示正确 ClearHelper::clearGoodsCacheById($goods_id); return; out_fail: // 失败从这里返回 AjaxHelper::header(); echo json_encode(array('error' => 1, 'message' => $errorMessage)); }
public function get($f3) { // 上传路径 $dataPathRoot = $f3->get('sysConfig[data_path_root]'); if (empty($dataPathRoot)) { $dataPathRoot = $f3->get('BASE') . '/data'; } // 上传路径对应的 URL 前缀 $dataUrlPrefix = $f3->get('sysConfig[data_url_prefix]'); if (empty($dataUrlPrefix)) { $dataUrlPrefix = $f3->get('BASE') . '/data'; } // 操作 $validator = new Validator($f3->get('GET')); $action = $validator->validate('action'); $kindEditor = new \KindEditor\KindEditor(); $kindEditor->doAction($dataPathRoot, $dataUrlPrefix, $action); }