php如何获取网页图像大小(以kb为单位)?

发布于 2024-11-14 02:01:05 字数 394 浏览 4 评论 0 原文

php如何获取网页图像大小(以kb为单位)?

getimagesize 仅获取宽度和高度。

filesize导致waring

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

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

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

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

揽清风入怀 2024-11-21 02:01:05

除了执行完整的 HTTP 请求之外,没有简单的方法:

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

您可以利用 cURL 但是要发送更轻的 HEAD 请求

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.

恋竹姑娘 2024-11-21 02:01:05
<?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
?>
<?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
?>
泡沫很甜 2024-11-21 02:01:05

不确定是否对远程文件使用 filesize(),但 php.net 上有关于使用 cURL 的很好的代码片段。

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

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

猫腻 2024-11-21 02:01:05

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

这是一个示例:

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

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

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!

热情消退 2024-11-21 02:01:05

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

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

这里使用 Curl 是一个很好的方法:

教程

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

迷乱花海 2024-11-21 02:01:05

你也可以使用这个功能

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

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;
}
?>
樱娆 2024-11-21 02:01:05

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

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

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";
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文