guzzle を使用して URL から画像を取得しています。これらの URL の一部は s3 からのものです。s3 は、application/octet-stream
画像のコンテンツ タイプを使用します。$request
コールバックでオブジェクトにアクセスできないと思うfulfilled
ので、拡張子が URL にあるかどうかわかりません。fulfilled
コンテンツ タイプが の場合、コールバックで拡張子を推測する方法はありapplication/octet-stream
ますか?
<?php
function getExtension(Response $response) {
$contentType = explode(';', $response->getHeaderLine('Content-Type'), 2)[0];
$extensions = [
'image/png' => '.png',
'image/jpg' => '.jpg',
'image/jpeg' => '.jpeg',
'image/gif' => '.gif',
'image/tiff' => '.tiff',
'image/bmp' => '.bmp',
'application/octet-stream' => false
];
return $extensions[$contentType] ?? null;
}
$client = new Client();
$pool = new Pool($client, $requests, [
'concurrency' => 5,
'fulfilled' => function ($response) use ($zip) {
$ext = getExtension($response);
if (null === $ext) {
return;
}
if (false === $ext) {
// figure out the extension somehow
// $ext = ".foobar";
}
$id = Uuid::uuid4()->toString();
$filename = $id . $ext;
$zip->addFromString($filename, (string)$response->getBody());
},
'rejected' => function ($reason, $index) {
// no-op
}
]
);
$pool->promise()->wait();