最快的速度获取网页所有图片的长和宽。
不知道大家有没有玩过 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的图片 } }}
------解决方案--------------------
也许能走个弯路,减轻服务器网络压力。
服务器负责解析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>

PHP is used to build dynamic websites, and its core functions include: 1. Generate dynamic content and generate web pages in real time by connecting with the database; 2. Process user interaction and form submissions, verify inputs and respond to operations; 3. Manage sessions and user authentication to provide a personalized experience; 4. Optimize performance and follow best practices to improve website efficiency and security.

PHP uses MySQLi and PDO extensions to interact in database operations and server-side logic processing, and processes server-side logic through functions such as session management. 1) Use MySQLi or PDO to connect to the database and execute SQL queries. 2) Handle HTTP requests and user status through session management and other functions. 3) Use transactions to ensure the atomicity of database operations. 4) Prevent SQL injection, use exception handling and closing connections for debugging. 5) Optimize performance through indexing and cache, write highly readable code and perform error handling.

Using preprocessing statements and PDO in PHP can effectively prevent SQL injection attacks. 1) Use PDO to connect to the database and set the error mode. 2) Create preprocessing statements through the prepare method and pass data using placeholders and execute methods. 3) Process query results and ensure the security and performance of the code.

PHP and Python have their own advantages and disadvantages, and the choice depends on project needs and personal preferences. 1.PHP is suitable for rapid development and maintenance of large-scale web applications. 2. Python dominates the field of data science and machine learning.

PHP is widely used in e-commerce, content management systems and API development. 1) E-commerce: used for shopping cart function and payment processing. 2) Content management system: used for dynamic content generation and user management. 3) API development: used for RESTful API development and API security. Through performance optimization and best practices, the efficiency and maintainability of PHP applications are improved.

PHP makes it easy to create interactive web content. 1) Dynamically generate content by embedding HTML and display it in real time based on user input or database data. 2) Process form submission and generate dynamic output to ensure that htmlspecialchars is used to prevent XSS. 3) Use MySQL to create a user registration system, and use password_hash and preprocessing statements to enhance security. Mastering these techniques will improve the efficiency of web development.

PHP and Python each have their own advantages, and choose according to project requirements. 1.PHP is suitable for web development, especially for rapid development and maintenance of websites. 2. Python is suitable for data science, machine learning and artificial intelligence, with concise syntax and suitable for beginners.

PHP is still dynamic and still occupies an important position in the field of modern programming. 1) PHP's simplicity and powerful community support make it widely used in web development; 2) Its flexibility and stability make it outstanding in handling web forms, database operations and file processing; 3) PHP is constantly evolving and optimizing, suitable for beginners and experienced developers.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Dreamweaver CS6
Visual web development tools

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

SublimeText3 Linux new version
SublimeText3 Linux latest version

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

WebStorm Mac version
Useful JavaScript development tools