Home >Backend Development >PHP Tutorial >PHP method to view request header information and obtain remote image size_PHP tutorial
If you want to get the size of a remote image, a common approach is to get the content of the remote image back first, and then use strlen to calculate the length. This method requires downloading the image before it can be calculated. If the image is large, network transmission will take a lot of time, and 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 will return the header information of the request, where Content-Length indicates the content size of the requested web page. If the request is for a picture, then Content-Length indicates the size of the picture. According to this, you only need to send a head request to obtain the returned header information. In PHP, header information can be obtained through the fsockopen method. The code is as follows:
In addition, some servers may block the HEAD request. If it is blocked, you can only use the GET request. If you want to do this, you can directly use the ready-made function getimagesize.
http://www.bkjia.com/PHPjc/621654.html
www.bkjia.com