/** * * @param string $logFileName 日志文件名 */ function __construct($logFileName = null) { // 取得云平台 日志模块 $this->cloudEngineLog = CloudHelper::getCloudModule(CloudHelper::CLOUD_MODULE_Log); // 初始化日志引擎 $this->cloudEngineLog->initLog($logFileName); }
/** * 构造函数 * * 一个配置的简单例子 * * $config = array( * "savePath" => $dataPathRoot . '/upload/image', * "pathFix" => $dataPathRoot, * 'urlPrefix' => $dataUrlPrefix, * "maxSize" => 1000, //单位KB * "allowFiles" => array(".gif", ".png", ".jpg", ".jpeg") * ); * * @param string $fileField 表单名称 * @param array $config 配置项 * @param bool $base64 是否解析base64编码,可省略。若开启,则$fileField代表的是base64编码的字符串表单名 */ public function __construct($fileField, $config, $base64 = false) { // 取得云引擎的 Storage 模块 $this->cloudStorage = CloudHelper::getCloudModule(CloudHelper::CLOUD_MODULE_STORAGE); $this->fileField = $fileField; $this->config = $config; $this->stateInfo = $this->stateMap[0]; $this->upFile($base64); }
/** * 取得数据库引擎,如果数据库引擎没有初始化,我们这里会做初始化 */ public static function getDbEngine() { if (null == static::$dbEngine) { global $f3; // 初始化全站数据库 static::$tablePrefix = $f3->get('sysConfig[db_table_prefix]'); // 获得云平台的数据库引擎 $cloudModuleDb = CloudHelper::getCloudModule(CloudHelper::CLOUD_MODULE_DB); static::$dbEngine = $cloudModuleDb->getDb(); } return static::$dbEngine; }
/** * 从网络抓取图片进入相册 * * @param $f3 */ public function Fetch($f3) { // 权限检查 $this->requirePrivilege('manage_goods_edit_edit_post'); // 参数验证 $validator = new Validator($f3->get('POST')); $goods_id = $validator->required('商品ID不能为空')->digits()->min(1)->validate('goods_id'); $imageUrl = $validator->required('图片地址不能为空')->validate('imageUrl'); if (!$this->validate($validator)) { goto out_fail; } // 抓取图片,伪装成浏览器防止被某些服务器阻止 $webInstance = \Web::instance(); $webInstance->engine('curl'); $request = $webInstance->request($imageUrl, array('user_agent' => 'Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; Trident/4.0; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729)')); if (!$request || isset($request['http_code']) && 200 != $request['http_code']) { $this->addFlashMessage('抓取失败,请检查你的抓取地址'); goto out; } // 把图片保存到 Storage 中 $cloudStorage = CloudHelper::getCloudModule(CloudHelper::CLOUD_MODULE_STORAGE); // 图片文件先保存到临时文件中 $tempSrcFilePath = $cloudStorage->getTempFilePath(); file_put_contents($tempSrcFilePath, $request['body']); // 上传目录 $dataPathRoot = $f3->get('sysConfig[data_path_root]'); $saveFilePathRelative = 'upload/image/' . date("Y/m/d") . '/' . date("YmdHis") . '_' . rand(1, 10000) . strtolower(strrchr($imageUrl, '.')); // 文件上传到 Storage if (!$cloudStorage->moveFileToStorage($dataPathRoot, $saveFilePathRelative, $tempSrcFilePath)) { $this->addFlashMessage('保存文件到存储失败,失败'); goto out; } @unlink($tempSrcFilePath); // 保存 goods_gallery 记录 $imageOriginalFileRelativeName = $saveFilePathRelative; $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 = '网络下载图片'; $goodsGallery->img_original = $imageOriginalFileRelativeName; $goodsGallery->img_url = $imageFileRelativeName; $goodsGallery->thumb_url = $imageThumbFileRelativeName; $goodsGallery->save(); $this->addFlashMessage('抓取图片成功'); //清除缓存,确保商品显示正确 ClearHelper::clearGoodsCacheById($goodsGallery->goods_id); out: // 释放资源 unset($request); unset($webInstance); RouteHelper::reRoute($this, RouteHelper::makeUrl('/Goods/Edit/Gallery', array('goods_id' => $goods_id), true)); return; // 成功从这里返回 out_fail: RouteHelper::reRoute($this, '/Goods/Search'); }
/** * 生成一个新的尺寸的图片,并且保存到缓存目录里面 * * @param string $dataPathRoot 数据存放的根目录 * @param string $imageFileRelativeName 源图片文件的相对路径名,相对于 $dataPathRoot * @param int $width * @param int $height * @param bool $crop 如果图片长宽比例不合适,是否剪切掉 * * @return string 返回相对于 $dataPathRoot 的新文件路径 */ public static function cropImageIfNotExist($dataPathRoot, $imageFileRelativeName, $width, $height) { $cloudStorage = CloudHelper::getCloudModule(CloudHelper::CLOUD_MODULE_STORAGE); $soureFilePath = $dataPathRoot . '/' . $imageFileRelativeName; // 源文件不存在,返回空 if (!$cloudStorage->fileExists($dataPathRoot, $imageFileRelativeName)) { printLog('[' . $soureFilePath . '] does not exist', __CLASS__, \Core\Log\Base::ERROR); return ''; } // 自动生成缩率文件,放在 Cache 目录下 $pathInfoArray = pathinfo($imageFileRelativeName); $targetDirPathRelative = static::$cacheDirName . '/' . $pathInfoArray['dirname']; $targetFilePathRelative = $targetDirPathRelative . '/' . $pathInfoArray['filename'] . '_' . $width . 'x' . $height . '_' . 'crop' . '.' . $pathInfoArray['extension']; if ($cloudStorage->fileExists($dataPathRoot, $targetFilePathRelative)) { goto out; } self::cropImage($dataPathRoot, $imageFileRelativeName, $targetFilePathRelative, $width, $height); printLog('crop [' . $soureFilePath . '] to [' . $dataPathRoot . '/' . $targetFilePathRelative . '] width [' . $width . '] height [' . $height . ']', __CLASS__); out: return $targetFilePathRelative; }