PHP implements an efficient way to get remote picture size and size

Source: Internet
Author: User
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 ' 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.