That URL is a simple 302 redirect to the actual resource. You can find the extension of the resource by extracting the URL from the location
header that you receive. This way you don't have to download the picture to figure it out.
<?php
/*
Question Author: panthro
Question Answerer: Jacob Mulquin
Question: Get a file extension from a image URL?
URL: https://stackoverflow.com/questions/76571719/get-a-file-extension-from-a-image-url
Tags: php
*/
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://picsum.photos/200/200?random=61081');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HEADER, true);
$result = curl_exec($ch);
// yoinked from https://stackoverflow.com/a/44698478/1427345
$headers = [];
$headerSize = curl_getinfo($ch, CURLINFO_HEADER_SIZE);
foreach(explode("\r\n", trim(substr($result, 0, $headerSize))) as $row) {
if(preg_match('/(.*?): (.*)/', $row, $matches)) {
$headers[$matches[1]] = $matches[2];
}
}
$extension = pathinfo($headers['location'], PATHINFO_EXTENSION);
$ext = explode('?',$extension)[0];
var_dump($ext);
Results in:
string(3) "jpg"