/** * 下载图像 */ public function downloadHeadImg() { ignore_user_abort(true); set_time_limit(0); for ($i = 100; $i > 0; $i++) { $rt = $this->table('task_headimgdownload')->where(array('status' => 0))->get(); if (!$rt) { $this->setTimeOut($i); continue; } $tempPath = '../TempImg/'; $path = 'img/headImg/'; foreach ($rt as $a) { //先验证是否已更换图片 $user = $this->table('user')->where(array('id' => $a['user_id']))->get(array('photo'), true); if ($user['photo'] != $a['photo']) { $status = 3; } else { $img = httpGet($a['photo']); $fileName = time() . rand(10000, 99999) . '.jpg'; $save = file_put_contents($tempPath . $fileName, $img); if ($save) { $uploadStatus = true; $image = new \System\lib\Image\Image($tempPath . $fileName); $fileName2 = str_replace('.jpg', '_150.jpg', $fileName); $image->thumb(200, 10000)->save($tempPath . $fileName2); $status = 1; //上传到图片服务器 $upload = httpPost($this->config('IMG_UPLOAD'), ['file' => new \CURLFile(realpath($tempPath . $fileName)), 'savePath' => $path, 'saveName' => basename($tempPath . $fileName)], true); $upload = json_decode($upload, true); if (!($upload && $upload['result'] == true)) { $uploadStatus = false; } //上传压缩图 $upload2 = httpPost($this->config('IMG_UPLOAD'), ['file' => new \CURLFile(realpath($tempPath . $fileName2)), 'savePath' => $path, 'saveName' => basename($tempPath . $fileName2)], true); $upload2 = json_decode($upload, true); if (!($upload2 && $upload2['result'] == true)) { $uploadStatus = false; } if ($uploadStatus == false) { $status = 2; } else { $up2 = $this->table('user')->where(['id' => $a['user_id']])->update(['photo' => '/', $path . $fileName, 'photo_150' => '/', $path . $fileName2]); $status = 1; } } else { $status = 2; } } $up = $this->table('task_headimgdownload')->where(['id' => $a['id']])->update(array('status' => $status, 'execute_time' => time())); } } }
/** * 获取微信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; }
public function pictureUploadMore($pictureNameMore, $folderNameMore, $whetherMore = false) { //验证上传文件是否是图片 $upload = new \System\lib\Upload\Upload(); $upload->exts = array('jpg', 'gif', 'png', 'jpeg'); // 设置附件上传类型 $upload->rootPath = '../html/'; $upload->savePath = '/images/' . $folderNameMore . '/'; // 设置附件上传(子)目录 //$upload->saveName = explode('.', $saveName)[0]; // 开启子目录保存 并以日期(格式为Ymd)为子目录 $upload->autoSub = true; $upload->subName = array('date', 'Ymd'); //$upload->savename = $fileName; //多图上传 $moreInfo = $upload->upload($pictureNameMore); /*if(!$moreInfo){ $errorMsg=$upload->getError(); $this->R(['errorMsg'=>$errorMsg],'40021'); }*/ if ($moreInfo) { $figureUrl = ''; $img3PathUrl = ''; foreach ($moreInfo as $key => $v) { $figureName = $v['savename']; $figurePath = '.' . $v['savepath']; $figureUrl .= $v['savepath'] . $v['savename'] . ';'; if ($whetherMore) { //生成压缩图片 $image = new \System\lib\Image\Image($figurePath . $figureName); //打开原图路径 $img1 = $image->size(); $return['img1']['path'] = $figurePath . $figureName; $return['img1']['width'] = $img1[0]; $return['img1']['heigh'] = $img1[1]; //200*200缩略图 $img3Path = $figurePath . str_replace('.', '_thumb300.', $figureName); $img3PathUrl .= $v['savepath'] . str_replace('.', '_thumb300.', $figureName) . ';'; $image->thumb(200, 10000)->save($img3Path); $img2 = $image->size(); $return['img2']['path'] = $img3Path; $return['img2']['width'] = $img2[0]; $return['img2']['heigh'] = $img2[1]; } } } else { $figureUrl = ''; $img3PathUrl = ''; } $returnData = ['img' => $figureUrl, 'thumb' => $img3PathUrl]; return $returnData; }