PHP efficient remote image size acquisition

Source: Internet
Author: User
PHP efficient remote image size acquisition
/*** Obtain the width and size of a remote image ** @ param string $ url remote image link * @ param string $ type to obtain remote image resources, the default value is curl. fread * @ param boolean $ isGetFilesize indicates whether to obtain the volume of the remote image. the default value is false, when set to true, $ type is forced to fread * @ return false | array */function myGetImageSize ($ url, $ type = 'Curl', $ isGetFilesize = false) {// If you want to obtain the image size, the fread method $ type = $ isGetFilesize is used by default? 'Fread': $ type; if ($ type = 'fread') {// or use the socket binary method to read data, to obtain the image volume, use this method $ handle = fopen ($ url, 'RB'); if (! $ Handle) return false; // only the fixed length of the header is 168 bytes. $ dataBlock = fread ($ handle, 168 );} else {// It is said that CURL can cache DNS more efficiently than socket $ ch = curl_init ($ url); // timeout setting curl_setopt ($ ch, CURLOPT_TIMEOUT, 5 ); // There is no problem in reading the width and height of the first 168 characters from the four test charts. if the data cannot be obtained, increase the value curl_setopt ($ ch, CURLOPT_RANGE, '0-167 '); // trace 301 jump curl_setopt ($ ch, CURLOPT_FOLLOWLOCATION, 1); // return result curl_setopt ($ ch, CURLOPT_RETURNTRANSFER, true ); $ dataBlock = curl_exec ($ ch); Curl_close ($ ch); if (! $ DataBlock) return false;} // Convert the Read Image information to the image path and obtain the image information. after testing, jpeg pairs are set to obtain png, gif information is not affected. you do not need to set these images separately. // some images can be viewed in the browser, but they are damaged. The information may not be parsed. $ size = getimagesize ('data: // image/jpeg; base64 ,'. base64_encode ($ dataBlock); if (empty ($ size) {return false;} $ result ['width'] = $ size [0]; $ result ['height'] = $ size [1]; // whether to obtain the image size if ($ isGetFilesize) {// get the file data stream information $ meta = stream_get_meta_data ($ handle); // The nginx information is stored in headers., Apache is directly in 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;} // link to the test Image echo ''; $ result = myGetImageSize (' http://s6.mogujie.cn/b7/bao/120630/2kpa6_kqywusdel5bfqrlwgfjeg5sckzsew_345x483.jpg_225x999.jpg ', 'Curl'); print_r ($ result); echo ''; $ result = myGetImageSize (' http://s5.mogujie.cn/b7/bao/120629/6d3or_kqytasdel5bgevsugfjeg5sckzsew_801x1193.jpg ', 'Fread'); print_r ($ result); echo ''; $ result = myGetImageSize (' http://hiphotos.baidu.com/zhengmingjiang/pic/item/1c5f338c6d22d797503d92f9.jpg ', 'Fread', true); print_r ($ result); echo ''; $ result = myGetImageSize (' http://www.vegandocumentary.com/wp-content/uploads/2009/01/imveganlogotransparentbackground.png ', 'Curl', true); print_r ($ result); echo ''; $ result = myGetImageSize (' http://jiaoyou.ai9475.com/front/templates/jiaoyou/styles/default/image/ad_pic_1.gif ', 'Fread'); print_r ($ result );

The preceding figure shows how PHP can efficiently obtain the size and size of remote images. For more information, see PHP Chinese website (www.php1.cn )!

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.