21

php how to get web image size in kb?

getimagesize only get the width and height.

and filesize caused waring.

$imgsize=filesize("http://static.adzerk.net/Advertisers/2564.jpg");
echo $imgsize;

Warning: filesize() [function.filesize]: stat failed for http://static.adzerk.net/Advertisers/2564.jpg

Is there any other way to get a web image size in kb?

Manuel Allenspach
  • 12,467
  • 14
  • 54
  • 76
fish man
  • 2,666
  • 21
  • 54
  • 94
  • 1
    possible duplicate of [PHP: Remote file size without downloading file](http://stackoverflow.com/questions/2602612/php-remote-file-size-without-downloading-file) – deceze Jun 07 '11 at 23:25
  • This seems to be related : [link]http://stackoverflow.com/questions/2145021/php-getimagesize-alternatives-without-javascript[/link] – knurdy Jun 07 '11 at 23:27

7 Answers7

29

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.

Community
  • 1
  • 1
mario
  • 144,265
  • 20
  • 237
  • 291
  • great, get_headers run faster. Thanks. – fish man Jun 07 '11 at 23:32
  • 2
    Make sure your HTTP client is not sending any headers saying it accepts gzipped HTTP responses, or else the `Content-Length` will be wrong because the server will send compressed data back. – Darien Jun 07 '11 at 23:39
  • @Darien: Excellent catch! Luckily `get_headers` sends a pretty plain HTTP/1.0 request. But for cURL this would need more diligence. – mario Jun 07 '11 at 23:48
  • 1
    `get_headers` can also do a HEAD request. there is an example in the php manual showing how. – Gordon Jan 23 '12 at 12:47
  • 1
    This sometimes returns an array: `Array ( [0] => 0 [1] => 103823 )` How do I handle that? `get_headers($imageURL, 1)["Content-Length"]` where `$imageURL` is https://s-media-cache-ak0.pinimg.com/originals/23/bb/f3/23bbf35e95d5ad470b038d420fc1a9c4.jpg – maxisme Aug 02 '17 at 09:17
6
<?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
?>
Morgan Delaney
  • 2,349
  • 3
  • 20
  • 23
3

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

Nick Pyett
  • 3,338
  • 1
  • 23
  • 27
2

That sounds like a permissions issue because filesize() should work just fine.

Here is an example:

php > echo filesize("./9832712.jpg");
1433719

Make sure the permissions are set correctly on the image and that the path is also correct. You will need to apply some math to convert from bytes to KB but after doing that you should be in good shape!

niczak
  • 3,897
  • 11
  • 45
  • 65
1

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;
}
?>
Sandeep Tawaniya
  • 717
  • 8
  • 17
1

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

lockdown
  • 1,456
  • 5
  • 16
  • 32
0

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";