Ejemplo n.º 1
0
 /**
  * 获取微信media并保存
  * @param str $mediaID  media_id
  * @param str $path 目录
  * @return bool
  */
 public static function getWechatMedia($mediaID, $savePath = null)
 {
     if (empty($savePath)) {
         $savePath = 'order/' . time() . '_' . rand(1000, 9999) . ".jpg";
     }
     $weObj = new \System\lib\Wechat\Wechat(\System\Entrance::config("WEIXIN_CONFIG"));
     $media = $weObj->getMedia($mediaID);
     if (!$media) {
         return false;
     }
     $temPath = '../TempImg/';
     //临时保存路径
     if (!dirExists($temPath)) {
         return false;
     }
     $fileName = basename($savePath);
     /* $fileName=basename($savePath);
        $checkPath=str_replace($fileName, '', $savePath);
        if(!dirExists($checkPath)) return false; */
     if (!file_put_contents($temPath . $fileName, $media)) {
         return false;
     }
     //生成压缩图片
     $image = new \System\lib\Image\Image($temPath . $fileName);
     $img1 = $image->size();
     $return['img1']['path'] = $temPath . $fileName;
     $return['img1']['width'] = $img1[0];
     $return['img1']['heigh'] = $img1[1];
     //200*200缩略图
     $img2Path = $temPath . str_replace('.', '_thumb200.', $fileName);
     $image->thumb(200, 10000)->save($img2Path);
     $img2 = $image->size();
     $return['img2']['path'] = $img2Path;
     $return['img2']['width'] = $img2[0];
     $return['img2']['heigh'] = $img2[1];
     //360*360缩略图
     $img3Path = $temPath . str_replace('.', '_thumb360.', $fileName);
     $image->open($temPath . $fileName);
     $image->thumb(360, 10000)->save($img3Path);
     $img3 = $image->size();
     $return['img3']['path'] = $img3Path;
     $return['img3']['width'] = $img3[0];
     $return['img3']['heigh'] = $img3[1];
     //dump($return);
     //上传
     foreach ($return as $key => $a) {
         $upload = httpPost(\System\Entrance::config('IMG_UPLOAD'), ['file' => new \CURLFile(realpath($a['path'])), 'savePath' => dirname($savePath) . '/', 'saveName' => basename($a['path'])], true);
         //dump($upload);
         $upload = json_decode($upload, true);
         if (!($upload && $upload['result'] == true)) {
             return false;
         }
         unlink($a['path']);
         $return[$key]['path'] = $upload['path'];
     }
     return $return;
 }