php获取远程图片体积大小的实例


Posted in PHP onNovember 12, 2013

直接看代码吧,很好明白的

//用法 echo remote_filesize($url,$user='',$pw='');
$url = "http://www.aa.com/librarys/images/random/rand_11.jpg";//这里要换成你的图片地址
echo remote_filesize($url,$user='',$pw='');
function remote_filesize($uri,$user='',$pw='')
{
// start output buffering
    ob_start();
// initialize curl with given uri
    $ch = curl_init($uri); // make sure we get the header
    curl_setopt($ch, CURLOPT_HEADER, 1); // make it a http HEAD request
    curl_setopt($ch, CURLOPT_NOBODY, 1); // if auth is needed, do it here
    if (!empty($user) && !empty($pw))
    {
        $headers = array('Authorization: Basic ' . base64_encode($user.':'.$pw));
        curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
    }
    $okay = curl_exec($ch);
    curl_close($ch); // get the output buffer
    $head = ob_get_contents(); // clean the output buffer and return to previous // buffer settings
    ob_end_clean();  // gets you the numeric value from the Content-Length // field in the http header
    $regex = '/Content-Length:\s([0-9].+?)\s/';
    $count = preg_match($regex, $head, $matches);  // if there was a Content-Length field, its value // will now be in $matches[1]
    if (isset($matches[1]))
    {
        $size = $matches[1];
    }
    else
    {
        $size = 'unknown';
    }
    $last_mb = round($size/(1024*1024),3);
 $last_kb = round($size/1024,3);
    return $last_kb . 'KB / ' . $last_mb.' MB';
}

函数的思路是,先CURL获取图片到缓冲区,然后正则获取图片的Content-Length信息就OK了。
PHP 相关文章推荐
PHP类与对象中的private访问控制的疑问
Nov 01 PHP
php页面跳转代码 输入网址跳转到你定义的页面
Mar 28 PHP
PHP 循环删除无限分类子节点的实现代码
Jun 21 PHP
PHP把空格、换行符、中文逗号等替换成英文逗号的正则表达式
May 04 PHP
PHP中使用glob函数实现一句话删除某个目录下的所有文件
Jul 22 PHP
php json转换成数组形式代码分享
Nov 10 PHP
php中判断数组相等的方法以及数组运算符介绍
Mar 30 PHP
php通过前序遍历树实现无需递归的无限极分类
Jul 10 PHP
PHP上传图片、删除图片简单实例
Nov 12 PHP
PHP两种实现无级递归分类的方法
Mar 02 PHP
PHP mongodb操作类定义与用法示例【适合mongodb2.x和mongodb3.x】
Jun 16 PHP
PHP中一个有趣的preg_replace函数详解
Aug 15 PHP
php过滤XSS攻击的函数
Nov 12 #PHP
php获取新浪微博数据API实例
Nov 12 #PHP
php生成N个不重复的随机数实例
Nov 12 #PHP
三种php连接access数据库方法
Nov 11 #PHP
PHP中ob_start函数的使用说明
Nov 11 #PHP
PHP开发工具ZendStudio下Xdebug工具使用说明详解
Nov 11 #PHP
PHP利用str_replace防注入的方法
Nov 10 #PHP
You might like
成本8450万,票房仅2亿,口碑两极分化,又一部DC电影扑街了
2020/04/09 欧美动漫
php设计模式 Observer(观察者模式)
2011/06/26 PHP
PHP合并数组函数array_merge用法分析
2017/02/17 PHP
ThinkPHP Where 条件中常用表达式示例(详解)
2017/03/31 PHP
laradock环境docker-compose操作详解
2019/07/29 PHP
PHP实现页面静态化深入讲解
2021/03/04 PHP
JavaScript中的History历史对象
2008/01/16 Javascript
JS 实现获取打开一个界面中输入的值
2013/03/19 Javascript
VueJs使用Amaze ui调整列表和内容页面
2017/11/30 Javascript
Vue+axios+WebApi+NPOI导出Excel文件实例方法
2019/06/05 Javascript
vue下使用nginx刷新页面404的问题解决
2019/08/02 Javascript
JS实现压缩上传图片base64长度功能
2019/12/03 Javascript
vue中实现点击变成全屏的多种方法
2020/09/27 Javascript
Python简单计算文件夹大小的方法
2015/07/14 Python
解决Django模板无法使用perms变量问题的方法
2017/09/10 Python
分分钟入门python语言
2018/03/20 Python
Python3.x爬虫下载网页图片的实例讲解
2018/05/22 Python
在python中使用requests 模拟浏览器发送请求数据的方法
2018/12/26 Python
如何运行带参数的python脚本
2019/11/15 Python
Python partial函数原理及用法解析
2019/12/11 Python
利用OpenCV和Python实现查找图片差异
2019/12/19 Python
python从内存地址上加载python对象过程详解
2020/01/08 Python
基于TensorBoard中graph模块图结构分析
2020/02/15 Python
python print 格式化输出,动态指定长度的实现
2020/04/12 Python
python3.8.1+selenium实现登录滑块验证功能
2020/05/22 Python
python -v 报错问题的解决方法
2020/09/15 Python
HTML5的表单(绝对特别强大的功能)使用示例
2013/06/20 HTML / CSS
伦敦剧院门票:London Theatre Direct
2018/11/21 全球购物
Reebok官方旗舰店:美国知名健身品牌锐步
2019/01/07 全球购物
C#如何判断当前用户是否输入某个域
2015/12/07 面试题
大学四年学习的自我评价分享
2013/12/09 职场文书
四年级学生评语大全
2014/04/21 职场文书
审计局2014法制宣传日活动总结
2014/11/01 职场文书
获奖感言一句话
2015/07/31 职场文书
MySQL中的全表扫描和索引树扫描
2022/05/15 MySQL
MYSQL如何查看操作日志详解
2022/05/30 MySQL