Home Backend Development PHP Tutorial 最快的速度获取网页所有图片的长和宽。解决办法

最快的速度获取网页所有图片的长和宽。解决办法

Jun 13, 2016 am 10:16 AM
com curl html images

最快的速度获取网页所有图片的长和宽。
不知道大家有没有玩过 http://pinterest.com ?注册后,它有一个 add a pin, 当你提交一个网站的URL后,按Find Images时,它可以查找你提交网页上所有图片的(并进行长和宽条件的筛选),整个过程一般在10秒左右。

最近想模仿它,做一个小功能组件。已经摒弃掉万恶的 getimagesize() (需要48.64秒),换用 imagecreatefromstring()(还是需要26.13秒),和它10秒左右的成绩,简直是天壤之别。

要考虑 TCP 连接数,要做到服务器资源最省化,还要考虑执行时间最少化。求助万能的大虾们,如何继续优化代码?可以跑的更快些。

PHP code
<!--Code highlighting produced by Actipro CodeHighlighter (freeware)http://www.CodeHighlighter.com/-->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);    return curl_exec($curl);    curl_close($curl);}//curl设置require dirname(__FILE__) . '/simple_html_dom.php'; //采用simple_html_dom.php分析HTML nod$url = 'http://www.huffingtonpost.com/';$html = file_get_html($url);if($html->find('img')){    foreach($html->find('img') as $element) {        $raw = ranger($element->src);        $im = @imagecreatefromstring($raw);        $width = @imagesx($im);        $height = @imagesy($im);        if($width>=200||$height>=200){            echo $element;//得出长大于大于200,宽大于等于200的图片        }    }}
Copy after login

 

------解决方案--------------------
也许能走个弯路,减轻服务器网络压力。
服务器负责解析HTML数据,统计image标签信息,最后将收集的文本数据送回客户端。
加载图片由客户端来完成,只需读取width,height属性,就完全可以获取图片的原始大小。
好处多多,不过可能的麻烦是防盗链
------解决方案--------------------
顶楼上
PHP获取资源
javascript 取图片长和宽
------解决方案--------------------
读取并解析 2.8秒
读取图片(138个) 27秒
找到 7 个

仅从优化代码出发,应该油水不大
可考虑多路并发
------解决方案--------------------
读取并解析 3.6秒
启动读取图片进程(138个) 1.3秒
结果文件中记录数 7 个
PHP code
http://s.huffpost.com/images/v/logos/v4/tagline.gifhttp://s.huffpost.com/images/v/logos/v4/homepage.gif?v9http://i.huffpost.com/gen/559399/thumbs/r-OLBERMANN-huge.jpghttp://s.huffpost.com/images/facebook_promo_connect.png?3http://images.huffingtonpost.com/2012-04-04-michaeljfoxmarlo2SECOND.jpghttp://images.huffingtonpost.com/2012-04-05-Screenshot20120405at9.40.24AM.jpghttp://i.huffpost.com/gen/557914/thumbs/s-SCORSESE-large300.jpg<br><font color="#e78608">------解决方案--------------------</font><br>我觉得,让客户端加载的方案是可行的,<br><br>客户端再将符合要求的图片信息提交给服务器,服务器端再验证一次后保存。。。<br><br><br>另外32768是怎么得来的?1-200不够吗<br><font color="#e78608">------解决方案--------------------</font><br>学习! 是用PHP获取图片url后直接读取图片的头信息吗?<br><font color="#e78608">------解决方案--------------------</font><br>pinterest那个pin功能创意很好,而且技术很简单,就是书签一串js代码,然后你点这个书签就相当于往当前页面文档append入一个js文件,这个js文件怎么写,就很简单了,主要就是遍历document.getElementsByTagName('img')<br><font color="#e78608">------解决方案--------------------</font><br><strong>138个照片并发,是不是就消耗了138个连接数</strong><br>对<br><br><strong>是否需要修改php.ini,增加连接数</strong><br>否,连接是向外的,如果要改,也是对方改<br><br><strong>CPU和内存开销如何</strong><br>这个不太好测试<br><br><strong>又</strong>,关于使用 js 判断的问题,由于他们没有给出代码,无法测试<br>自己写了两个方案都不理想,也就作罢了<br><br><strong>用JS并发和直接PHP并发,2者从资源消耗角度来比,哪个会更少</strong><br>资源消耗角度来比 都一样,都要完整的加载图片<br>不过前者是消耗客户端资源,后者是消耗服务器端资源<br>另外浏览器的机制不很了解,是否真的是并发也未可知<br><font color="#e78608">------解决方案--------------------</font><br>这段代码在我这里大约 1.8秒,不计算 file_get_html ( $url ) 时间<br><br>$res [] = $url ;//$temp;<br>这样就是网络地址了<br><br>他是保存为本地文件后用 getimagesize 获取尺寸的<br><br>他应该是通过 curl 并发的,这个机制我不太了解<br><font color="#e78608">------解决方案--------------------</font><br>但是 if(in_array($absUrl, $visited))continue; 这行报错。 Warning: in_array() expects parameter 2 to be array, null。<div class="clear">
                 
              
              
        
            </div>
Copy after login
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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Table Border in HTML Table Border in HTML Sep 04, 2024 pm 04:49 PM

Guide to Table Border in HTML. Here we discuss multiple ways for defining table-border with examples of the Table Border in HTML.

HTML margin-left HTML margin-left Sep 04, 2024 pm 04:48 PM

Guide to HTML margin-left. Here we discuss a brief overview on HTML margin-left and its Examples along with its Code Implementation.

Nested Table in HTML Nested Table in HTML Sep 04, 2024 pm 04:49 PM

This is a guide to Nested Table in HTML. Here we discuss how to create a table within the table along with the respective examples.

HTML Table Layout HTML Table Layout Sep 04, 2024 pm 04:54 PM

Guide to HTML Table Layout. Here we discuss the Values of HTML Table Layout along with the examples and outputs n detail.

HTML Input Placeholder HTML Input Placeholder Sep 04, 2024 pm 04:54 PM

Guide to HTML Input Placeholder. Here we discuss the Examples of HTML Input Placeholder along with the codes and outputs.

HTML Ordered List HTML Ordered List Sep 04, 2024 pm 04:43 PM

Guide to the HTML Ordered List. Here we also discuss introduction of HTML Ordered list and types along with their example respectively

Moving Text in HTML Moving Text in HTML Sep 04, 2024 pm 04:45 PM

Guide to Moving Text in HTML. Here we discuss an introduction, how marquee tag work with syntax and examples to implement.

HTML onclick Button HTML onclick Button Sep 04, 2024 pm 04:49 PM

Guide to HTML onclick Button. Here we discuss their introduction, working, examples and onclick Event in various events respectively.

See all articles