php how to get web image size in kb?

前端 未结 7 888
迷失自我
迷失自我 2020-12-31 01:40

php how to get web image size in kb?

getimagesize only get the width and height.

and filesize caused waring.



        
相关标签:
7条回答
  • 2020-12-31 02:00

    You can get the file size by using the get_headers() function. Use below code:

        $image = get_headers($url, 1);
        $bytes = $image["Content-Length"];
        $mb = $bytes/(1024 * 1024);
        echo number_format($mb,2) . " MB";
    
    0 讨论(0)
  • 2020-12-31 02:03
    <?php
    $file_size = filesize($_SERVER['DOCUMENT_ROOT']."/Advertisers/2564.jpg"); // Get file size in bytes
    $file_size = $file_size / 1024; // Get file size in KB
    echo $file_size; // Echo file size
    ?>
    
    0 讨论(0)
  • 2020-12-31 02:06

    Short of doing a complete HTTP request, there is no easy way:

    $img = get_headers("http://static.adzerk.net/Advertisers/2564.jpg", 1);
    print $img["Content-Length"];
    

    You can likely utilize cURL however to send a lighter HEAD request instead.

    0 讨论(0)
  • 2020-12-31 02:07

    You can use also this function

    <?php
    $filesize=file_get_size($dir.'/'.$ff);
    $filesize=$filesize/1024;// to convert in KB
    echo $filesize;
    
    
    function file_get_size($file) {
        //open file
        $fh = fopen($file, "r");
        //declare some variables
        $size = "0";
        $char = "";
        //set file pointer to 0; I'm a little bit paranoid, you can remove this
        fseek($fh, 0, SEEK_SET);
        //set multiplicator to zero
        $count = 0;
        while (true) {
            //jump 1 MB forward in file
            fseek($fh, 1048576, SEEK_CUR);
            //check if we actually left the file
            if (($char = fgetc($fh)) !== false) {
                //if not, go on
                $count ++;
            } else {
                //else jump back where we were before leaving and exit loop
                fseek($fh, -1048576, SEEK_CUR);
                break;
            }
        }
        //we could make $count jumps, so the file is at least $count * 1.000001 MB large
        //1048577 because we jump 1 MB and fgetc goes 1 B forward too
        $size = bcmul("1048577", $count);
        //now count the last few bytes; they're always less than 1048576 so it's quite fast
        $fine = 0;
        while(false !== ($char = fgetc($fh))) {
            $fine ++;
        }
        //and add them
        $size = bcadd($size, $fine);
        fclose($fh);
        return $size;
    }
    ?>
    
    0 讨论(0)
  • 2020-12-31 02:10

    Not sure about using filesize() for remote files, but there are good snippets on php.net though about using cURL.

    http://www.php.net/manual/en/function.filesize.php#92462

    0 讨论(0)
  • 2020-12-31 02:18

    Here is a good link regarding filesize()

    You cannot use filesize() to retrieve remote file information. It must first be downloaded or determined by another method

    Using Curl here is a good method:

    Tutorial

    0 讨论(0)
提交回复
热议问题