The fastest speed gets the length and width of all the pictures on the page. Solutions

Source: Internet
Author: User
The fastest speed gets the length and width of all the pictures on the page.
I wonder if you've ever played http://pinterest.com? After registering, it has an add a pin, and when you submit a URL to a website, when you press find images, it can look up all the pictures you submit on the page (both the governor and the wide conditional filter), the whole process is usually around 10 seconds.

Recently want to imitate it, make a small functional component. Has abandoned the evil getimagesize (48.64 seconds), swapping with imagecreatefromstring (or 26.13 seconds), and its 10 seconds or so of the results, is simply a difference.

To consider the number of TCP connections, to achieve the most provincial server resources, but also to consider the minimization of execution time. How do you continue to optimize your code for the mighty prawns? can run faster.

PHP Code
  
   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 sets require dirname (__file__). '/simple_html_dom.php '; Use simple_html_dom.php to parse 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;//get older than 200, larger than equal to 200 picture        }}}    



------Solution--------------------
May be able to take a detour, reduce the server network pressure.
The server is responsible for parsing the HTML data, statistics image tag information, and finally sending the collected text data back to the client.
Loading the image is done by the client, just read the Width,height property, you can get the original size of the picture.
A lot of benefits, but the possible trouble is the chain of anti-theft
------Solution--------------------
Top floor
PHP Get Resources
JavaScript takes picture length and width
------Solution--------------------
Read and parse for 2.8 seconds
Read pictures (138) 27 seconds
7 Total found

Only from the optimized code, it should not be oil and water
Multi-path concurrency can be considered
------Solution--------------------
Read and parse for 3.6 seconds
Start read picture process (138) 1.3 seconds
Number of records in result file 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
------Solution--------------------
I think it's possible for the client to load the scenario,

The client then submits the requested picture information to the server, and the server verifies it once again to save ...


How did the other 32768 come from? 1-200 not enough?
------Solution--------------------
Learn! is the header of the image directly read by using PHP to get the image URL?
------Solution--------------------
Pinterest that pin function creativity is very good, and the technology is very simple, is bookmarks a string of JS code, then you point this bookmark is equivalent to the current page document append into a JS file, this JS file how to write, it is very simple, The main thing is to traverse document.getelementsbytagname (' img ')
------Solution--------------------
138 Photos Concurrent, is not consumed 138 connection number
Right

If you need to modify php.ini, increase the number of connections
No, the connection is outward, if you want to change, it is the other side to change

how CPU and memory overhead
This is a bad test.

and, about using JS to judge the problem, because they did not give code, can not test
I wrote two of the plans are not ideal, also do it

with JS concurrency and direct php concurrency, 2 from the resource consumption point of view, which will be less
The resource consumption angle is the same, all the pictures should be loaded completely
However, the former consumes client resources, the latter consumes server-side resources
In addition the browser mechanism does not know very well, whether it is really concurrency is also unknown
------Solution--------------------
This code is in my place for about 1.8 seconds, not counting file_get_html ($url) time

$res [] = $url;//$temp;
This is the network address.

He was saved as a local file and used getimagesize to get the size of the

He's supposed to be through curl, and this mechanism I don't quite understand.
------Solution--------------------
But if (In_array ($ABSURL, $visited)) continue; This line is an error. Warning:in_array () expects parameter 2 to is array, null.
  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.