图片下载站经常会用到的,自动获取图片的高、宽和大小,
1、添加函数
PHP
function tc_imgssize_getImageSize($url, $type = 'curl', $isGetFilesize = false){ $type = $isGetFilesize ? 'fread' : $type; if ($type == 'fread') { $handle = fopen($url, 'rb'); if (! $handle) return false; $dataBlock = fread($handle, 168); }else{ $ch = curl_init($url); curl_setopt($ch, CURLOPT_TIMEOUT, 5); curl_setopt($ch, CURLOPT_RANGE, '0-167'); curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); $dataBlock = curl_exec($ch); curl_close($ch); if (! $dataBlock) return false; } $size = getimagesize('data://image/jpeg;base64,'. base64_encode($dataBlock)); if (empty($size)) { // $size = (getimagesize('compress.zlib://'.$url)); // if (empty($size)) { // return false; // } return false; } $result['width'] = $size[0]; $result['height'] = $size[1]; if ($isGetFilesize) { $meta = stream_get_meta_data($handle); $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; } } } if ($type == 'fread') fclose($handle); return $result;}
2、调用函数
PHP
$imageInfo=tc_imgssize_getImageSize($imgurl, 'fread', true);
3、输入图片的高、宽和大小
PHP
$width=$imageInfo['width'];$height=$imageInfo['height'];$size=$imageInfo['size'];
高、宽的单位是px,大小的单位是kb。