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 196365
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T16:45:40+00:00 2026-05-11T16:45:40+00:00

I’m working with arrays of image filepaths. A typical array might have 5 image

  • 0

I’m working with arrays of image filepaths. A typical array might have 5 image filepaths stored in it.

For each array, I want to pull out just the “best” photo to display as a thumbnail for the collection.

I find looping and arrays very confusing and after 4 hours of trying to figure out how to structure this, I’m at a loss.

Here are the rules I’m working with:

  1. The very best photos have “-large” in their filepaths. Not all arrays will have images like this in them, but if they do, that’s always the photo I want to pluck out.

  2. The next best photos are 260px wide. I can look this up with getimagesize. If I find one of these, I want to stop looking and use it.

  3. The next best photos are 265 wide. If I find one I want to use it and stop looking.

  4. The next best photos are 600px wide. Same deal.

  5. Then 220px wide.

Do I need 5 separate for loops? 5 nested for-loops

Here’s what I’m trying:

if $image_array{    
  loop through $image_array looking for "-large"
  if you find it, print it and break;             

  if you didn't find it, loop through $image_array looking for 260px wide.
  if you find it, print it and break;
}

and so on….

But this doesn’t appear to be working.

I want to “search” my array for the best single image based on these criteria. If it can’t find the first type, then it looks for the second on so on. How’s that done?

  • 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-11T16:45:40+00:00Added an answer on May 11, 2026 at 4:45 pm
    // predefined list of image qualities (higher number = best quality)
    // you can add more levels as you see fit
    $quality_levels = array(
        260 => 4, 
        265 => 3, 
        600 => 2,
        220 => 1
    );
    
    
    if ($image_arry) {
    
        $best_image = null;
    
        // first search for "-large" in filename 
        // because looping through array of strings is faster then getimagesize
        foreach ($image_arry as $filename) {
              if (strpos('-large', $filename) !== false) {
                    $best_image = $filename;
                    break;
                }
        }
    
        // only do this loop if -large image doesn't exist
        if ($best_image == null) {
                $best_quality_so_far = 0;
    
            foreach ($image_arry as $filename) {
                $size = getimagesize($filename);
                $width = $size[0];
    
                        // translate width into quality level
                $quality = $quality_levels[$width];
    
                if ($quality > $best_quality_so_far) {
                    $best_quality_so_far = $quality;
                    $best_image = $filename;
                }
            }
        }
    
        // we should have best image now
        if ($best == null) {
            echo "no image found";
        } else {
            echo "best image is $best";
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have just tried to save a simple *.rtf file with some websites and
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I have a French site that I want to parse, but am running into
I have a bunch of posts stored in text files formatted in yaml/textile (from
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want to count how many characters a certain string has in PHP, but
I have a jquery bug and I've been looking for hours now, I can't
Basically, what I'm trying to create is a page of div tags, each has
this is what i have right now Drawing an RSS feed into the php,

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.