{"id":1899,"date":"2012-07-22T13:21:00","date_gmt":"2012-07-22T05:21:00","guid":{"rendered":""},"modified":"2013-11-25T18:12:15","modified_gmt":"2013-11-25T10:12:15","slug":"php%e4%b8%8a%e4%bc%a0%e7%bd%91%e7%bb%9c%e5%9b%be%e7%89%87%e5%ae%9e%e7%8e%b0","status":"publish","type":"post","link":"https:\/\/kyle.ai\/blog\/1899.html","title":{"rendered":"PHP\u4e0a\u4f20\u7f51\u7edc\u56fe\u7247\u5b9e\u73b0"},"content":{"rendered":"<p>\u529f\u80fd\u622a\u56fe\uff1a<\/p>\n<p><img decoding=\"async\" src=\".\/wp-content\/uploads\/hibaidu\/ca3302f2d7ca7bcb7693f3bcbe096b63f724a873.jpg\" alt=\"\" \/><\/p>\n<p>\u5b9e\u73b0\u601d\u8def\uff1a\u9996\u5148\u8981\u5bf9\u7528\u6237\u8f93\u5165\u7684URL\u8fdb\u884c\u5feb\u901f\u7684\u5408\u6cd5\u6027\u5224\u65ad\uff0c\u5305\u62ec\u6587\u4ef6\u5927\u5c0f\uff0c\u6587\u4ef6\u7c7b\u578b\u7b49\u3002\u7136\u540e\u518d\u4e0b\u8f7d\uff0c\u518d\u8fdb\u884c\u540e\u5904\u7406\u7b49\u3002<\/p>\n<p>\u5b9e\u73b01\u3001\u9996\u5148\u4e0d\u4e0b\u8f7d\uff0c\u5feb\u901f\u5224\u65ad\u6587\u4ef6\u7c7b\u578b\u4e0e\u6587\u4ef6\u5927\u5c0f<\/p>\n<pre class=\"brush: php; title: ; notranslate\" title=\"\">\r\n      \/\/get image size  and MIME type added by chenming 2012-7-20\r\n\r\n     function getFileSizeAndType($url){\r\n\r\n        $result = array();\r\n\r\n        $url = parse_url( $url);\r\n\r\n        if($fp = @fsockopen($url&#x5B; 'host'],empty($url&#x5B;'port' ])?80: $url&#x5B;'port'],$error)){\r\n\r\n               fputs($fp,&quot;GET &quot; .(empty( $url&#x5B;'path'])? \u2018\/\u2019:$url&#x5B; 'path']).&quot; HTTP\/1.1\\r\\n&quot;);\r\n\r\n               fputs($fp,&quot;Host: $url&#x5B;host] \\r\\n\\r\\n&quot;);\r\n\r\n               while(!feof ($fp)){\r\n\r\n                     $tmp = fgets($fp);\r\n\r\n                      if(trim($tmp) == \u201d){\r\n\r\n                            break;\r\n\r\n                     } else if(preg_match (\u2018\/Content-Length:(.*)\/si\u2019,$tmp,$arr)){\r\n\r\n                           $result&#x5B; 'size'] = trim($arr&#x5B; 1]);\r\n\r\n                     } else if(preg_match (\u2018\/Content-Type:(.*)\/si\u2019, $tmp, $arr)){\r\n\r\n                           $result&#x5B; 'type'] = trim($arr&#x5B; 1]);\r\n\r\n                     }\r\n\r\n                      if(count($result) == 2){\r\n\r\n                            return $result;\r\n\r\n                     }\r\n\r\n              }\r\n\r\n               return null;\r\n\r\n       } else{\r\n\r\n               return null;\r\n\r\n       }\r\n\r\n     }\r\n<\/pre>\n<p>\u7b2c2\u3001\u4ee5\u4e0a\u7c7b\u578b\u4e0e\u5927\u5c0f\u90fd\u68c0\u67e5\u901a\u8fc7\u540e\uff0c\u518d\u4e0b\u8f7d\u6587\u4ef6<\/p>\n<pre class=\"brush: php; title: ; notranslate\" title=\"\">\r\n      \/*\r\n\r\n      * \u4e0b\u8f7d\u56fe\u7247 \uff0c\u5e76\u83b7\u53d6\u7c7b\u578b, added by chenming 2012-7-19\r\n\r\n      * *\/\r\n\r\n      function getRemoteContentType($remoteFile){\r\n\r\n             # the request\r\n\r\n            $ch = curl_init($remoteFile);\r\n\r\n             curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);\r\n\r\n            $data = curl_exec($ch);\r\n\r\n             # get the content type\r\n\r\n            $info = curl_getinfo($ch, CURLINFO_CONTENT_TYPE);\r\n\r\n             return array(\u2018data\u2019 =&gt;$data, \u2018type\u2019=&gt;$info);\r\n\r\n     }\r\n<\/pre>\n<p>\u7b2c3\u3001\u5bf9\u56fe\u7247\u5927\u5c0f\u8fdb\u884c\u5904\u7406\uff0c\u83b7\u53d6\u521a\u4e0b\u8f7d\u56fe\u7247\u5927\u5c0f\u7684\u65b9\u6cd5<\/p>\n<pre class=\"brush: php; title: ; notranslate\" title=\"\">\r\n     $datainfo = $this-&gt; getRemoteContentType ($url);  \/\/\u4e0b\u8f7d\u5185\u5bb9\u5e76\u627e\u51faMIME\u7c7b\u578b\r\n\r\n     $file = $datainfo&#x5B; 'data'];\r\n\r\n     $img = imagecreatefromstring($datainfo&#x5B;'data' ]);\r\n\r\n     $img_width = imagesx($img);\r\n\r\n     $img_height = imagesy($img);\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u529f\u80fd\u622a\u56fe\uff1a \u5b9e\u73b0\u601d\u8def\uff1a\u9996\u5148\u8981\u5bf9\u7528\u6237\u8f93\u5165\u7684URL\u8fdb\u884c\u5feb\u901f\u7684\u5408\u6cd5\u6027\u5224\u65ad\uff0c\u5305\u62ec\u6587\u4ef6\u5927\u5c0f\uff0c\u6587\u4ef6\u7c7b\u578b\u7b49\u3002\u7136\u540e\u518d\u4e0b\u8f7d\uff0c\u518d\u8fdb [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[6],"tags":[],"class_list":["post-1899","post","type-post","status-publish","format-standard","hentry","category-code_related"],"_links":{"self":[{"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/posts\/1899","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/comments?post=1899"}],"version-history":[{"count":1,"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/posts\/1899\/revisions"}],"predecessor-version":[{"id":5215,"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/posts\/1899\/revisions\/5215"}],"wp:attachment":[{"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/media?parent=1899"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/categories?post=1899"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/tags?post=1899"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}