easy-swoole / easyswoole

swoole,easyswoole,swoole framework
https://www.easyswoole.com/
Apache License 2.0
4.73k stars 512 forks source link

HttpClient的Download方法出现图片下载不完整问题 #565

Open supperbro opened 1 year ago

supperbro commented 1 year ago

//需要开启协程数、判断需要处理的图片数与协程数,取较小值 $loopSize = $size < ($imageLength-$start) ? $size : ($imageLength-$start); for ($j = 0; $j < $loopSize; $j++){ //保存图片 $wg->add(); go (function () use($wg, $redis, $j, $start, $imageLocalPath, $channel, $imageUrl, $imagePath, $exportId){ $imageIndex = $j + $start; $image_url = $imageUrl[$imageIndex]; if (empty($image_url) || $image_url == "") { $imageData = [$imageIndex=>""]; $channel->push($imageData); }else { $client = new HttpClient(); $endWith = pathinfo($image_url); $endType = "jpg"; if (isset($endWith['extension'])) { $endType = $endWith['extension']; } $client->setUrl($image_url); $client->setTimeout(15); $filename = "{$imagePath}/{$imageIndex}.{$endType}"; $imageLocalFullPath = "{$imageLocalPath}/{$imageIndex}.{$endType}"; $imageData = [$imageIndex=>$imageLocalFullPath]; $channel->push($imageData); try { $res = $client->download($filename, 0, HttpClient::METHOD_GET, $client->get()->getBody()); Manager::getInstance()->get("ssc_capture")->invoke(function (Redis $redis) use ($exportId) { $redis->hIncrBy($this->taskId, "handlenum", 1); }); $client->getClient()->close(); } catch (\Exception $e) { echo CLASS."".FUNCTION.""."下载图片异常".$e->getMessage().PHP_EOL; } } $wg->done(); }); } $wg->wait(); }