本文介绍了在php中超级快速getimagesize的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试获取数百张远程图像的图像尺寸(图像尺寸,宽度和高度),并且太慢了。

I am trying to get image size (image dimensions, width and height) of hundreds of remote images and getimagesize is way too slow.

我做了一些阅读,发现最快的方法是使用从图像中读取一定数量的字节并检查二进制数据中的大小。

I have done some reading and found out the quickest way would be to use file_get_contents to read a certain amount of bytes from the images and examining the size within the binary data.

以前有人尝试过吗?我该如何检查不同的格式?有人见过这个库?

Anyone attempted this before? How would I examine different formats? Anyone has seen any library for this?

推荐答案

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 ...

加载32kb数据为我工作。

Loading 32kb of data worked for me.

这篇关于在php中超级快速getimagesize的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-15 09:59