我得到了一张随机的图像,如下所示:
https://picsum.photos/200/200?random=61081
我怎样才能获得图像扩展名?JPG/PNG等?
我考虑过这个:$extension = pathinfo($file, PATHINFO_EXTENSION);
美元,但行不通.
编辑:$extension = pathinfo($file, PATHINFO_EXTENSION);
表示空字符串.
我得到了一张随机的图像,如下所示:
https://picsum.photos/200/200?random=61081
我怎样才能获得图像扩展名?JPG/PNG等?
我考虑过这个:$extension = pathinfo($file, PATHINFO_EXTENSION);
美元,但行不通.
编辑:$extension = pathinfo($file, PATHINFO_EXTENSION);
表示空字符串.
该链接只是生成的路径,而不是实际的文件路径.在这种情况下,响应应该包括接收到的内容的类型,如JSON、图像等.
You can read this using the 100 function.
$imageUrl = 'https://picsum.photos/200/200?random=61081';
$headers = get_headers($imageUrl, true); // data from response
$extension = 'undefined';
$contentType = $headers['Content-Type']; // response type
// if image/jpeg, then extension is .jpg
if ($contentType === 'image/jpeg') {
$extension = 'jpg';
}
// ...
get_headers()
- PHP Docs (with example output, where can found Content-Type
)
What is Content-Type
- MDN Docs (this value is always MIME type
)
What is MIME type
- MDN Docs
List of MIME types - iana.org
100 - StackOverflow Answer