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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T21:07:46+00:00 2026-05-11T21:07:46+00:00

<?php foreach (glob(*.jpg) as $filename) { echo $filename size . filesize($filename) . <br>; echo

  • 0
<?php
foreach (glob("*.jpg") as $filename) {
    echo "$filename size " . filesize($filename) . "<br>";
    echo '<img src="'.$filename.'" height=150px><br>';
}
?>

using that code, i am able to display all the images in the folder
i want to display only one that meets a size parameter, and if there are more ignore them
basically want to display the one thats like a box

and alternatively i want to display only the first image of that foreach loop
and if there are no images display a default image

EDIT
so

<?php
foreach (glob("*.jpg") as $filename) {
    $info = getimagesize($filename);
    if ($info[0] == $info[1]) {
        echo "$filename size " . filesize($filename) . "<br>";
        echo '<img src="'.$filename.'" height=150px><br>';
        break;
    }
}
?>

gives you an image if its a box, (thanks to schnaader)
right but images that come close to a box should be included too, how can that be sorted,
meaning if the height divided by width gives more than 1

  • 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-11T21:07:47+00:00Added an answer on May 11, 2026 at 9:07 pm

    You can display only the first image by using break:

    foreach (glob("*.jpg") as $filename) {
        echo "$filename size " . filesize($filename) . "<br>";
        echo '<img src="'.$filename.'" height=150px><br>';
        break;
    }
    

    For only printing the one that meets a specific size, use getimagesize and compare the width/height with the one you want, so basically use the code above, but wrap an if around the lines inside the foreach loop. The following could work, but I haven’t used PHP for long, so don’t rely on it:

    foreach (glob("*.jpg") as $filename) {
        $info = getimagesize($filename);
        if ($info[0] == $width_i_want) {
          if ($info[1] == $height_i_want) {
            echo "$filename size " . filesize($filename) . "<br>";
            echo '<img src="'.$filename.'" height=150px><br>';
            break;
          }
        }
    }
    

    And if you want to get the square-sized image, use

    if ($info[0] == $info[1]) {
      [...]
    

    EDIT: To get the image which is closest to square-sized, try this:

    $record_ratio = 0;
    foreach (glob("*.jpg") as $filename) {
        $info = getimagesize($filename);
        $ratio = $info[0] / $info[1];
        if (abs(1 - $ratio) < abs(1 - $record_ratio)) {
          $record_ratio = $ratio;
          $record_filename = $filename;
        }
        if (record_ratio == 1) break;
    }
    if ($record_ratio > 0) {
      echo '<img src="'.$record_filename.'" height=150px><br>';
    }
    

    Variations of this can also give you the images sorted by ratio or images with a ratio between two values (for example 0.75 <= ratio <= 1.25).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

using this code <?php foreach (glob(*.txt) as $filename) { $file = $filename; $contents =
I've been using the following snippet to include multiple files: foreach(glob(APP_PATH.libs/*.php) as $path) {
http://php.net/glob The documentation page on glob() has this example: <?php foreach (glob("*.txt") as $filename)
The code for the page is the following <?php foreach ($organization->index() as $id=>$content) {
In PHP I can use a foreach loop such that I have access to
I am using a foreach loop within PHP similar to this: foreach ($class->getAttributes() as
I wrote mini function for including all php files in directory foreach (glob(*.php) as
<?php foreach ($jobeet_job_list as $jobeet_job): ?> <tr> <td><a href=<?php echo url_for('job/show?id='.$jobeet_job->getId()) ?>><?php echo $jobeet_job->getId()
Here is my code: <?php foreach ($users as $user) { //some php code here
Possible Duplicate: On every third iteration in PHP Using PHP foreach to get a

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.