获取远程图片的宽、高和大小

来源:互联网 发布:sqlserver exists in 编辑:程序博客网 时间:2024/06/06 04:42

php中获取远程图片的宽、高和大小,这里介绍两种方法:

方法一:

 function getImageinfo($url)    {        $result = array(            'width'=>'',            'height'=>'',            'size'=>'',        );        $imageInfo = getimagesize($url);        $result['width']=$imageInfo[0];        $result['height']=$imageInfo[1];                $headerInfo = get_headers($url,true);        $result['size']=$headerInfo['Content-Length'];        return $result;    }        $url = 'https://qncdn.wanshifu.com/237f4f2eebd1aea845e014a1634cad36?imageView2/0/interlace/1/ignore-error/1';    $res = getImageinfo($url);    print_r($res);




方法二:

function getImageinfo($url)    {        $imageInfo = getimagesize($url);        if($imageInfo){             $result['width'] = $imageInfo[0];             $result['height'] = $imageInfo[1];        }               // 若需要获取图片体积大小则默认使用 fread 方式或者使用 socket 二进制方式读取, 需要获取图片体积大小最好使用此方法        $handle = fopen($url, 'rb');        if ($handle) {              //是否获取图片体积大小                $meta = stream_get_meta_data($handle);// 获取文件数据流信息                // nginx 的信息保存在 headers 里,apache 则直接在 wrapper_data                $dataInfo = isset($meta['wrapper_data']['headers']) ? $meta['wrapper_data']['headers'] : $meta['wrapper_data'];                foreach ($dataInfo as $va) {                    if (preg_match('/length/iU', $va)) {                        $ts = explode(':', $va);                        $result['size'] = trim(array_pop($ts));                        break;                    }                }                //关闭文件流                fclose($handle);        }        return $result;    }   $url = 'https://qncdn.wanshifu.com/237f4f2eebd1aea845e014a1634cad36?imageView2/0/interlace/1/ignore-error/1';    $res = getImageinfo($url);    print_r($res);


0 0