I need to find the largest height & width values in a directory. I've tried to use:

if ($handle = opendir('path/to/image')) {
    while (false !== ($file = readdir($handle))) {
        if (preg_match('/(jpg|gif|png)/', $file )) {
            $images[] = $file;
            list($width, $height) = getimagesize('path/to/image/'.$file);
            $max_w = max(array($width));
            $max_h = max(array($height));
        }
    }
    closedir($handle);
}

$max_w returns the last image in an arrrays size, as it does in a foreach statement.
This works: $max_w = max(array(135, 100, 150, 101));, but it reads as a string above.
How do I get the width into an array so I can get the max value?

hi,

Try reading One and two.

Modify codes presented on two

echo 'wide image<br/>';
$k = 0;
//$area = 0;
$x = 0;
$y = 0;
$largest_image;
for ($i = 0; $i <= sizeof ($a_img); $i++) {
    if(@$a_img[$i]) {
        if(@getimagesize (@$imgdir.$a_img[$i])) {
                list($width, $height, $type, $attr) = getimagesize(@$imgdir.$a_img[$i]);
            // calculate current image area
            $latest_area = $width; //$width * $height;
            // if current image area is greater than previous
            //if($latest_area > $area) {
            if($latest_area > $x){
                //$area = $latest_area;
                $x = $area = $latest_area;
                $largest_image = $a_img[$i];
                //echo $largest_image.'<br/>';
                $k++;
            }
        }
    }
}



print_r($imgdir.$a_img[$k]);

$imgdir.$a_img[$k] gives you the widest image in the directory. To find the tallest modify the codes for $y.

Correction, this

$x = $area = $latest_area;

should read

$x =  $latest_area;

sorry about that.. :)

This works if the largest image is first. I'd like to know if max could be used, if I could get the widths into an array.

What if two or more images are the largest? Anyway, here's an example with glob():

<?php

$f = glob('./path/*.{jpg,png,gif}', GLOB_BRACE);
$r = array();

foreach($f as $file)
{
    list($w, $h) = getimagesize($file);
    $r[$w*$h][] = array(
            'name'  => $file,
            'width' => $w,
            'height' => $h
        );
}

$keys = array_keys($r);

# all results
echo "<pre>";
print_r($r);
echo "</pre>";

# one of the largest
echo $r[max($keys)][0]['name'];

# array with the widest images
echo "<pre>";
print_r($r[max($keys)]);
echo "</pre>";

Bye!

Here's what I ended up doing. I put together a function that can get the minimum or maximun height or width:

function imageMaxDimension ($dir, $ort, $ord) {
    if ($handle = opendir($dir)) {
            while (false !== ($f = readdir($handle))) {
            if (preg_match('/(jpg|gif|png)/', $f )) {
                list($w, $h) = getimagesize($dir.$f);
                $d[] = $ort == 'width'
                    ? $w
                    : $h;
                $o = $ord == 'max'
                    ? max($d)
                    : min($d);
                $v = $o;
            }
        }
        closedir($handle);
        return $v;
    }
}

$dir = 'path/to/directory/';
$ort = 'width'; // width or height
$ord = 'max';   // minimum or maximum
echo imageMaxDimension ($dir, $ort, $ord);

okay, let me try one more time, but I strongly suggests to try out Cereal's approach first.

I'm kind of lazy today so I will be using my referenced link above. That would be the first one.

I will be modifying this script.

I will chop it and add some ingredients to it as needed..

<?php

    ## we want those arrays defined first
    $current_width = array();
    $current_height = array();

    $imgdir = 'images/test_image/'; //Pick your folder

    $allowed_types = array('png','jpg','jpeg','gif'); //Allowed extensions

    $dimg = opendir($imgdir);//Open directory
    while($imgfile = readdir($dimg))
    {
      if( in_array(strtolower(substr($imgfile,-3)),$allowed_types) OR
          in_array(strtolower(substr($imgfile,-4)),$allowed_types) )
    /*If the file is an image add it to the array*/
      {

      $a_img[] = $imgfile;
      list($width, $height) = getimagesize($imgdir.$imgfile);

          ## we assign the current width and height in the loop into the content_width/height arrays.

            $current_width[] = $width; 
            $current_height[] = $height;

      }

    }

        echo 'max value width x :'. (max($current_width));
        echo '<br/>';
        echo 'max value y :'. (max($current_height));

That's pretty much we need. If we do this outside the while loop

var_dump($current_height);

it should give us an array of heights of the images from the directory. The output should be something like this. This is just my imaginary dimensions.

array(8) { [0]=> int(400) [1]=> int(900) [2]=> int(332) [3]=> int(593) [4]=> int(450) [5]=> int(261) [6]=> int(600) [7]=> int(450) } 

if we do the same for the widths,

var_dump($current_width);

again, it will give us something similar to this

array(8) { [0]=> int(500) [1]=> int(1200) [2]=> int(500) [3]=> int(768) [4]=> int(600) [5]=> int(193) [6]=> int(600) [7]=> int(600) } 

so, our first imaginary image have an approximate dimension of 500 x 400.

If we do this,

echo 'max value width x :'. (max($current_width));

we will get

1200

The same is certain for the max height

echo '<br/>';
echo 'max value y :'. (max($current_height));

we should get

900

as expected from the given dimension on the height array.

That's pretty much I can do with this question.... I hope I have contributed something rather than adding more confusion to the problem presented.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.