private static function makePromisesFromQueue(array $imgQueue, Client $client, array &$imgSizes, array &$errors) { /** @var Promise[] $promises */ $promises = []; foreach ($imgQueue as $imageSrc) { $promise = $client->headAsync($imageSrc)->then(function (ResponseInterface $res) use(&$imgSizes, &$errors, $imageSrc) { if ($headers = $res->getHeaders() and array_key_exists('Content-Length', $headers) and is_array($headers['Content-Length']) and count($headers['Content-Length']) > 0) { array_push($imgSizes, intval($headers['Content-Length'][0])); } else { array_push($errors, 'unable to get image size: ' . $imageSrc); } }, function (RequestException $e) use($imageSrc, &$errors) { array_push($errors, 'unable to get image headers: ' . $imageSrc); }); array_push($promises, $promise); } return $promises; }