<?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
You can display only the first image by using 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:
And if you want to get the square-sized image, use
EDIT: To get the image which is closest to square-sized, try this:
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).