Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
supperbro opened this issue Dec 8, 2022 · 0 comments
Open

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

supperbro opened this issue Dec 8, 2022 · 0 comments

Comments

@supperbro
Copy link

//需要开启协程数、判断需要处理的图片数与协程数,取较小值
$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, "handle_num", 1);
});
$client->getClient()->close();
} catch (\Exception $e) {
echo CLASS."".FUNCTION.""."下载图片异常_".$e->getMessage().PHP_EOL;
}
}
$wg->done();
});
}
$wg->wait();
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant