Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 5948561
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T17:07:30+00:00 2026-05-22T17:07:30+00:00

I’m trying to write a program that will grab a bunch of images from

  • 0

I’m trying to write a program that will grab a bunch of images from a webpage and figure out which of the images is the largest.

So far I’ve taken the images, put them into an array, used the getimagesize() function to determine the heights. I then placed all of the heights into another array and sorted them in reverse order to get the largest one. So far so good.

My problem now is that I must find a way to re-associate the largest image with its initial image link. I’ve thought of potentially running the initial code to fetch the images again from the web page. Then comparing the 1st value in the array I’ve used to determine the biggest image with the images a second time, but this seems like a waste of bandwidth and I get a feeling there is an easier way to re-associate the height value with its initial image. Am I right?

<?php

$url = 'http://lockerz.com/s/104049300';

// Fetch page
$string = FetchPage($url);

// Regex that extracts the images (full tag)
$image_regex_src_url = '/<img[^>]*'.

'src=[\"|\'](.*)[\"|\']/Ui';

preg_match_all($image_regex_src_url, $string, $out, PREG_PATTERN_ORDER);

$img_tag_array = $out[0];

echo "<pre>"; print_r($img_tag_array); echo "</pre>";

// Regex for SRC Value
$image_regex_src_url = '/<img[^>]*'.

'src=[\"|\'](.*)[\"|\']/Ui';

preg_match_all($image_regex_src_url, $string, $out, PREG_PATTERN_ORDER);

$images_url_array = $out[1];
$image_heights_array = array();

foreach ($images_url_array as $imagelink)
{
if (substr($imagelink,0,7)=="http://")
    {
     $getheight = getimagesize($imagelink);
     array_push($image_heights_array,"$getheight[1]");
    }
}

rsort($image_heights_array);
echo "<pre>"; print_r($image_heights_array); echo "</pre>";
// Fetch Page Function

function FetchPage($path)
{
$file = fopen($path, "r");

if (!$file)
{
exit("The was a connection error!");
} 

$data = '';

while (!feof($file))
{
// Extract the data from the file / url

$data .= fgets($file, 1024);
}
return $data;
}
?>  
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-22T17:07:30+00:00Added an answer on May 22, 2026 at 5:07 pm

    First off:

    // Regex that extracts the images (full tag)
    $image_regex_src_url = '/<img[^>]*'.
    
    'src=[\"|\'](.*)[\"|\']/Ui';
    
    preg_match_all($image_regex_src_url, $string, $out, PREG_PATTERN_ORDER);
    
    $img_tag_array = $out[0];
    
    echo "<pre>"; print_r($img_tag_array); echo "</pre>";
    
    // Regex for SRC Value
    $image_regex_src_url = '/<img[^>]*'.
    
    'src=[\"|\'](.*)[\"|\']/Ui';
    
    preg_match_all($image_regex_src_url, $string, $out, PREG_PATTERN_ORDER);
    

    HTML parsing regex ewww. Let’s simplify this… with an HTML parser.

    <?php
    // EDIT: Use a custom function to do the
    // reverse of SORT_NUMERIC with asort
    function height_compare($a, $b)
    {
        if ($a == $b) {
            return 0;
        }
        return ($a > $b) ? -1 : 1;
    }
    
    $url = 'http://lockerz.com/s/104049300';
    
    $doc = new DOMDocument();
    // Okay this is HTML is kind of screwy
    // So we're going to supress errors
    @$doc->loadHTMLFile($url);
    
    // Get all images
    $images_list = $doc->getElementsByTagName('img');
    
    $images = array();
    foreach($images_list as $image) {
      // Get the src attribute
      $image_source = $image->getAttribute('src');
      $image_size_info = getimagesize($image_source);
    
      $images[$image_source] = $image_size_info[1];
    }
    
    // Do a numeric sort on the height
    uasort($images, "height_compare");
    print_r($images);
    ?>
    

    Much shorter and more readable. The result:

    $ php test.php 
    
    Array
    (
        [http://c0013784.cdn1.cloudfiles.rackspacecloud.com/x2_633aa94] => 328
        [http://ad.doubleclick.net/ad/buz.plixi/photos;pos=300a;celeb=;kw=;tile=2;sz=300x250,300x600;ord=123456789?] => 250
        [http://static.lockerz.com/pegasus/images/video_thumb.jpg?1.0.0] => 207
        [http://static.lockerz.com/pegasus/images/plixi-banner.png?1.0.0] => 107
        [http://ad.doubleclick.net/ad/buz.plixi/photos;pos=728a;celeb=;kw=;tile=1;sz=728x90;ord=123456789?] => 90
        [http://c0013787.cdn1.cloudfiles.rackspacecloud.com/x2_636f30c] => 79
        [http://c0013787.cdn1.cloudfiles.rackspacecloud.com/x2_637676e] => 79
        [http://c0013787.cdn1.cloudfiles.rackspacecloud.com/x2_63735a0] => 79
        [http://c0013787.cdn1.cloudfiles.rackspacecloud.com/x2_636e73c] => 79
        [http://c0013787.cdn1.cloudfiles.rackspacecloud.com/x2_63795d0] => 79
        [http://c0013787.cdn1.cloudfiles.rackspacecloud.com/x2_636a2c7] => 79
        [http://c0013787.cdn1.cloudfiles.rackspacecloud.com/x2_636bf79] => 79
        [http://c0013787.cdn1.cloudfiles.rackspacecloud.com/x2_636ca08] => 79
        [http://c0013787.cdn1.cloudfiles.rackspacecloud.com/x2_636e419] => 79
        [http://c0013787.cdn1.cloudfiles.rackspacecloud.com/x2_636deca] => 79
        [http://c0013787.cdn1.cloudfiles.rackspacecloud.com/x2_6384277] => 79
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I am trying to understand how to use SyndicationItem to display feed which is
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have a text area in my form which accepts all possible characters from
I need a function that will clean a strings' special characters. I do NOT
I'm trying to create an if statement in PHP that prevents a single post
I have a bunch of posts stored in text files formatted in yaml/textile (from
I am trying to loop through a bunch of documents I have to put
Basically, what I'm trying to create is a page of div tags, each has
I'm new to using the Perl treebuilder module for HTML parsing and can't figure

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.