Example #1
0
/**
 * 抓取商品的图片到本地,并且自动生成缩率图
 *
 */
function fetchGoodsImage($goods_id, $imageUrl)
{
    global $f3;
    printLog('start to fetch goods_id [' . $goods_id . '] imageUrl[' . $imageUrl . ']');
    // 抓取图片,伪装成浏览器防止被某些服务器阻止
    $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']) {
        // 抓取失败,什么都不做
        printLog('can not fetch [' . $imageUrl . ']', 'fetchGoodsImage', \Core\Log\Base::ERROR);
        goto out_release_res;
    }
    // 上传目录
    $dataPathRoot = $f3->get('sysConfig[data_path_root]');
    $saveFilePath = $dataPathRoot . '/upload/image/' . date("Y/m/d");
    if (!file_exists($saveFilePath)) {
        if (!mkdir($saveFilePath, 0755, true)) {
            printLog('can not mkdir [' . $saveFilePath . ']', 'fetchGoodsImage', \Core\Log\Base::ERROR);
            goto out_release_res;
        }
    }
    //保存文件
    $saveFilePath .= '/' . date("YmdHis") . '_' . rand(1, 10000) . strtolower(strrchr($imageUrl, '.'));
    file_put_contents($saveFilePath, $request['body']);
    printLog('save to image : ' . $saveFilePath);
    // 保存 goods_gallery 记录
    $imageFileRelativeName = str_replace($dataPathRoot . '/', '', $saveFilePath);
    $pathInfoArray = pathinfo($imageFileRelativeName);
    $imageThumbFileRelativeName = $pathInfoArray['dirname'] . '/' . $pathInfoArray['filename'] . '_' . $f3->get('sysConfig[image_thumb_width]') . 'x' . $f3->get('sysConfig[image_thumb_height]') . '.jpg';
    //生成缩略图
    StorageImageHelper::resizeImage($dataPathRoot, $imageFileRelativeName, $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_url = $imageFileRelativeName;
    $goodsGallery->img_desc = '最土转化图片';
    $goodsGallery->img_original = $imageFileRelativeName;
    $goodsGallery->thumb_url = $imageThumbFileRelativeName;
    $goodsGallery->save();
    printLog('success fetch [' . $goods_id . '] [' . $imageUrl . ']', 'fetchGoodsImage');
    out_release_res:
    unset($request);
    unset($webInstance);
}
Example #2
0
 public function run(array $params)
 {
     global $f3;
     // 每次处理多少条记录
     $batchProcessCount = 100;
     // 图片所在的根目录
     $dataPathRoot = $f3->get('sysConfig[data_path_root]');
     $image_thumb_width = $f3->get('sysConfig[image_thumb_width]');
     $image_thumb_height = $f3->get('sysConfig[image_thumb_height]');
     $goodsGalleryService = new GoodsGalleryService();
     $baseService = new BaseService();
     $totalGoodsGalleryCount = $baseService->_countArray('goods_gallery', null);
     // 记录处理开始
     for ($offset = 0; $offset < $totalGoodsGalleryCount; $offset += $batchProcessCount) {
         $goodsGalleryArray = $baseService->_fetchArray('goods_gallery', '*', null, array('order' => 'img_id asc'), $offset, $batchProcessCount);
         foreach ($goodsGalleryArray as $goodsGalleryItem) {
             if (!is_file($dataPathRoot . '/' . $goodsGalleryItem['img_original'])) {
                 continue;
                 // 文件不存在,不处理
             }
             $pathInfoArray = pathinfo($goodsGalleryItem['img_original']);
             //生成缩略图
             $imageThumbFileRelativeName = $pathInfoArray['dirname'] . '/' . $pathInfoArray['filename'] . '_' . $image_thumb_width . 'x' . $image_thumb_height . '.jpg';
             //重新生存缩略图
             printLog('Re-generate File :' . $imageThumbFileRelativeName);
             StorageImageHelper::resizeImage($dataPathRoot, $goodsGalleryItem['img_original'], $imageThumbFileRelativeName, $image_thumb_width, $image_thumb_height);
             // 更新 goods_gallery 设置
             printLog('update goods_gallery img_id [' . $goodsGalleryItem['img_id'] . ']');
             $goodsGallery = $goodsGalleryService->loadGoodsGalleryById($goodsGalleryItem['img_id']);
             if (!$goodsGallery->isEmpty()) {
                 $goodsGallery->thumb_url = $imageThumbFileRelativeName;
                 $goodsGallery->save();
             }
             // 主动释放资源
             unset($goodsGallery);
             unset($pathInfoArray);
             unset($imageThumbFileRelativeName);
         }
         unset($goodsGalleryArray);
         printLog('re-generate thumb image offset : ' . $offset);
     }
     printLog('re-generate thumb image finished , offset : ' . $offset);
 }
Example #3
0
 /**
  * 从网络抓取图片进入相册
  *
  * @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');
 }