PHP View request Header info get remote picture size

Source: Internet
Author: User

 php gets header information through the Fsockopen method, and if the request is a picture, the content-length here represents the size of the picture

If you want to get the size of a remote picture, it's a common practice to get the contents of the remote picture back and then use strlen to compute the length, which requires downloading the pictures before you can compute them. If the picture is very large, then the network transmission will take a lot of time, the efficiency is obviously low. The author provides a method to improve efficiency, mainly using HTTP header information.   When accessing a Web page, the server returns the requested header information, where content-length represents the size of the requested page content. If the request is a picture, then content-length represents the size of the picture. Based on this, just send the head request to get the returned header information OK. In PHP, you can get header information through the Fsockopen method. The code is as follows:     code is as follows: $fp = Fsockopen ("www.baidu.com", $errno, $errstr, 30); if ($fp) {   //The request is set to head on the line     $out = "Head/img/baidu_sylogo1.gif http/1.1rn";     $ou T. = "HOST:WWW.BAIDU.COMRN";     $out. = "Connection:closernrn";     fwrite ($fp, $out);     while (!feof ($fp)) {        $header = fgets ($fp);         if (strip OS ($header, ' Content-length ')!== false) {            $size = Trim (substr ($header, Strpos ($ Header, ': ') + 1);             echo $size;        }     {    fclose ($FP); else {   echo "$errstr ($errno)";     as well as initiating a GET request, just set the request type get to head. The requested host and path, modify it to their own needs on it.   Summary:   PHP can also use Get_headers to get header information, but the author tested this function, is a GET request, details Reference: PHP function get_headers is the head request or getting request.   Other servers may block head requests, and if they are blocked, they can only use get requests honestly. If you want to do this, you can use the Out-of-the-box function getimagesize directly.  

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.