skip to Main Content

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.

4

Answers


  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

    Login or Signup to reply.
  2. 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; 
    
    ?>
    
    Login or Signup to reply.
  3. 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("rn", 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"
    
    Login or Signup to reply.
  4. 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'];
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search