PHP method to view request header information and obtain remote image size_PHP tutorial

WBOY
Release: 2016-07-13 17:18:41
Original
1031 people have browsed it

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:

Copy code The code is as follows:

$fp = fsockopen("www.baidu.com", 80, $errno, $errstr, 30);
if ($fp) {
//Just set the request to HEAD
$out = "HEAD /img/baidu_sylogo1.gif HTTP/1.1rn";
$out .= "Host: www.baidu.comrn";
$out .= "Connection: Closernrn";
fwrite($fp, $out);
while (!feof($fp) ) {
$header = fgets($fp);
if (stripos($header, 'Content-Length') !== false) {
$size = trim(substr($header, strpos ($header, ':') + 1));
                                                                                                                   ; ($errno)";
}



Same as initiating a GET request, just set the request type GET to HEAD. Just modify the requested host and path to what you need.

Summary:
You can also use get_headers in php to obtain header information, but the author has tested this function and it is a GET request. For details, please refer to: Is the php function get_headers a HEAD request or a GET request.

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

truehttp: //www.bkjia.com/PHPjc/621654.htmlTechArticleIf you want to get the size of the 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, and then...
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!