views:

82

answers:

4

I want to open a URL and RegEx all the image's URLs from the page. Then I want to cURL all of them and check what size they have. In the end I want to get the biggest one. How do I do this?

+2  A: 

You could start with getting the URL using curl, saving it in a variable.
Then you could apply a regex like this one: <img.*?src=['"](.*?)['"]>

Check if the source starts with http or is a relative link, if its a relative link you can prepend the url of the page.

Finally get the size of the images using getimagesize() http://php.net/manual/en/function.getimagesize.php

gX
What about img tags with a newline somewhere in it?
Time Machine
What about img tags which are closed?
Time Machine
What about CSS sprites?
Time Machine
What about images loaded using AJAX?
Time Machine
What about images loaded using a plug-in?
Time Machine
What about images... well... 6 comments are enough...
Time Machine
hehe trust me i get the point, I went through the complete CodingHorrors article :)
gX
mkay I trust you =]
Time Machine
+4  A: 

From http://stackoverflow.com/questions/1732348/regex-match-open-tags-except-xhtml-self-contained-tags:

You can't parse [X]HTML with regex. Because HTML can't be parsed by regex. Regex is not a tool that can be used to correctly parse HTML. As I have answered in HTML-and-regex questions here so many times before, the use of regex will not allow you to consume HTML. Regular expressions are a tool that is insufficiently sophisticated to understand the constructs employed by HTML. HTML is not a regular language and hence cannot be parsed by regular expressions. Regex queries are not equipped to break down HTML into its meaningful parts. so many times but it is not getting to me. Even enhanced irregular regular expressions as used by Perl are not up to the task of parsing HTML. You will never make me crack. HTML is a language of sufficient complexity that it cannot be parsed by regular expressions. Even Jon Skeet cannot parse HTML using regular expressions. Every time you attempt to parse HTML with regular expressions, the unholy child weeps the blood of virgins, and Russian hackers pwn your webapp. Parsing HTML with regex summons tainted souls into the realm of the living. HTML and regex go together like love, marriage, and ritual infanticide. The <center> cannot hold it is too late. The force of regex and HTML together in the same conceptual space will destroy your mind like so much watery putty. If you parse HTML with regex you are giving in to Them and their blasphemous ways which doom us all to inhuman toil for the One whose Name cannot be expressed in the Basic Multilingual Plane, he comes. HTML-plus-regexp will liquify the n​erves of the sentient whilst you observe, your psyche withering in the onslaught of horror. Rege̿̔̉x-based HTML parsers are the cancer that is killing StackOverflow it is too late it is too late we cannot be saved the trangession of a chi͡ld ensures regex will consume all living tissue (except for HTML which it cannot, as previously prophesied) dear lord help us how can anyone survive this scourge using regex to parse HTML has doomed humanity to an eternity of dread torture and security holes using regex as a tool to process HTML establishes a breach between this world and the dread realm of c͒ͪo͛ͫrrupt entities (like SGML entities, but more corrupt) a mere glimpse of the world of reg​ex parsers for HTML will ins​tantly transport a programmer's consciousness into a world of ceaseless screaming, he comes, the pestilent slithy regex-infection wil​l devour your HT​ML parser, application and existence for all time like Visual Basic only worse he comes he comes do not fi​ght he com̡e̶s, ̕h̵i​s un̨ho͞ly radiańcé destro҉ying all enli̍̈́̂̈́ghtenment, HTML tags lea͠ki̧n͘g fr̶ǫm ̡yo​͟ur eye͢s̸ ̛l̕ik͏e liq​uid pain, the song of re̸gular exp​ression parsing will exti​nguish the voices of mor​tal man from the sp​here I can see it can you see ̲͚̖͔̙î̩́t̲͎̩̱͔́̋̀ it is beautiful t​he final snuffing of the lie​s of Man ALL IS LOŚ͖̩͇̗̪̏̈́T ALL I​S LOST the pon̷y he comes he c̶̮omes he comes the ich​or permeates all MY FACE MY FACE ᵒh god no NO NOO̼O​O NΘ stop the an​*̶͑̾̾​̅ͫ͏̙̤g͇̫͛͆̾ͫ̑͆l͖͉̗̩̳̟̍ͫͥͨe̠̅s ͎a̧͈͖r̽̾̈́͒͑e n​ot rè̑ͧ̌aͨl̘̝̙̃ͤ͂̾̆ ZA̡͊͠͝LGΌ ISͮ̂҉̯͈͕̹̘̱ TO͇̹̺ͅƝ̴ȳ̳ TH̘Ë͖́̉ ͠P̯͍̭O̚​N̐Y̡ H̸̡̪̯ͨ͊̽̅̾̎Ȩ̬̩̾͛ͪ̈́̀́͘ ̶̧̨̱̹̭̯ͧ̾ͬC̷̙̲̝͖ͭ̏ͥͮ͟Oͮ͏̮̪̝͍M̲̖͊̒ͪͩͬ̚̚͜Ȇ̴̟̟͙̞ͩ͌͝S̨̥̫͎̭ͯ̿̔̀ͅ

Have you tried using an XML parser instead?

Also, you should thing about CSS Sprites, AJAX-loaded images, etc... <-- this is really hard to do!

Time Machine
good idea.... poor soul :-D
daniel
+1  A: 

Use the php DOM to find the images.

I have not tested this code at all, but it should get you headed in the right direction.

$urls = array();
$dom = DOMDocument::loadHTML(YOUR_HTML);
$imgList = $dom->getElementsByTagName('img');
$imgCount = $imgList->length;
for ($i = 0; $i < $imgCount; $i++) {
    $imgElement = $imgList->item($i);
    if ($imgElement->hasAttribute('src')) {
        $urls[] = $imgElement->getAttribute('src');
    }
}

If you want to get linked images, you can change 'img'/'src' to 'a'/'href'. But you will need to find a way to filter the list to get only images.

You did not say what your criteria is for image size, so I can't help you there. Do you want maximum file size or resolution?

mcrumley
A: 

It might be already obvious by now, use a DOM parser, not regex. Just get all elements by tag name <img> and then get for each the URL from its src attribute. To determine the image's size without downloading the entire image, you'd probably like to fire a HTTP HEAD request instead and then determine the Content-Length header in the obtained response. The http_head() may be useful in this.

BalusC