【问题标题】:php how to get web image size in kb?php如何获取以kb为单位的网络图像大小?
【发布时间】:2011-09-10 11:43:09
【问题描述】:

php如何获取以kb为单位的网络图像大小?

getimagesize 只获取宽度和高度。

filesize 导致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

还有其他方法可以获取以 kb 为单位的网络图像大小吗?

【问题讨论】:

标签: php image image-size


【解决方案1】:

这听起来像是权限问题,因为 filesize() 应该可以正常工作。

这是一个例子:

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

确保图像上的权限设置正确并且路径也正确。您将需要应用一些数学来将字节转换为 KB,但完成此操作后您的状态应该很好!

【讨论】:

    【解决方案2】:
    <?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
    ?>
    

    【讨论】:

      【解决方案3】:

      这是一个关于filesize()的好链接

      您不能使用 filesize() 来检索远程文件信息。必须先下载或通过其他方法确定

      这里使用Curl是个好方法:

      Tutorial

      【讨论】:

        【解决方案4】:

        做一个完整的HTTP请求,没有简单的方法:

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

        您也可以使用cURL 发送lighter HEAD request instead

        【讨论】:

        • 很好,get_headers 运行得更快。谢谢。
        • 确保您的 HTTP 客户端没有发送任何标头说它接受 gzip 后的 HTTP 响应,否则 Content-Length 将出错,因为服务器会发回压缩数据。
        • @Darien:非常棒的收获!幸运的是 get_headers 发送了一个非常简单的 HTTP/1.0 请求。但对于 cURL,这需要更多的努力。
        • get_headers 也可以执行 HEAD 请求。 php 手册中有一个示例显示如何。
        • 这有时会返回一个数组:Array ( [0] =&gt; 0 [1] =&gt; 103823 ) 我该如何处理? get_headers($imageURL, 1)["Content-Length"] 其中$imageURLs-media-cache-ak0.pinimg.com/originals/23/bb/f3/…
        【解决方案5】:

        不确定是否将filesize() 用于远程文件,但php.net 上有很好的sn-ps 虽然关于使用cURL。

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

        【讨论】:

          【解决方案6】:

          你也可以使用这个功能

          <?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;
          }
          ?>
          

          【讨论】:

            【解决方案7】:

            您可以使用 get_headers() 函数获取文件大小。使用以下代码:

                $image = get_headers($url, 1);
                $bytes = $image["Content-Length"];
                $mb = $bytes/(1024 * 1024);
                echo number_format($mb,2) . " MB";
            

            【讨论】:

              猜你喜欢
              • 2020-04-20
              • 2014-02-19
              • 2023-02-04
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 2014-09-11
              • 1970-01-01
              • 2019-04-22
              相关资源
              最近更新 更多