PHP implements an efficient way to get remote picture size and size
Implementation code:
/** * Gets the width and volume of the remote picture * * @param string $url A link to a remote picture * @param string $type the way to get a remote picture resource, the default is Curl optional fread * @param boo Lean $isGetFilesize whether to get the volume size of the remote picture, default false not to get, set to True when $type is forced to fread * @return False|array */function Mygetimages Ize ($url, $type = ' curl ', $isGetFilesize = False) {//If you need to get the picture volume size then the fread mode is used by default $type = $isGetFilesize? ' Fread ': $type; if ($type = = ' Fread ') {//or read using socket binary mode, it is best to use this method $handle = fopen ($url, ' RB ') to obtain a picture volume size; if (! $handle) return false; Take only the fixed length of the head 168 bytes of data $dataBlock = Fread ($handle, 168); } else {//It is said that CURL can cache DNS efficiency higher than the socket $ch = Curl_init ($url); Timeout Settings curl_setopt ($ch, Curlopt_timeout, 5); Take the first 168 characters through four test charts to read the width and height of the results are no problem, if the data can be obtained appropriately increased numerical curl_setopt ($ch, Curlopt_range, ' 0-167 '); Track 301 Jump curl_setopt ($ch, curlopt_followlocation, 1); Returns the result curl_setopt ($ch, Curlopt_returntransfer, true); $dataBlock = curl_exec ($ch); Curl_close ($ch); If (! $dataBlock) return false; }//To convert the picture information read to the picture path and obtain the picture information, after testing, here the conversion settings JPEG has no effect on obtaining png,gif information, no need to set//some pictures although can be viewed in the browser but the actual damage may not be able to resolve the information $size = GE Timagesize (' Data://image/jpeg;base64, '. Base64_encode ($dataBlock)); if (empty ($size)) {return false; } $result [' width '] = $size [0]; $result [' height '] = $size [1]; Whether to get picture volume size if ($isGetFilesize) {//Get File data stream Information $meta = Stream_get_meta_data ($handle); Nginx information is stored in headers, Apache is directly in the 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 }}} if ($type = = ' Fread ') fclose ($handle); return $result; }//test the picture link echo ' <pre> '; $result = Mygetimagesize (' http://s6.mogujie.cn/b7/bao/120630/2kpa6_kqywusdel5bfqrlwgfjeg5sckzsew_345x483. Jpg_225x999.jpg ', ' curl '); Print_r ($result); echo '