您好,欢迎来到三六零分类信息网!老站,搜索引擎当天收录,欢迎发信息

php获取远程图片大小函数示例

2024/3/6 20:05:00发布18次查看
//用法 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信息,即实现了获取远程图片大小的功能。
该用户其它信息

VIP推荐

免费发布信息,免费发布B2B信息网站平台 - 三六零分类信息网 沪ICP备09012988号-2
企业名录 Product