开发者

verify if a given URL is a valid Image/size using HEAD method

开发者 https://www.devze.com 2023-03-17 06:15 出处:网络
How should one check an URL with HEAD method to check if the given URL is an image and it not exceed 200x100 px开发者_运维问答?This is inherently impossible.

How should one check an URL with HEAD method to check if the given URL is an image and it not exceed 200x100 px开发者_运维问答?


This is inherently impossible.
The HEAD method does not return any information about the resource's content.


You can by download enough data (not entire image), and then check...

function ranger($url){
    $headers = array(
    "Range: bytes=0-32768"
    );

    $curl = curl_init($url);
    curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
    $data = curl_exec($curl);
    curl_close($curl);
    return $data;
}

$start = microtime(true);

$url = "http://news.softpedia.com/images/news2/Debian-Turns-15-2.jpeg";

$raw = ranger($url);
$im = imagecreatefromstring($raw);

$width = imagesx($im);
$height = imagesy($im);

$stop = round(microtime(true) - $start, 5);

echo $width." x ".$height." ({$stop}s)";

test...

640 x 480 (0.20859s)

Loading 32kb of data worked for me. (image is 90kb)

0

精彩评论

暂无评论...
验证码 换一张
取 消