-
Notifications
You must be signed in to change notification settings - Fork 511
Description
//需要开启协程数、判断需要处理的图片数与协程数,取较小值
$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();
}