0

I get a random image like this:

https://picsum.photos/200/200?random=61081

How can I get the image extension? jpg/png etc.?

I've considered this: $extension = pathinfo($file, PATHINFO_EXTENSION); but it does not work.

EDIT: $extension = pathinfo($file, PATHINFO_EXTENSION); gives an empty string.

panthro
  • 22,779
  • 66
  • 183
  • 324
  • 1
    I just checked the url, and it does a redirect to a file with an extension: `https://fastly.picsum.photos/id/1066/200/200.jpg?hmac=BHYYzH0KERL1WifyefL6hVVg0wURJUgTaByr75WmJug`. Follow the redirect then use `pathinfo` – Jacob Mulquin Jun 28 '23 at 09:21
  • 1
    Does this answer your question? [Get image extension from dynamic image url](https://stackoverflow.com/questions/54919489/get-image-extension-from-dynamic-image-url) – DarkBee Jun 28 '23 at 09:21
  • 2
    I think there're some wrong expectations here. [pathinfo()](https://php.net/pathinfo) is basically a string manipulation function: "pathinfo() operates naively on the input string, and is not aware of the actual filesystem". It will not download or open the file, let alone parse its contents to guess the file type. – Álvaro González Jun 28 '23 at 10:02

4 Answers4

1

The link is just a generated path, not the actual file path. In such cases, the response should include the type of the received content, such as JSON, image, etc.

You can read this using the get_headers() 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';
}

// ...

More information

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

How to get File-Extension from MIME-Type - StackOverflow Answer

rozsazoltan
  • 2,831
  • 2
  • 7
  • 20
1

You can try

<?php

$url = 'https://picsum.photos/200/200?random=61081';

$headers = get_headers($url, 1);
$contentType = $headers['Content-Type'];
$extension = explode('/', $contentType)[1];

echo $extension; 

?>
PRATHEESH PC
  • 1,461
  • 1
  • 3
  • 15
1

Since the extension is given in the redirection url, you can just parse it without downloading the whole image:

$ch = curl_init('https://picsum.photos/200/200?random=61081');
if(curl_exec($ch)) {
    $url = curl_getinfo($ch, CURLINFO_REDIRECT_URL);
    $ext = pathinfo(parse_url($url, PHP_URL_PATH))['extension'];
}
shingo
  • 18,436
  • 5
  • 23
  • 42
0

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"
Jacob Mulquin
  • 3,458
  • 1
  • 19
  • 22